在提交到 git 之前,如何查看文件中的更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4456532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I see what has changed in a file before committing to git?
提问by Timothy T.
I've noticed that while working on one or two tickets, if I step away, I'm not sure what I worked on, what changed, etcetera.
我注意到在处理一两张票时,如果我离开,我不确定我在做什么,发生了什么变化等等。
Is there a way to see the changes made for a given file before git add and then git commit?
有没有办法在 git add 和 git commit 之前查看对给定文件所做的更改?
回答by Cascabel
You're looking for git diff
. Depending on your exact situation, there are three useful ways to use it:
您正在寻找git diff
. 根据您的具体情况,有三种有用的方法可以使用它:
# show differences between index and working tree
# that is, changes you haven't staged to commit
git diff [filename]
# show differences between current commit and index
# that is, what you're about to commit
# --staged does exactly the same thing, use what you like
git diff --cached [filename]
# show differences between current commit and working tree
git diff HEAD [filename]
It'll work recursively on directories, and if no paths are given, it shows all changes.
它将在目录上递归工作,如果没有给出路径,它会显示所有更改。
回答by elmt
git diff filename
git diff filename
回答by Reed Hedges
For me git add -p
is the most useful way (and intended I think by git developers?) to review all unstaged changes (it shows the diff for each file), choose a good set of changes that ought to go with a commit, then when you have staged all of those, then use git commit
, and repeat for the next commit. Then you can make each commit be a useful or meaningful set of changes even if they took place in various files. I would also suggest creating a new branch for each ticket or similar activity, and switch between them using checkout
(perhaps using git stash
if you don't want to commit before switching), though if you are doing many quick changes this may be a pain. Don't forget to merge often.
对我来说git add -p
是最有用的方法(我认为 git 开发人员打算这样做?)所有未暂存的更改(它显示每个文件的差异),选择一组应该与提交一起进行的好的更改,然后当你有暂存所有这些,然后使用git commit
,并在下一次提交中重复。然后您可以使每次提交成为有用或有意义的一组更改,即使它们发生在不同的文件中。我还建议为每个工单或类似活动创建一个新分支,并在它们之间切换使用checkout
(git stash
如果您不想在切换前提交,则可能使用),但如果您进行许多快速更改,这可能会很痛苦。不要忘记经常合并。
回答by miku
回答by Dustin
Remember, you're committing changes, not files.
请记住,您提交的是更改,而不是文件。
For this reason, it's very rare that I don't use git add -p
(or the magit equivalent) to add my changes.
出于这个原因,我很少使用git add -p
(或等效的 magit)来添加我的更改。
回答by HariKishore
git diff <path>/filename
path can your be complete system path till the file or
if you are in the project you paste the modified file path also
for Modified files with path use :git status
路径可以是文件之前的完整系统路径,或者
如果您在项目中,您也
可以将修改后的文件路径粘贴到带有路径使用的修改后的文件中:git status
回答by Kirby
Well, my case when you don't want to care about files list. Just show them all.
好吧,我的情况是您不想关心文件列表。把它们全部展示出来。
When you already ran git add
with your files list:
当您已经运行git add
文件列表时:
$ git diff --cached $(git diff --cached --name-only)
In more recent versions of git
, you can use --staged
also, which is a synonym of --cached
.
在 的更新版本中git
,您还可以使用--staged
also,它是 的同义词--cached
。
The same can be used for haven't added files but without --cached
option.
同样可以用于没有添加文件但没有--cached
选项。
$ git diff $(git diff --name-only)
Git command alias for "cached" option:
“缓存”选项的 Git 命令别名:
$ git config --global alias.diff-cached '!git diff --cached $(git diff --cached --name-only)'
回答by Yash Bansal
Go to your respective git repo, then run the below command:
转到您各自的 git 存储库,然后运行以下命令:
git diff filename
git diff 文件名
It will open the file with the changes marked, press return/enter key to scroll down the file.
它将打开标有更改的文件,按回车/回车键向下滚动文件。
P.S. filename should include the full path of the file or else you can run without the full file path by going in the respective directory/folder of the file
PS 文件名应包含文件的完整路径,否则您可以通过进入文件的相应目录/文件夹来在没有完整文件路径的情况下运行
回答by AskYous
You can also use a git-friendly text editor. They show colors on the lines that have been modified, another color for added lines, another color for deleted lines, etc.
您还可以使用 git 友好的文本编辑器。它们在已修改的行上显示颜色,添加行的另一种颜色,删除行的另一种颜色等。
A good text editor that does this is GitHub's Atom 1.0.
一个很好的文本编辑器是GitHub 的 Atom 1.0。