Git diff

Git diff

Git Diff: Comparing Changes in Your Code

git diff shows the differences between files or commits. It's useful to see what changes have been made in your working directory, staging area, or between commits.

1. Viewing Differences in Your Working Directory

1.1 Check Unstaged Changes

To see the differences between your working directory and the last commit (for unstaged changes):

git diff

🔹 This shows the changes you made but have not staged yet.

1.2 Check Staged Changes

To see what is staged but not committed yet:

git diff --staged

🔹 This shows the differences between the staged changes and the last commit.

2. Comparing Two Commits

You can compare any two commits by specifying their commit hashes.
To compare changes between two commits:

git diff <commit-hash1> <commit-hash2>

🔹 This shows the differences between the two specified commits.

3. Comparing Branches

You can also compare changes between branches.
To compare the differences between branch1 and branch2:

git diff branch1..branch2

🔹 This shows the differences between the two branches.

4. Viewing Differences in Specific Files

To compare a specific file or directory between your working directory and the last commit:

git diff path/to/file

🔹 This shows only the differences for that specific file.

To compare a specific file between commits or branches:

git diff <commit-hash1> <commit-hash2> -- path/to/file

5. Output Formatting Options

You can customize the output of git diff using different options:

5.1 Summary of Changes

To see only a summary of the changes:

git diff --stat

🔹 This shows the number of files changed, lines added, and removed.

5.2 Word-wise Diff

To see a word-by-word diff (useful for small changes):

git diff --word-diff

6. Applying Changes from a Diff

If you want to apply a diff (patch) from a file or output:

git apply <patch-file>

🔹 This applies the changes from a patch file (you can save the output of git diff to a patch file).

7. Conclusion

ActionCommand
View unstaged changesgit diff
View staged changesgit diff --staged
Compare two commitsgit diff <commit-hash1> <commit-hash2>
Compare two branchesgit diff branch1..branch2
Compare specific filegit diff path/to/file
Show a summary of changesgit diff --stat
Word-by-word diffgit diff --word-diff
Apply changes from diffgit apply <patch-file>

🔹 git diff is great for reviewing changes before committing! Need help with a specific use case? 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close