git-fu: Git for the lazy

Jeff Mancuso July 29th, 2008

Getting started with Git? Git for the lazy is one of the better starting points we’ve seen [via SvN].

There are a million Git cheat-sheets out there, but this one does a pretty good job at picking the more intuitive way of performing a particular task, and showing some sample commands to get it done. The How To Fix Mistakes section is particularly useful, and succint:

Haven't committed yet, but don't want to save the changes? 
You can throw them away:   
git reset --hard   

You can also do it for individual files, but it's a bit different:   
git checkout myfile.txt   

Forgot something in your last commit? That's easy to fix.   
git reset --soft HEAD^   

Then write over the last commit:   
git commit --amend   

Don't make a habit of overwriting/changing history if 
it's a public repo you're working with, though.

If I read another post with a laundry list of why git is useful, each bullet linking me to the man page, I’m going to scream. How much time do you think I have?

There is no shortage programmers coming over from Subversion who feel confused and perplexed by all that Git has to offer. My biggest complaint about Git is that there are quite a number of ways to accomplish a specific task, and it’s not always clear that the best practices are.

Tags: , , , , ,

3 Responses to “git-fu: Git for the lazy”

  1. anon Says:

    Umm, I think part of your example is wrong. If you want to change a previous commit, you can do git reset –soft HEAD^ git add filename.c git commit OR as a shorthand (and to preserve the commit message) you can do git add filename.c git commit –amend

    But doing the reset and the –amend will mean that you overwrite not your most recent commit, but actually compress your changes, plus the previous commit into TWO commits ago.

  2. Fred Says:

    You can also do it for individual files, but it’s a bit different: git checkout myfile.txt

    I just tried that and it checks out the entire huge repository. How do you check out just individual files or directories?

  3. Jon Shea Says:
    How do you check out just individual files or directories?

    Fred, git checkout filename should checkout just filename from HEAD (the last commit) of the current branch, so you can use it to revert a file that you’ve messed up. The exception is if filename is also the name of a branch or tag, in which case it will checkout the whole branch or tag. You can get around this by using the command git checkout -- filename. The -- indicates that everything after it should be considered a target rather than an option or flag.

Leave a Reply

Subscribe:

Add to Google
RSS
Try ExpanDrive

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