Cover image for Git Branching (II): Explained Like I'm Five

Git Branching (II): Explained Like I'm Five

olawanle_joel profile image Joel Olawanle ・3 min read

In my previous article on Git branching, I explained what Git branching is all about, how to create a branch, and also how to push your codes to a new branch but there are still some few git commands I was unable to make use of, so I decided to explain some of them in this new article.

In this article, I will explain how to check for differences between two branches via git, how to delete a branch, and finally explain the reset git command.

Comparing two branches

Actually, this can easily be done on Github as I explained in the previous article when I wrote a little about Pull request(PR) but my emphasis here is how it can be done via git so you can get familiar with the git command used and also compare before pushing your changes to Github.

  • git diff

When working with Git, it is quite common to use different branches in order to have your work clearly separated from the main codebase.

When working on those branches, you might want to merge branches in order to have the resulting work in your main branch.

Before thinking of merging, the first thing you will like to do is to compare the differences between the two branches since this is very beneficial in order to avoid merging conflicts.

To compare two branches we make use of git diff command.

$ git diff <comparison-branch-name>
Enter fullscreen mode Exit fullscreen mode

This will show you all the commits the comparison-branch has that are not in the main branch.

For Example,
Let's assume I have a project with two branches. The first thing will be to switch to the new branch which I have explained in the previous post.

Alt Text

Once you have switched to this new branch you can go ahead to make changes to your code, save, and now lets check for the differences before pushing to Github.

In my own case, according to the picture above, I have two branches: main and new-branch. After making changes to new-branch, I can now check for the differences (i.e what new-branch has that are not in main).

Alt Text

As you can see, two lines of code have been added to the index.html file.

Git is using a color code in order to display differences done between two branches: lines in green are lines added to the files and lines in red are the ones that are deleted from the files.

That's all about comparing two branches using the git diff command. After checking for differences you can now go ahead to either push and create a pull request.

The next thing I will be explaining briefly is how to delete a branch.

  • git branch -d

This command is used to delete a specified branch from your local repository.

$ git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

The first thing you have to do is make sure you are not on that branch by changing your branch using git checkout <name> then you can now delete the branch.

Alt Text

You can confirm if the branch has been deleted by using the git branch command to check for the available branch.

  • git reset

This command is used to unstage or reset changes made while trying to push your changes to Github. Suppose you already staged an index.html file using the git add index.html command, you can easily unstage by using git reset index.html

 To reset all files staged:
$ git reset

 To reset a specific file:
$ git reset index.html
Enter fullscreen mode Exit fullscreen mode

As always, any questions or suggestions, please feel free to leave a response or tweet me 🤭! Be sure to connect with me on socials! 😎


Editor guide