Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout -- * and skip untracked files

Tags:

git

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.

like image 624
Buttle Butkus Avatar asked Dec 18 '22 21:12

Buttle Butkus


1 Answers

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
like image 129
Kent Shikama Avatar answered Jan 01 '23 19:01

Kent Shikama