Buttle Butkus
Buttle Butkus

Reputation: 9476

git checkout -- * and skip untracked files

I'm trying to restore files after a colossal screw up on my part.

git checkout -- * is working great so far. Except that, for example, in my .gitignore file I have "LICENSE.txt" listed, and it apparently applies not only to the root level file of that name, but every file in the whole directory tree with that name.

So when I run git checkout -- * in a wordpress folder, it fails with error:

error: pathspec 'blog/license.txt' did not match any file(s) known to git

How can I run the command so that it only applies to tracked files? My other option is to go through every folder and restore files one-by-one.

Also note, license.txt is not my only ignored file problem. There are dozens.

Upvotes: 4

Views: 8816

Answers (1)

Kent Shikama
Kent Shikama

Reputation: 4060

If I understand your question correctly, you want to reset all files that are currently tracked to the previous commit and leave the untracked files alone.

If so, then I would do as follows.

First way

As mentioned by Porges in the comments.

First unstage all your currently modified files:

git reset

Then reset all unstaged modified files to the previous commit.

git checkout -- .

Second way (Git 1.7.7+ only)

First I would stash the tracked files as follows:

git stash

Then I would stash the untracked files as follows:

git stash -u

Hence, now you have two stashes on your stack: one with tracked files on the bottom and one with untracked files on the top. Pop off the tracked files as follows (aka apply the stash that is second in the stack):

git stash apply stash@{1}

Then reset to the previous commit:

git reset --hard

Finally, apply the untracked files:

git stash apply

Upvotes: 12

Related Questions