Home > OS >  Git undo uncommitted changes to several files (but not all) with one command and with untracked file
Git undo uncommitted changes to several files (but not all) with one command and with untracked file

Time:11-10

Say I had a repo structure with the following unstaged changes:

modified: A/B/C/file1
modified: A/B/C/D/file2
modified: A/B/E/file3

How would I revert changes from only file1 and file2 with a single command? The command git checkout -- A/B/C/* doesn't work because A/B/C also contains several generated files which aren't versioned. The error from git is "error: pathspec 'A/B/C/file4' did not match any file(s) known to git"

CodePudding user response:

The command git checkout -- A/B/C/* doesn't work because A/B/C also contains several generated files which aren't versioned.

That's because you're letting your shell do the glob expansion, so it's looking at all files.

If you instead let git to do its own glob expansion, it will only look at files already under version control:

$ git checkout -- A/B/C/\*

Although frankly I'd prefer to run two commands than to worry about manually deciding which special characters are handled where.

  •  Tags:  
  • git
  • Related