175

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 Git: Checkout all files except one and tried git checkout . and did not work either.

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

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598

5 Answers5

262

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).

poke
  • 369,085
  • 72
  • 557
  • 602
  • are the two options equivalent (if we reset to HEAD) ? – lucidbrot Sep 27 '17 at 06:56
  • 16
    No, there are certain differences. For example `git checkout -- .` will only reset the changes to the state of the index. So if you already have added files to the index, that’s what it will reset to. On the other hand `git reset --hard` will also throw away the index, so for example if you have untracked files added to the index (to start tracking them), they will also be removed. `git checkout` also allows you to only restore the working directory partially by passing a more specific path which can be very useful. – poke Sep 27 '17 at 08:11
  • what is the difference between git checkout . and git checkout -- .? – vinay kumar reddy Oct 06 '17 at 13:25
  • 1
    @vinaykumarreddy [Meaning of Git checkout double dashes](https://stackoverflow.com/q/13321458/216074) – poke Oct 09 '17 at 09:43
  • 4
    @poke Hi Poke, i am sure git checkout -- . doesn't work. I tried it for multiple unstaged changes. – Aman Oct 10 '17 at 06:15
  • found this looking for a way to bypass gits smart checkout, where it won't change a file if the file "hasn't changed" between versions, and while this is normally a nice thing, some of my files were checked out somehow with the wrong line endings, and I wanted to force update the whole index tree, but `git reset --hard` doesn't work for that. Is there something that will? (note my workaround was to checkout my initial commit and then recheckout master) – xenoterracide Dec 21 '17 at 09:07
  • `git checkout .`also works unless you have a branch called `.`, which would be unwise anyway. – Xavier T. May 25 '18 at 13:10
  • 4
    other commands to achieve the same result: `git checkout --force`, `git reset --hard` – tjalling Aug 17 '18 at 11:09
  • There should have been `git checkout -u` like there is `git add -u`. Git is absolutely not consistent! – jaques-sam Feb 14 '19 at 14:34
  • 'git checkout -- . ' did not work for me. 'git checkout --force' did the job though – Dominik Myszkowski Nov 04 '21 at 08:52
13

Other way which I found useful is:

git checkout <wildcard> 

Example:

git checkout *.html

More generally:

git checkout <branch> <filename/wildcard>
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Singhak
  • 8,508
  • 2
  • 31
  • 34
  • 4
    If globbing is enabled on your shell and there are untracked files in the working directory then this will fail unless you escape the asterisk, e.g. `git checkout \*` or `git checkout '*'`. – Josh Cooley Mar 24 '20 at 18:47
  • `git checkout ` will move `head` to `branch` but put the files in the second argument to the local files state instead of the branch state? – Timo May 17 '21 at 17:34
  • `git checkout *.html` This wont work for subdirectories on most setups. – Sk446 May 20 '21 at 10:48
  • The wildcard works different depending on your OS. If you're on Windows with Powershell, `git checkout *` will checkout recursively, but the same cannot be said for a linux machine running BASH. – Kellen Stuart Dec 28 '21 at 16:48
4

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.

Boris Davidov
  • 294
  • 2
  • 6
1
  • If you are in base directory location of your tracked files then git checkout . will works otherwise it won't work
Prince Kumar
  • 331
  • 2
  • 4
0

If you want to checkout all the files 'anywhere'

git checkout -- $(git rev-parse --show-toplevel)
RayKim
  • 423
  • 1
  • 6
  • 22