What is git diff & git branch

Amit Prajapati
MindOrks
Published in
7 min readSep 18, 2019

In this blog, we will try to understand the most important commands i.e git diff and git branch.

Today’s motivation

“Let go of the past and go for the future. Go confidently in the direction of your dreams. Live the life you imagined”.

Let’s get started… 🙂

What is gitk?

The gitk is the git repository browser. Displays changes in a repository or a selected set of commits. This includes visualizing the commit-graph, showing information related to each commit, and the files in the trees of each revision.

Historically, gitk was the first repository browser. It’s written in Tcl/Tk ( Tool Command Language and Tk is a graphical user interface toolkit) and started off in a separate repository but was later merged into the main git repository.

gitk --all: Show all branches.

gitk --since=<date>: Show commits more recent than a specific date.

for e.g. git --since=”2 weeks ago” : Show the changes during the last two weeks to the file gitk. The “--” is necessary to avoid confusion with the branch named gitk.

gitk --until=<date>: Show commits older than a specific date.

This command will open a git repository browser.

Understanding Comparing files with diff

The main objective of version control is to enable you to work with different versions of files. Git provides a diff command to let you compare different versions of your files. The most common scenario to use diff is to see what changes you made after your last commit.

The above image shows our remote repository.

for e.g: Let’s say, you have changed the file index.txt. Suppose, you want to view what you have changed in the index.txt file. To view the differences we need to run git diff [file-name] command.

git diff : Compares the working directory with index, i.e. shows the changes that are not staged yet.

git diff --staged [file-name] : To view the changes in the staging area you need to use --staged flag.

The text in red color shows the previous content which was overwritten with the new content which is in green color as you see in the above image. Now, commit the changes and push to the remote repository.

To view changes in the remote repository, see the image below.

This the place where you can view your commits.
The pink color simply means removed contents, and green color means added new content at the place of the previous commit.

For e.g: Let’s say, you again changed index.txt and added the file in the staging area.

As you see in the above image, The command git diff doesn’t show anything it because the index.txt file is in the staging area. Once you add the files in the staging area, the git diff command won’t show anything. So, to view the changes in the staging area. You need to run git diff --staged [file-name] command.

After some time your team member says that you have to remove that “- good one” from the ‘index.txtfile. Now, you have to reset the changes, but how? Let’s understand, firstly you have to unstage the files index.txt because it’s in the staged area right now. We use git reset [file-name] to unstage.

Then after, you have to run thegit checkout [file-name] command. This command is to updates files in the working tree to match the version in the index or the specified tree or we can say, it switches branches or restores working tree files.

Now, to view the contents of a file in git bash use git show :[file-name] .

The index.txt file is reset successfully

Understanding branches in git

Branching is a feature available in the most modern version control system. Branching in other version control systems can be an expensive operation in both time and disk space. In Git, branches are a part of your everyday development process. Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug-no matter how big or how small -you spawn a new branch to encapsulate your changes. This makes it harder for unstable code to get merged into the main codebase, and it gives you the chance to clean up your future’s history before merging it into the main branch.

The diagram above visualizes a repository with two isolated lines of development, one for a little feature, and one for a longer-running feature. By developing them in branches, it’s not only possible to work on both of them in parallel, but it also keeps the main master branch free from questionable code. A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process.

The git branch command lets you create, list, rename and delete branches. It doesn’t let you switch between branches. For this reason, the git branch is tightly integrated with the git checkout and git merge commands.

For e.g: Suppose, you want to add a login feature on our website so you will create the login.txt file in your working directory. But, you feel that what happens when an error occurs, if errors occur it will disturb the flow of our website because it will disturb our master branch. So, you think let’s create another branch named “login-feature” and try to implement the login feature in this branch. If the newly added login feature gives an error, then no problem? because our previous code will be working properly. How? because it’s in a different branch and our previous code is in the master branch.

git branch <branch-name>

In the above image, we see that the master” occurs in green color with an asterisk (*) symbol which means we can currently on the master branch then use the command ls to view the files in the master branch as you can see we also have a branch called newsBranch’ in that we also have a file called news.txt which was not in the master branch. So, the command git branch is used to view the branches in the repository.

As you see, we created another branch called login-feature which has all files the same as the master branch. Now, we will be created a file named login.txt in this branch.

The command git checkout <branch-name> is used to switch the branch.

We successfully created the login.txt file.

Then after, you pushed your login feature on the remote repository. Let’s have a look at our remote repository.

You see that our remote repository is updated with the new branch and having the login.txt file. After adding the login feature you think it’s working perfectly fine, let’s merge this branch into the master branch means our default branch. So, to merge the branch we have to use git merge <branch-name> command.

To merge the login-feature branch into master firstly you have switched to the master branch then run the git merge command.

Now, after successfully merging the login-feature branch and there no need for a login-feature branch now. So, let’s delete that branch.

Deleting a local/remote

To delete the branch from your system. The command is git branch -d <branch-name> .

As you see in the above image, the login-feature branch is successfully deleted from our system. But, if you see in the remote repository the login-feature is still there as you see in the below image.

So, to delete this branch from remote repo the command is git push origin --delete <branch-name> .

We successfully deleted the branch from the remote repository.

Do clap 👏 the post as much as you can to show your support and motivate me to write more.

Thank you.

--

--