How to use the git diff command. DevOps manual to compare changes in your code.
Andrey Byhalenko
I am a self-driven DevOps expert with 15 years of experience in the IT industry. I write articles aimed at Junior DevOps engineers and those aspiring to become DevOps engineers.
The git diff command It's a powerful tool for examining changes over time, understanding what has been modified, added, or removed, and preparing changes to be committed.
The git diff can show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk.
SYNOPSIS:
git diff [<options>] [<commit>] [--] [<path>…]
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>…]
git diff [<options>] [--merge-base] <commit> [<commit>…] <commit> [--] [<path>…]
git diff [<options>] <commit>…<commit> [--] [<path>…]
git diff [<options>] <blob> <blob>
git diff [<options>] --no-index [--] <path> <path>
I’ve created a sample git repository and cloned it to my local pc.
It contains only the README.md file.
Let me run git diff to see the unstaged changes since the last commit:
As you see, there is nothing here.
I will create first_file file, add some text to it, run git add?. and check git diff again.
Still nothing when I run the git diff. Let’s make another change to the file and run git diff again.
Now I see the changes in the output.
git diff lists all the differences between changes in the working directory and staging area. When I ran the git diff for the first time, it showed nothing because I hadn’t changed anything in the working area after I staged the first changes. So there was nothing to compare.
Then I added the second line to the working area without adding to the staging area and ran the git diff.
Understanding the output results:
diff?—?git a/first-file b/first-file: a/first_file = old file version b/first_file = new file version
@@ -1 +1,2 @@: chunk of modified lines.? - for the old file (what was removed/changed) + for the new file (what was added)
“-# this is the first comment inside the first file”: This line remains in the old file.
“+# this is the first comment inside the first file +# This is the second line inside the first file”: These lines are from the new file.
Here are a few examples of git diff usage:
List all the differences between changes in your working directory and staging area:
git diff
List all changes staged for the commit:
git diff --staged
领英推荐
List both staged and unstaged changes:
git diff HEAD
Compare two branches:
git diff branch1..branch2
This will show you all the commits that branch2 has that branch1 doesn’t.
In the example below, you see all the commits that diff-test has compared to the Development branch.
In the image below, you can see the changes the Development branch has, compared to the diff-test branch.
Compare between the two most recent commits:
git diff HEAD HEAD~1
Compare between the two specific commits:
git diff <commit1> <commit2>
Get a summary of the changes, including how many lines have been added, modified, and removed:
git diff --stat
Use Git Aliases for Common Diffs:
If you find yourself using complex git diff commands frequently, consider adding aliases to your?.gitconfig for quick access.
For example, you could alias difflast to git diff HEAD^ HEAD to quickly compare the last commit to the one before it.
As you see, git diff is a powerful tool.
By mastering git diff and its various options, you can significantly improve your workflow, making it easier to review changes, catch errors early, and understand the history of your project.
If you liked my articles, you can join my newsletter, and you will receive weekly DevOps tutorials, articles, and tips every Saturday.
As a bonus, you will receive a free step-by-step DevOps CI/CD project, which you can use in your portfolio.
Subscribe here: https://junior-devops-hub.ck.page
CEO @ Immigrant Women In Business | Social Impact Innovator | Global Advocate for Women's Empowerment
7 个月???? ??? ?? ?? ???????? ??? ?????? ???? ?????? ???: ?????? ????? ??? ??????? ????? ????? ?????? ??????. https://chat.whatsapp.com/BubG8iFDe2bHHWkNYiboeU