I have a git alias (git undo) that undoes everything in the working directory, including new files, changed files, and deleted files:
!git reset --hard && git ls-files -d | xargs -0 git rm --ignore-unmatch && git clean -fq
On OS X, this works great. On Linux, however, I run into the following issue: if no files have been deleted from the repository, the git ls-files -d | xargs -0 git rm --ignore-unmatch command will fail (xargs will be passed nothing).
Is there a way to have xargs silently move on if it receives nothing from git ls-files?
With a
git reset --hardbefore it, thegit ls-files -dshould never generate any output (and if it did, you would want to usegit ls-files -d -zto have it produce NUL-terminated output forxargs -0).Once you have done
git reset --hard, the tracked portion of the working tree and the whole index will match the HEAD commit.git ls-files -dwill only show files that are in the index but not in the working tree. Since the working tree will have everthing that the index has, there should never be any deleted files after a hard reset.The
git cleanbit is useful to delete untracked files (whichgit reset --hardwill not touch), but you might want to change it togit clean -dfqto also delete wholly untracked directories).