Let’s say I want to contribute to a project on github. The project repository is at wp-cli/wp-cli. First I fork it, and then clone the resulting repository, scribu/wp-cli:

git clone --recursive [email protected]:scribu/wp-cli.git
cd wp-cli

Now, I make some commits to master, push them to my fork and open a pull request. Piece of cake:

git commit -m "awesome new feature"
git push

But, what happens if my pull request is rejected or only certain commits are accepted? I’m left with a dirty master branch. Oh noes!

There are two ways of solving this:

A. Delete my fork and create it again via the github interface. Can’t get any easier than that.

B. Use git checkout -B:

git remote add upstream git://github.com/wp-cli/wp-cli.git
git fetch upstream
git branch backup
git checkout upstream/master -B master
git push --force

If I made a mistake, I can rescue my commits by calling git checkout backup.

PS: As Adam Backstrom pointed out in the comments, you can avoid this problem entirely by pushing your commits to some other branch, which you can simply delete after the pull request is merged:

git checkout upstream/master -b my-feature