ChatGPT解决这个技术问题 Extra ChatGPT

git checkout all the files

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?


p
poke

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


are the two options equivalent (if we reset to HEAD) ?
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 Hi Poke, i am sure git checkout -- . doesn't work. I tried it for multiple unstaged changes.
other commands to achieve the same result: git checkout --force, git reset --hard
f
fedorqui

Other way which I found useful is:

git checkout <wildcard> 

Example:

git checkout *.html

More generally:

git checkout <branch> <filename/wildcard>

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 '*'.
git checkout <branch> <filename/wildcard> will move head to branch but put the files in the second argument to the local files state instead of the branch state?
git checkout *.html This wont work for subdirectories on most setups.
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.
B
Boris Davidov

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.


R
RayKim

If you want to checkout all the files 'anywhere'

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

P
Prince Kumar

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