git-fu: The first post
Wednesday, July 2nd, 2008As you’re no doubt aware, a lot of talented, high profile programmers have announced that they’re moving to git for version control. Many of these programmers (for example, yours truly) have promised to post their impressions, their tips and tricks, their how-tos, and their workflows and the internet for the betterment of humanity.
You may have wondered why so few people have followed through with this promise. I’ll tell you: it’s because we still have no clue what we’re doing. We still pour over the cheat sheet and man pages every time we need to do something even remotely outside of our standard workflow (pull, commit -a, push). And when I say “we”, I don’t just mean those of us at Magnetk. Git is fundamentally different than subversion. It’s taking us some time to wrap our heads around it, and we’re all a little gun shy.
Well, not me. I’m not afraid of making mistakes and looking like an idiot.
Say you add git add a file or directory, and then, before you commit, decide “No, no. I did not want to add that file. I’ve changed my mind. Please do not add that file to the repository. I do not even what my coworkers to know that I thought about adding that file to the repository.” What do you do? git reset -- RegretableAdd.tex. The file will still be on disk, but it won’t get added to the repository when you commit.
If you delete or modify a file, and then decide that you want to get it back the way it was in the last commit, you can just do a git checkout fileToBeRestored.sty, and you’ll get it back. But what happens if you git rm it instead?
$ git rm DO_NOT_DELETE
$ # "I did not mean to do that. That was a mistake"
$ git checkout DO_NOT_DELETE
error: pathspec 'DO_NOT_DELETE' did not match any file(s) known to git.
Did you forget to 'git add'?
Probably not what you were hoping for, right? Can you get back DO_NOT_DELETE? In git, you bet you can.
$ git rm DO_NOT_DELETE
$ # "I did not mean to do that. That was a mistake"
$ git reset -- DO_NOT_DELETE
todo.rtf: needs update
$ git checkout DO_NOT_DELETE
And that’s all there is to it. More to come. The git-reset man page, unlike some of the other git man pages, is extremely readable. It contains sample workflows for such situations as “Undo a commit and redo”, “Undo a commit, making it a topic branch”, or “Undo a merge or pull”.
Corrections and additions are welcome.


