Hello Guys, in this post we will explore about git diff command options when to use what option etc.
Git basic work flow is going happen initially starts at work area. When you add the files then it will be in the stage area. Once you feel everything is good then we will commit those changes then it will goes to local repository area. In simple words we can say work area -> stage area -> repo area.
The pictorial representation of my understanding about the git workflow:
|
Work flow for git diff command exeuction |
How to Compare files in git to get the last changes?
In git we have this nice feature to compare a file what has been changed who did the changes we can track them with the inputs we can pass as options to the 'git diff' command.
Syntax: git diff [code-file]
Examples:
You can compare and see all the files which are changed with the last commit.
git diff
You can compare and see specific file, for example we consider index.html file recently modified and then git add executed. That means changes are in the staged area.
git diff index.html
Hint: This will compare after git add
How can I compare a file of work area with local repo
We can do compare a file that has changes from local repo, when you run the following command the git with
a pointer HEAD node
Syntax: git diff HEAD [codefile]
Examples:
When we use HEAD pointer, git will show the changes comparing with the work area file(s) with local repository containing file(s)
git diff HEAD
Specific file changes comparing with the work area file with local repository
git diff HEAD ./mist.yml
Hint: This will compare after git commit
|
git diff with HEAD |
Compare between stage area and repo area
Git diff with –stage or –cached options between stage area file with last committed file
Syntax:
git diff [--staged] [code.file]
git diff [--cached] [code.file]
Here is the execution of --stage flag where it will be displaying the changes were made between stage area and repo area.
Examples
git diff --staged
One more example when we choose a specific file:
git diff --staged myweb.html
Hint: This will compare git commit file with git add
|
git diff with staged flag |
How to get the comparison between two commits?
We can compare a file at two different commit levels, where we need to provide the input as two different commit IDs.
Comparing two different commits
Syntax: git diff [commit-SHA-ID] [commit-SHA-ID] [code-file]
Examples: Here I'm using short form of the commit ID, considering min 5 char of SHAcode
git diff e220bb005 e5aa90d79
Now focusing on my specific file here it is mist.yml file that changed between given two commit IDs.
git diff e220bb005 e5aa90d79 mist.yml
Hint: This will compare after multiple git commits use git log –pretty=oneliner
IMAGE: git-diff-commit-IDs
|
git diff with commit IDs |
How to find the list of files that changed between last two commits?
Here we want to get only the name of the files which were modified between last two commits which can be pointed with the HEAD and last but-one using HEAD^.
git diff --name-only HEAD HEAD^
Can I compare two branches?
Yes, it is possible. Now beyond the single branch now, we can compare two branches together this is generally done when a merge request comes and you need to observe the changes.
Syntax: git diff branch1..branch2 [codefile]
Examples:
Here I've created two branches and have different levels of changes in each. Let's see first compare master branch with the vt-dev development branch.
git diff vt-dev..master
Where it shows many changes on the output. Now lets filter to specific file by providing the file name.
git diff vt-dev vt-test mist.yml
IMAGE: The git-diff-branches
There is specific file
Git diff Output Simplified with flags
Simplifying output format with options Stats for the file it is special option which will tells us the file changed were happen to a file and how many changes were made to the file count with a plus symbol. The flag options we have here:
git diff --stat master...vt-dev mist.yml
To show filter out those file name-only will be helpful when you need more concern about the file names:
git diff --name-only vt-dev...feature
We can also get the the file is in which Status is it modified or just Added or Updated an existing committed file with the first letter as indicator. Example M - Modified
git diff --name-status vt-dev...feature
>
No comments:
Post a Comment