Skip to content

user byrne git cheat sheet

byrnereese edited this page Feb 7, 2011 · 6 revisions

Git Cheat Sheet

The following git cheat sheet are the commands given to me by our very own git-god resident Jay Allen. He routinely gives me the best tips and tricks to help me better manage my repository and create better code commits.

Feature Branch Script

This is a shell script that helps to create a new feature branch.

#!/usr/bin/env bash
if [[ -z "$*" ]]; then
    echo "Usage: $(basename $0) NEW_BRANCHNAME"
    exit
fi
REMOTE=$(git remote -v | egrep "openmelody/melody.git \(fetch\)" | awk '{print $1}')
git checkout -b "$*" $REMOTE/master
echo -n "Updating branch with latest Melody changes..."
git pull

Note from Jay: You can find this script in the Melody repo at tools/new-branch. Just cd into the Melody directory and type the command. There's some weird formatting above so copy/paste isn't a good idea.

What branches can I delete?

To figure out what branches have been merged into core and are safe to delete I do this:

git branch -r --merged

Sample output:

prompt> git branch -r --merged
   origin/bug-400-revision-histories
   origin/bug-410-refactor-load-order
   origin/bug-446-update-plugins-test

This works with branches that were merged smoothly. Often if a merge is borked, the commit messages need to be rewritten, etc. In which case, Jay will create a branch off of your branch, and then merge that one -- thus losing the trail to your branch. Lesson: follow the contribution instructions!

Note from Jay: That command tells you which branches are ancestors of your current branch. So basically, any branches shown are already contained in your current branch. If your current branch is up to date with openmelody/master, then you can find out what branches have been merged into Melody.

Force my local master to point to openmelody's master

When I want to synchronize my master to be the same as the master I forked from, do this:

prompt> git branch --set-upstream master openmelody/master

Note from Jay: I'm still not convinced this is a good idea. If you're using tools/new-branch consistently, the state of your master branch should not matter because you're never in it.

How to edit an old commit note

Jay shared this great tidbit with me when I failed to provide adequate information in one of my commit notes:

# Checkout the branch you want to edit
git checkout bug-507-change-default-asset-folder

# Interactive rebase - Google it!
git rebase -i bd631e1e # This is commit you created this branch from
# when your editor pops up, look for the line with 
# the commit note you want to edit and change the "pick" at the 
# beginning of the line to "reword". Save and close, then edit the
# commit note in the new window that pops up
# to say what you're changing it to, save and close again

# Get the latest from GitHub and put _your_ changes on top of them
git remote update && git rebase openmelody/master

# Repush the branch forcibly to $repo
git push -f origin bug-507-change-default-asset-folder

Note from Jay: You should be careful with this one. Anytime you rebase (or do git commit --amend) you rewrite your history meaning that the commits you edit and all that follow them are replaced by new copies with different SHA1 IDs. Do it as much as you like on commits that have never been outside of your local repo but don't try to edit commits that you pushed a month ago and have been merged into Melody. You'll end up with a big mess.

That said, I use gir rebase, git rebase -i and git commit --amend every single day and with almost every topic branch. It's a fantastic power tool and like any power tool you should read the manual before using. One last tip: with "git rebase -i" you can not only edit your commit notes but you can squash them together, delete one or more commits or even reorder them.

Splitting a commit up into multiple (with a visual tool)

So you have a commit that is messy. It has lots of different changes, e.g. fixes different bugs, how to create a clean pull request? Split up your changes into smaller more specific commits. You can do that doing this:

git add -i <filename>

Or Jay shared with me this trick using a visual tool Gitx:

Ah, okay. I just wanted to make sure it was code that was supposed to be pushed. BTW, next time you have a file with modifications made in different places, open up GitX in the repo and then hit Command-2 (Commit View). Click on the modified file and check out the line-by-line addition feature. It's the exact same thing you can do using git add -i but so much cooler because it's as easy as clicking on diffs...

Catching up an old branch with the latest

Let's say you started work on a feature a long time ago. You decided to set it aside to work on something else. Time passes and your feature branch becomes stale. Now you want to resume work on that branch. How do you resynchronize your branch with the latest and greatest?

git remote update
git rebase openmelody/master

What you should NOT do is a git pull openmelody master!



Questions, comments, can't find something? Let us know at our community outpost on Get Satisfaction.

Credits

  • Author: Byrne Reese
  • Edited by: Jay Allen
Clone this wiki locally