How can I see what has changed in a file before committing to git?
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?
You're looking for
git diff --staged
Depending on your exact situation, there are three useful ways to use 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]
git diff
works recursively on directories, and if no paths are given, it shows all changes.
Use git-diff
:
git diff -- yourfile
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 diff filename
git diff
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.
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 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
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 diff --cached $(git diff --cached --name-only)
In more recent versions of git
, you can use --staged
also, which is a synonym of --cached
.
The same can be used for haven't added files but without --cached
option.
$ git diff $(git diff --name-only)
Git command alias for "cached" option:
$ git config --global alias.diff-cached '!git diff --cached $(git diff --cached --name-only)'
Go to your respective git repo, then run the below command:
git diff filename
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
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.
A good text editor that does this is GitHub's Atom 1.0.
For some paths, the other answers will return an error of the form fatal: ambiguous argument
.
In these cases diff needs a separator to differentiate filename arguments from commit strings. For example to answer the question asked you'd need to execute:
$ git diff --cached -- <path-to-file>
This will display the changes between the modified files and the last commit.
On the other hand:
git diff --cached HEAD~3 <path-to-file>
will display the changes between the local version of and the version three commits ago.
If you are unsure about the files you made the changes and looking to know the changes before committing to your local branch, use git add -p
which helps you to verify the changes before accepting to add in your local.
Using this query
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? will give you multiple options like y as to Stage the change, n to not to stage the change etc.
Other options: q - quit; do not stage this hunk nor any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk nor any of the later hunks in the file g - select a hunk to go to / - search for a hunk matching the given regex j - leave this hunk undecided, see next undecided hunk J - leave this hunk undecided, see next hunk k - leave this hunk undecided, see previous undecided hunk K - leave this hunk undecided, see previous hunk s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help
Hope it helps..!! Happy Gitting...
精彩评论