Git: How to Do Things

April 27, 2014

Have you ever tried to do something with git but you didn’t know how? Look no further! This is a comprehensive guide to doing things using git.

Be sure to bookmark this page. Studies have shown that it’s completely impossible to remember anything about git ever, so you will definitely need it again.

Revert your changes to a file

To revert to the committed version of file.txt, try using the obvious command:

$ git revert file.txt
fatal: bad revision 'file.txt'

When git prints a complete non-sequitur, fly into a momentary micro-rage directed at nothing in particular. It’s clearly git’s fault, or your shell’s fault, or a cosmic ray.

At this point, it can be helpful to remember that you’re an ape sitting in a chair, pushing small buttons with your fingers and staring straight ahead.

After you’ve calmed down, type a random series of letters and punctuation:

$ git checkout HEAD -- file.txt

Revert all your changes to all the files

Okay, okay, you totally got this one. Just leave off the filename to revert everything:

$ git checkout HEAD
error: pathspec 'HEAD' did not match any file(s) known to git.

Of course not. The problem here is that you have no idea how to use computers anymore; you’re getting old and should probably give up the trade. You can probably find work as an at-home envelope stuffer or a furniture-discount sign-waver.

In case those careers don’t work out, though, you’ll need to remember a completely different git command:

$ git reset --hard

The next step is to realize that your changes are completely gone forever with no backups to speak of. Consider that life is transition and that attachment can only lead to pain.

Send a new branch to the server

One way to do this would be to use a flag on the push command indicating that there’s a new branch:

$ git push --new-branch

But that would clearly be wrong. Instead, you instruct git to set the branch’s upstream remote to the remote you’re pushing to and then, after that, type the name of the branch (this is just for fun, presumably):

$ git push --set-upstream origin why-must-i-endure-this-daily-torment

Tell git not to add a file after all

Say you’ve added a file but then you change your mind. This one should be easy; git ships with 159 built-in commands (not a joke), so there’s probably a command for doing that!

$ git forget file.txt
git: 'forget' is not a git command. See 'git --help'.

The git developers disrespect you. They have been calling you cheap behind your back, writing checks in your name, and spreading rumors about the way you treat other people’s pets. Truly disgusting. They made this flag up to gaslight you:

$ git rm --cached file.txt

Do exactly the thing you meant to do all along, seriously

As an adorable little reminder of the worthlessness of all human toil, git has laid a few fun “traps” for you along the way. A randomly selected subset of git commands work most of the time but silently do the wrong thing just when you need them most: