So, I'm relatively new to git, and a situation I've run into a few times is that I have a copy of code I want, but for whatever reason, since I've just done a "git checkout" on several different branches of the same codebase, I end up with a handful of files on my filesystem that don't match what's checked into my current HEAD, and so it shows those files as "Changes not staged for commit" which is correct. However, I don't want to keep ANY of these changes (they're safely in some other branch).
The only way I can see to do this is to "git checkout -- " as is mentioned in the "git status" message.
The issue is that with 10-20 files, I have to copy paste the filename 10-20 times, which is inconvenient.
Is there a better way of doing what I'm trying to do? I've tried "git checkout master" and similar commands with no success.
If the files are 'not staged for commit', then they're in the workspace, not in the staging area.
If you want to forget any changes in the workspace, you can run git checkout .
, which will (recursively) put all unstaged files back into an unmodified state.
Running git checkout .
will have no effect on staged changes (i.e. changes that have been marked for commit with git add
). To unstage those changes, you can run git reset HEAD <path>
(where <path>
again can be a single file, a folder, or a number of both).
The following image shows some of the states a file can be in, and how to move files between those states ("index" is the same as "staging area"):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With