Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between git diff HEAD vs. git diff --staged?

Tags:

git

What is the difference between git diff HEAD and git diff --staged? I tried both but both give the same output.

like image 633
vhd Avatar asked May 15 '13 10:05

vhd


People also ask

What is git diff head?

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.

What is the use of git diff -- staged?

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.

What is another way to call git diff -- staged?

--staged is a synonym of --cached . Stage/cache/index are all synonyms for the staging area.

What is the difference between staged and unstaged changes in git?

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.


1 Answers

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.

like image 159
Carlos Campderrós Avatar answered Oct 20 '22 11:10

Carlos Campderrós