Master Git: The 10 Must-Know Commands

Git – the version control system that makes code collaboration and management a breeze. But if you’re new to Git, you might feel like you’re lost in a labyrinth of commands and options. Fear not, because here’s my guide for the top 10 must-know Git commands, that’ll make you want to Git going!

git init

Let’s start at the very beginning – initializing a Git repository. Think of it like giving birth to a new baby repo. To do this, simply run git init in your terminal, and voila! You’ve created a new Git repository. For example:

$ mkdir my-project
$ cd my-project
$ git init
Initialized empty Git repository in /path/to/my-project/.git/

git add

Now that your baby repo is born, it’s time to add some files to it. Use git add to add files to the staging area, where they’ll be ready to be committed. It’s like putting toys into a toy box, so they’re ready to play with. For example:

$ git add index.html

This will add the index.html file to the Git staging area, which means it’s ready to be committed.

git commit

When you’re ready to save your changes, you’ll need to commit them. A commit is like a diary entry – you write a short message about what you’ve changed and then commit it to the repository. For example, “Fixed the bug that made the unicorn fly off the page”. Use git commit -m "your message here" to commit your changes. For example

$ git commit -m "Updated the homepage layout"

This will create a new commit with the message “Updated the homepage layout”.

git status

Before you commit, you might want to see what changes you’ve made. That’s where git status comes in. It’s like asking your mom if you’ve cleaned your room – it’ll tell you if there’s any mess left to clean up. For example:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)

    modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

git log

Want to see all your past diary entries? Use git log. It’s like flipping through the pages of your diary, but for code changes. For example:

$ git log
commit 1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p
Author: Jane Doe <jane@example.com>
Date:   Mon Feb 28 12:00:00 2023 -0500

    Updated the homepage layout

commit 2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q
Author: John Doe <john@example.com>
Date:   Sun Feb 27 12:00:00 2023 -0500

    Added new feature X

This will show you a list of all the commits in your repository, along with the author, date, and commit message.

git branch

Sometimes you need to work on multiple things at once, like a chef who’s cooking different dishes. To keep your changes organized, use git branch to create different branches. Each branch is like a separate kitchen where you can cook up different dishes without mixing them up. For example:

$ git branch feature-x

This will create a new branch called feature-x. You can switch to this branch using the git checkout command (more on that in a moment).

git checkout

If you need to switch between different branches, use git checkout. It’s like changing into different outfits for different occasions. One minute you’re wearing a fancy dress for a dinner party, and the next you’re in sweatpants for a lazy day at home. For example:

$ git checkout feature-x

git merge

When you’re done cooking up a storm in one branch, you might want to merge it back into the main branch. Think of it like a chef bringing all their dishes to the dining table. Use git merge to merge your changes back into the main branch. For example:

git checkout master

Then, run the merge command:

git merge feature-login

git remote

Sometimes you need to work with other chefs in different kitchens. Use git remote to manage remote repositories, like a virtual dining room where everyone can see and share their dishes. For example:

git remote add origin https://github.com/yourusername/yourproject.git

Replace yourusername with your GitHub username and yourproject with the name of your repository.

git push

Finally, when you’re ready to share your code with the world, use git push. It’s like sending your dishes out to the customers in the dining room. Your code will be pushed to the remote repository for others to see and collaborate on. For example

git add .
git commit -m "Your commit message here"

Then, push your changes to the master branch:

git push origin master

If you’ve created a new branch and want to push it to the remote repository, use git push -u origin <branchname> the first time you push the branch, and git push origin <branchname> for subsequent pushes. For example, to push the feature-login branch, you would run:

git push -u origin feature-login

And there you have it – the top 10 must-know Git commands, Remember, Git is a powerful tool that can make your life as a developer easier and more efficient. With these commands under your belt, you’re well on your way to becoming a Git master Happy Coding!