Bilal Ahmed Yaseen
Bilal Ahmed Yaseen

Reputation: 2654

Git discarded uncommitted changes after checkout

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?

Upvotes: 3

Views: 1397

Answers (2)

Anthony Geoghegan
Anthony Geoghegan

Reputation: 11983

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.

Upvotes: 0

Christophe Muller
Christophe Muller

Reputation: 5090

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.

Upvotes: 6

Related Questions