0

How do you get rid of untracked patch files in git? I don't understand exactly how to get myself out of this state. The commands that were run were:

git format-patch 'some-commit-hash'^..'some-commit-hash'

git am *.patch

And while that didn't exactly do what I was intending, I am stuck with various untracked .patch files. I've tried doing a hard reset to the branch, aborting the format-patch. Swapping to other branches. I'm at a loss here.

Jose
  • 4,880
  • 8
  • 27
  • 49

1 Answers1

1

Indeed, git reset --hard SHA1 won't remove the untracked files. It will just reset the HEAD pointer to the SHA1/commit/branch specified, and overwrite the index and the working directory accordingly (changing there only the tracked files).

If you want to "clean up" your working directory in an automatic - but somewhat aggressive way, beware that the deletion of files cannot be canceled - a possible solution is to run:

git clean -d -x -n
# dry-run to inspect the untracked and ignored files
git clean -d -x -f
# to remove all these files

For more information on these commands, you can see e.g. the doc of git clean as well as the "Discussion" section of the doc of git reset.

Another solution if you want more flexibility, is to rely on git ls-files to display the list of such files, and possibly chain this with a bash pipe. Below is one such example, which is inspired by this answer to "How can I see list of ignored files in git?":

git ls-files --exclude-standard --ignored --others | grep '\.patch$'
# to display the ignored or untracked files with .patch file-extension
git ls-files --exclude-standard --ignored --others | grep '\.patch$' | xargs rm -v
# to remove these files
ErikMD
  • 13,377
  • 3
  • 35
  • 71
  • Just a note here. Never use `git clean` unless you really mean it. It will delete files outside of git and there's no going back... – Jose Mar 28 '18 at 22:09
  • @Jose indeed: doing `git clean -d -x -f` will delete all untracked files and this action cannot be canceled. I had already mentioned in my answer that `git clean` acts in a "somewhat aggressive way" and recommended to do a dry-run beforehand via `git clean -d -x -n`. But following your comment and to keep on the safe side, I've added an extra comment about this. – ErikMD Mar 29 '18 at 13:34