3

As far I know git checkout doesn't allow us to checkout branch until we commit all the previous changes but my changes have been discarded.

I executed git status command and it showed me the list of modified files. Then I executed git checkout . (dot) command but it didn't prompt me to first commit my previous changes and discarded all my changes and checked out master branch on my local machine.

Can anyone please guide me that why git checkout . behaved in this way? And how I can move back to my previous code on my local machine (with modified and uncommitted changes)? Why did it discard my changes?

2 Answers 2

6

Unfortunately, git checkout is a command that has several very different meanings, and this has caused your problem: with checkout you can switch branches, create a branch, update your working directory files with a specific version or the HEAD of a branch.

When you do: git checkout <branchname>, you ask git to switch branch, so it will warn you if you have uncommitted changes that you can remove, stash or commit.

When you do: git checkout <pathname>, you ask git to update your files with a version, so you ask git to override potential modifications with what is already in the repository; which is what git did.

Hope it'll help. Sorry for your discarded changes. This is where the simplicity of a UI is important.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your quick reply. but I used just git checkout . means I didn't specify any path as you specified here git checkout <pathname>. Then with which version it will update my files?
. is a pathname; it refers to the current directory. Great answer, BTW.
So, with . it will override my code with one on server without any prompt by discarding my all local changes? Well done GIT
Please ask Linus Torvarld :) in the checkout you specify explicitly the path (file/folder, even .) so why does git need to promt warning? Best practice is using checkout HEAD and don't provide any path.
0

Here is more info to complement Christophe’s answer and help understand what git checkout does:

git-checkout - Checkout a branch or paths to the working tree

branch

git checkout <branch> is used to switch branch by updating the index and the files in the working tree, and by pointing HEAD at the specified branch.

Any changes to the files in the working tree are kept, so that they can later be committed to the <branch>.

pathspec

git checkout <pathspec> updates files in the working with versions from the index (staging area) rather than from another branch.

From the man page:

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>... 

When <paths> 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 <tree-ish> (most often a commit).

If the file changes had been added to the index (aka staging area) using git add, their changes would not have been lost by a git checkout . since this command uses the changes stored in the index to update the specified paths.


This command can be helpful if the changes you made were accidental, e.g., if you deleted a directory in your working directory and then realised that you really needed to keep it, running git checkout <dirname> would restore all the (tracked) files in that directory from the index.

It’s just a pity that it doesn’t warn the user when it updates files and directories changes by irreversibly discarding any changes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.