Git discard uncommitted changes after checkout

As far as I know, git checkout

prevents us from checking out the branch until we have committed all previous changes, but my changes have been rolled back.

I ran the command git status

and showed me a list of changed files. Then I ran the git checkout .

(dot) command , but I was not prompted to migrate my previous changes first and discard all my changes and checkout the master branch on my local machine.

Can someone explain to me why it git checkout .

behaved this way? And how can I go back to my previous code on my local machine (with modified and uncommitted changes)? Why did this cancel my changes?

+3


source to share


2 answers


Unfortunately, git checkout

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

When you run:, git checkout <branchname>

you ask git to switch branch so that it warns you if you have uncommitted changes that you can delete, commit, or commit.



When you run:, git checkout <pathname>

you are asking git to update its files with the version, so ask git to override potential changes with what is already in the repository; this is what git did.

Hope this helps. Sorry for your discarded changes. This is where the simplicity of the user interface is important.

+5


source


Here's some additional information to complement Christophes' answer and help you understand what it does git checkout

:

git-checkout

- Checkout the branch or paths to the working tree

branch

git checkout <branch>

is used to switch a branch by updating the index and files in the working tree and by specifying HEAD on the specified branch.

Any changes made to files in the working tree are saved, so they can later be linked to <branch>

.

pathspec

git checkout <pathspec>

updates files when working with versions from the index (staging area) and not from another branch.



From the man page:

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

      

If <paths>

or is given --patch

, git checkout

branches are not switched. It updates the named paths in the working tree from the index of the file or from the name <tree-ish>

(most commonly a commit).

If the file changes were added to the index (aka staging area) using git add

, their changes would not be lost with using git checkout .

, as this command uses the changes stored in index

to update the specified paths.


This command can be useful if the changes you made were accidental, for example if you deleted a directory in your working directory and then realized that you really need to maintain it, running it git checkout <dirname>

will restore all files (tracked) in that directory from the index.

It's just a shame that it doesn't alert the user when it updates changes to files and directories, irreversibly discarding any changes.

0


source







All Articles