git checkout all the files

GitGit Checkout

Git Problem Overview


How can I get rid of all the changes in all the files of my repository?

Say I am in a branch and I did some changes. git status returns a set of files in the "Changes not staged for commit" and I notice I would like to get rid of all of these changes in all the files. How can I do this with a single command?

I know I can do the following to checkout just one file:

git checkout -- <file>

I noticed that git checkout -- alone returns the list of all uncommited files. However, I cannot find a way to checkout all of them, something like git checkout --all.

I checked man git checkout and could not find anything. Also I saw https://stackoverflow.com/q/20008025/1983854 and tried git checkout . and did not work either.

Would I have to do it programmatically, by looping through the git checkout -- output?

Git Solutions


Solution 1 - Git

If you are at the root of your working directory, you can do git checkout -- . to check-out all files in the current HEAD and replace your local files.

You can also do git reset --hard to reset your working directory and replace all changes (including the index).

Solution 2 - Git

Other way which I found useful is:

git checkout <wildcard> 

Example:

git checkout *.html

More generally:

git checkout <branch> <filename/wildcard>

Solution 3 - Git

Of course you could use hard reset:

git reset --hard

Additionally I'm using simple method allowing to specify the pattern:

git checkout `git ls-files -m | grep "some pattern"`

So here "some pattern" could be the folder or file name or file extension.

Solution 4 - Git

If you want to checkout all the files 'anywhere'

git checkout -- $(git rev-parse --show-toplevel)

Solution 5 - Git

  • If you are in base directory location of your tracked files then git checkout . will works otherwise it won't work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfedorquiView Question on Stackoverflow
Solution 1 - GitpokeView Answer on Stackoverflow
Solution 2 - GitSinghakView Answer on Stackoverflow
Solution 3 - GitBoris DavidovView Answer on Stackoverflow
Solution 4 - GitRayKimView Answer on Stackoverflow
Solution 5 - GitPrince KumarView Answer on Stackoverflow