Question

I had an untracked file which was not appearing in a git diff and when I added it to the 'changes to be committed' area, it still doesn't show up in the git diff. I shows up with a git status -v when I do a diff against HEAD.

I'm still very new to git, so could anyone please tell me if the file will be committed even if it doesn't show up in a regular diff, as it has been added to the staging area?

Était-ce utile?

La solution

If you'd like to see the staged changes in a diff, you can still use git diff, you just need to pass the --staged flag:

david@pav:~/dummy_repo$ echo "Hello, world" > hello.txt
david@pav:~/dummy_repo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   hello.txt
nothing added to commit but untracked files present
david@pav:~/dummy_repo$ git add hello.txt
david@pav:~/dummy_repo$ git diff
david@pav:~/dummy_repo$ git diff --staged
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..76d5293
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello, world

If you only care about which files are staged, you can of course do a git status, but git diff --staged --name-only will give each staged filename on its own line.

Autres conseils

To see all the changes in the working tree since your last commit including new files use:

git diff HEAD

See more examples here: git-diff

As you've seen. it shows in git status, so yes it will be committed when you git commit it.

You may find it helpful to use a visual git tool such as gitx (Mac) or gitg (Linux),

It has nice panels to show you staged, not staged, etc.

enter image description here

My screenshot seems spare as I currently have no changes in that status.

If you typed in "git add __", the file will be included in your next commit. This commit will be pushed to the server when you run "git push".

I'd like to add to David Cain's top answer the following nuance: the command...

git diff --staged

...shows only the staged changes (as @rogerdpack pointed out).

Now, If you're only after generating the diff of a single file then I have nothing to add. But if what you're after is to generate a diff file with "all changes since the latest HEAD", then you probably should (1.) stage all files with changes, (2.) run the diff, (3.) unstage all files, as follows:

git add -A
git diff --staged HEAD
git reset

Needless to say, bear in mind that these commands will mess around with all your previously staged and unstaged files, so you should exercise caution if you have lots of temporary files lying around. See the git diff's manual page for more info.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top