How do I show the changes which have been staged?

2019-01-03 00:33发布

I staged a few changes to be committed; how can I see the diff of all files which are staged for the next commit? I'm aware of git status, but I'd like to see the actual diffs - not just the names of files which are staged.

I saw that the git-diff(1) man page says

git diff [--options] [--] […]

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell git to further add to the index but you still haven't. You can stage these changes by using git-add(1).

Unfortunately, I can't quite make sense of this. There must be some handy one-liner which I could create an alias for, right?

13条回答
何必那么认真
2楼-- · 2019-01-03 00:51

From version 1.7 and later it should be:

git diff --staged
查看更多
对你真心纯属浪费
3楼-- · 2019-01-03 00:51

Think about the gitk tool also , provided with git and very useful to see the changes

查看更多
老娘就宠你
4楼-- · 2019-01-03 00:52

git gui and git-cola are graphical utilities that let you view and manipulate the index. Both include simple visual diffs for staged files, and git-cola can also launch a more sophisticated side-by-side visual diff tool.

See my closely related answer at How to remove a file from the index in git?, and also this official catalog of Git - GUI Clients.

查看更多
仙女界的扛把子
5楼-- · 2019-01-03 00:54

You can use this command.

git diff --cached --name-only

The --cached option of git diff means to get staged files, and the --name-only option means to get only names of the files.

查看更多
Lonely孤独者°
6楼-- · 2019-01-03 00:55

It should just be:

git diff --cached

--cached means show the changes in the cache/index (i.e. staged changes) against the current HEAD. --staged is a synonym for --cached.

--staged and --cached does not point to HEAD, just difference with respect to HEAD. If you cherry pick what to commit using git add --patch (or git add -p), --staged will return what is staged.

查看更多
甜甜的少女心
7楼-- · 2019-01-03 00:55

If you have more than one file with staged changes, it may more practical to use git add -i, then select 6: diff, and finally pick the file(s) you are interested in.

查看更多
登录 后发表回答