Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

git diff - Difference between a git commit and the working directory?

The git-diff manual pages says that git diff is used to

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.

But what about if you want to show the difference between a commit prior to HEAD and the working directory? Is that possible?

question from:https://stackoverflow.com/questions/18124699/difference-between-a-git-commit-and-the-working-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, but it depends a bit on your definition on what the “current project state” is. The manual for git diff goes on.

If you are interested in comparing with the staged changes:

git diff [--options] --cached [<commit>] [--] [<path>...]

This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. If HEAD does not exist (e.g. unborned branches) and <commit> is not given, it shows all staged changes. --staged is a synonym of --cached.

If you are interested in comparing with the unstaged changes:

git diff [--options] <commit> [--] [<path>...]

This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

Or if you are just interested in comparing any two commits (one could be HEAD):

git diff [--options] <commit> <commit> [--] [<path>...]

This is to view the changes between two arbitrary <commit>s.

So you might want to run git diff someOldCommit HEAD to see the differences between someOldCommit and the current HEAD.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...