Wednesday, January 9, 2013

Making Mistakes in Git

I wanted to make a couple of changes to an open source library that was based on GitHub. I had made a contribution to another library before, but I didn't really do it properly. This time, I was definitely going to do it right. Here's what I learnt.

Start and End with a Branch

I thought I was being good with my git branch foo, git checkout foo skills. While I was diligent in committing to my local branch, when I was finished I merged it into my master and pushed from there.

Pro tip: git push foo origin after you've finished working in your branch.

Doing this means you can open a shiny pull request with only your change in it.

Creating Remote Branches from Locally Merged Branches

Firstly; give up on your local branch. Seriously -- delete it using git branch -D foo. Also make sure you haven't got an existing remote branch (if you do, get rid of it using git push origin :foo).

Then recreate your local branch, and use the magic git rebase -i HEAD~5 (where 5 means get the last 5 commits). This opens an editor where you can delete the commits you don't want to appear in the branch.

Once you've done this, you can git push foo origin, and you'll have a beautiful remote branch all set up for a pull request.

No comments: