Ever accidentally installed a node module or added a bunch of images and then decided, "I don't want these changes"? Git makes it easy to blow away changes to staged files (
git reset) and to remove files that aren't tracked (
git clean), but doing both to get yourself back to a clean working directory can be tedious. So I wrote a little git alias called
git nuke that looks like this:
alias.nuke !git clean -fd :/ && git reset --hard
Besides the fact that I take a strange pleasure in running
git nuke, it's a useful command because it leaves your repository in a pristine state, regardless of what changes were made to it. Here's what it does:
The bang ("!") at the beginning means "run the following as commands to the shell." Normally, git aliases look like
alias.rename branch -m, meaning that "branch -m" will be substituted in place of "rename" when "git rename" is run. Since we need to run multiple commands, however, we need to run them against the shell directly (git doesn't provide a way to concatenate multiple commands).
As previously noted,
git clean removes untracked files. The
f option is for
force (i.e. don't stop me or prompt or anything . . . just blow them away no matter what) and the
d option is for
directories (for which the help says "remove whole directories"), so these options combined force delete all untracked files and directories. The
:/ means "from the root of the repository." You can run
git clean <path> to only remove untracked files in a particular directory, but in this case, we want to remove everything.
Then we run
git reset --hard to handle undoing changes to any tracked files.
You'll have to add this alias manually to your gitconfig because bash has a hard time with the "!" in the command. So sidebar, first run this:
git config --global alias.edit config --global -e so that you can type
git edit to open your global .gitconfig in your editor. Then add the command above in the section headed with
This command requires due warning. If you run it, there will be virtually - I try never to speak in absolutes with git - virtually no way of recovering your changes. You have been warned.