Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a git commit and the working directory?

Tags:

git-diff

The git-diff manual pages says that git diff is used to

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.

But what about if you want to show the difference between a commit prior to HEAD and the working directory? Is that possible?

like image 760
HelloGoodbye Avatar asked Aug 08 '13 11:08

HelloGoodbye


People also ask

Which command is used to show difference between working directory and last commit?

The git diff command shows the differences between the files in two commits or between your current repository and a previous commit.

What is the working directory in git?

You can access the commit history with the Git log. The working tree, or working directory, consists of files that you are currently working on. You can think of a working tree as a file system where you can view and modify files. The index, or staging area, is where commits are prepared.

What is the difference between git commit?

git commit -- takes the commit message from the given file. In the parameter you should enter the name of the file you want from your repository. git commit --only is the default mode of operation of git commit.

What is the git command used to shows the difference between the working directory and the index?

The git diff command is used when you want to see differences between any two trees.


Video Answer


2 Answers

Yes, but it depends a bit on your definition on what the “current project state” is. The manual for git diff goes on.

If you are interested in comparing with the staged changes:

git diff [--options] --cached [<commit>] [--] [<path>...] 

This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. If HEAD does not exist (e.g. unborned branches) and <commit> is not given, it shows all staged changes. --staged is a synonym of --cached.

If you are interested in comparing with the unstaged changes:

git diff [--options] <commit> [--] [<path>...] 

This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

Or if you are just interested in comparing any two commits (one could be HEAD):

git diff [--options] <commit> <commit> [--] [<path>...] 

This is to view the changes between two arbitrary <commit>s.

So you might want to run git diff someOldCommit HEAD to see the differences between someOldCommit and the current HEAD.

like image 181
poke Avatar answered Sep 20 '22 13:09

poke


It ist just simple:

git diff HEAD 

Explanation: Current changes in the working directory compared with the last commit.

like image 26
Lonely Avatar answered Sep 19 '22 13:09

Lonely