What is the difference between git diff HEAD
and git diff --staged
? I tried both but both give the same output.
The git diff HEAD [filename] command allows you to compare the file version in your working directory with the file version last committed in your remote repository. The HEAD in the git command refers to the remote repository.
The git diff command allows us to track the changes that are staged but not committed. We can track the changes in the staging area. To check the already staged changes, use the --staged option along with git diff command.
--staged is a synonym of --cached . Stage/cache/index are all synonyms for the staging area.
Staged changes are a lot like unstaged changes, except that they've been marked to be committed the next time you run git commit . Upon your next commit, your staged changes become part of your Git history. git status will no longer list them as changes since they're part of your last commit now.
Suppose this output for git status
:
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: y # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: x #
As you see, there is one file modified but not staged for commit, and a new file added that is ready to be committed.
git diff --staged
will only show changes to files in the "staged" area.
git diff HEAD
will show all changes to tracked files. If you have all changes staged for commit, then both commands will output the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With