Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between git reset file and git checkout file?

Why is it that git allows me to reset a file? I thought I understood reset, in the sense that it was moving the HEAD ... clearly I was wrong.

So, git reset sha file seems to do the same as git checkout sha file, with the exception that I see file in the index and in the working directory.

It doesn't make sense to me. Can someone please explain the difference?

like image 242
Senthess Avatar asked Aug 02 '11 21:08

Senthess


People also ask

What does git reset file do?

Summary. To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

What is the difference between reset and reset git?

git reset --soft , which will keep your files, and stage all changes back automatically. git reset --hard , which will completely destroy any changes and remove them from the local directory. Only use this if you know what you're doing.

What is the difference between git checkout and git branch?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

What is the difference between git reset head header C and git checkout -- header C?

git checkout -- . : copies from index, to work-tree, only. git checkout HEAD -- . : copies from HEAD, to index, and then to work-tree. git reset --mixed : resets index from HEAD (and then leaves work-tree alone) git reset --hard : resets index from HEAD, then resets work-tree from index.


1 Answers

tl;dr git reset COMMIT FILE changes only index, git checkout COMMIT FILE will change both index and working tree.

git reset has very important flags of --soft, --hard and --mixed ( and --keep and --merge )

http://git-scm.com/docs/git-reset

--mixed is the default and when you do git reset sha file you are doing mixed reset whereby:

--mixed

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

Like it says above, the reset in this case would not touch your working tree at all and only the version in the index is reset to the one in the sha.

git checkout on the other hand:

When or --patch are given, git checkout does not switch branches. It updates the named paths in the working tree from the index file or from a named (most often a commit).

So when you do git checkout you will lose the changes in the file and it will be replaced with whatever was there in the version of file in sha, whereas when you do the mixed reset, only your index will be reset and your working directory will still have the changes which you can later stage again as needed.

like image 50
manojlds Avatar answered Oct 02 '22 01:10

manojlds