10 Useful git commands
We have been using git for months now and I still learn something new about it almost every week. Here are some of the more useful commands that I have come across. Have some good ones yourself? Add them to the comments on the digg story.
git commit --amend- Allows you to change the very last commit in your history. You can change just the message itself (great for typos) or you can use
git add file.txtthengit commit --amendand add it to the commit. git pull --rebase- This command will save any commits you might have in a temporary place,grab any new ones you might be missing form your tracking branch and stick yours back on the end. Keep the history clean and say goodbye to those merge commits.
git checkout HEAD~3 path/to/file.txt- Don't like the last 3 changes on a given file? This will allow you to get a file back exactly as it was at a given point in time.
git cherry-pick 3c1f6a472- You can grab just a specific changeset and apply it to your history, no need to merge the entire branch. But be careful, it changes the commit hash.
git stash- Not ready to commit yet but need to change gears and make a quick change for someone else? Save your working copy in a temporary place then use
git stash popto get it back. git reset- Have some files ready to be committed then you decide you're not ready yet?
git resetwill remove all the changes from the 'ready to be committed' state but leave them intact in your working copy. git reset HEAD^- Will remove the last commit from your history but keep the changes in your working copy.
git reset --hard HEAD^- Like the command above, this will remove the last commit in your history, but it will also wipe away the changes in your working copy.
git merge --squash- Have a bunch of work/changesets on a branch and are ready to merge it to master? Use
git merge --squashto take all those changesets and merge them onto master in a 'ready to be committed' state. Then you can commit them all as a single changeset and keep the history nice and clean. git rebase -i HEAD~3- Have three commits that really should be one? This command will let you combine multiple commits into one or even split a single commit into multiple.
If you'd like to use git in a world class engineering environment, check out jobs.digg.com