let's understand git diff

rajhawaldar

Raj Hawaldar

Posted on July 26, 2020

let's understand git diff

Have you ever used git diff command? git diff is multi-use command which will help you to find out the difference between git data sources. These data sources can be commits, branches, files.

First understand some key-terms,

Working directory: Git working directory implies the area where we do all our changes. Git continously monitor this area for changes. If you execute command 'git status' you can see the changes present in working directory.

Staging area(also known as git index): staging area contains all the changes which are ready for the commit. You can add/remove files from staging area using command git add or git rm.

HEAD: It is pointer which points to the last commit of your current branch.

Local Repository(.git): Once we commit the changes from staging area usin git commit command they will get stored in your local repository.

tracked file: It is any file which git tracks actively. For example:Files which are already commited or files present in staging area.

untracked file: A newly created file which is not under git's version control.

Now lets move to the git diff command,

git diff: It shows only those changes of tracked files which are present in working directory.

git diff --cached: It shows only those changes of tracked files which are present in staging area.

git diff HEAD: It shows all changes of tracked files which are present in working directory and staging area.

git diff branch1..branch2: Git will compare the HEAD of both branches and display a 'diff' recap that you can use to see modifications. It will show you all the commits that 'branch2” has that are not in 'branch1'.
Alt Text

git diff branch1...branch2: Using 'git diff' with three dots compares the HEAD of the second branch with the common ancestor of the two branches. (It will show the changes made only in branch2).
Alt Text
Note: If we provide file name at the end of all the above commands, the diff operation will be scoped to the specified file.

I hope this is helpful.

💖 💪 🙅 🚩
rajhawaldar
Raj Hawaldar

Posted on July 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

let's understand git diff
git let's understand git diff

July 26, 2020