git-fu: The first post

Jon Shea July 2nd

As 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.

Tags: ,

2 Responses to “git-fu: The first post”

  1. Todd Vierling Says:

    The “rm…oops!” issue is not unique to git.

    Subversion requires a “svn revert” where the “git reset” line is in the example here, although a subsequent “svn update” isn’t required (the “revert” implicitly revives the deleted file). So it’s just a command syntax difference.

    CVS is weirder; undoing a “cvs remove” means editing the CVS/Entries file to remove the whiteout line, followed by “cvs update”. (Or nuking the entire directory and then “cvs update -d” from one level up.)

  2. Kevin Old Says:

    Ah, this is very true. I’m a longtime CVS/Subversion user and a few months ago switched to Git. While I love it and fanatical about it, I’ve found myself in that same situation where what I thought would work didn’t and it sent me to the man pages and googling for a few mins before I found the “git way” to do it. I like you hope to be able to post solutions as I discover Git.

Leave a Reply

Subscribe:

Add to Google
RSS
Try ExpanDrive

If you’ve heard of SSH then you need ExpanDrive.