git-fu: Git for the lazy
July 29th, 2008Getting 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: git, git checkout, git reset, git revert, man, subversion



July 30th, 2008 at 12:05 am
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.
July 2nd, 2009 at 6:30 pm
I just tried that and it checks out the entire huge repository. How do you check out just individual files or directories?
July 3rd, 2009 at 10:30 am
Fred,
git checkout filenameshould checkout justfilenamefrom 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 iffilenameis 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 commandgit checkout -- filename. The--indicates that everything after it should be considered a target rather than an option or flag.