Learning Git branches

Written by
Date: 2013-01-28 20:44:00 00:00

Working with Git branches

I am using Git to version control this blog. As I am the only one contributor to the blog, I really do not need branches at all. But because I want to learn a little bit more about Git, I have decided to use branches for writing draft posts.

Of course Git branches are great if I were making big changes to my site, like for example, changing the whole template.

I work on three computers, my PC at work, my PC at home, and sometimes my laptop, so I have a central bare repository where my blog is hosted. When I decide to work on a new post, I just go to my working directory do a git pull create the post, and then git push. So I am basically using git as Dropbox, but I trust Git more than Dropbox.

I decided to change my work flow a little, and now what I will do is:

Create a new branch for drafts

This is done just once (I think).

git checkout -b drafts

I can start a draft, or more in this branch, If I am not planning to publish all drafts together is easier to have a branch per draft. I can use git cherry-pick to merge only some commits to the master branch. But using one branch per draft seems easier.

When I think I am ready I issue a:

jekyll --server

And see how it goes. Then, after leaving my PC I may need to do a:

git push origin drafts

To update my drafts branch with the central repository, this just once too. Next time a simple git push will update all branches that exists in local and remote. Because git push normal uploads all branches that exist with the same names in your local repo as well as in the master repo.

Once the draft is finished I can move to master and merge or rebase the draft branch into it.

Merging the drafts branch into master

git checkout master

git merge drafts

And I am ready to publish to the server.

Rebasing the drafts branch into master

git checkput master

git rebase drafts

I am not going to use rebase, as it seems to be safe. But only because I am the only one working on this project. As a rule of thumb "You should not use rebase if you shared your branch with some others".

Delete the drafts branch

Now that all my work is published and integrated in the master branch, I can safely delete the drafts branch.

Delete the branch on the central repository

Maybe this is not needed, as I can re-use the same branch for future drafts, but in case I do not need a branch anymore, I can delete it from the central bare repository with:

git push origin :drafts

Delete the branch locally

Once the branch is deleted from the master repo, I can safely delete it from my local repository too, with:

git branch -d drafts

Summary

As I still do not master git (maybe I never will) I am going to write down here a summary of the commands needed to work with branches.

  • Create a local branch

    git checkout -b newbranch

  • Create the same branch on the master repo, or upload it to master

    git push origin newbranch

  • Download/pull the new branch from master to another local repository

    git pull newbranch origin/newbranch

  • Merge changes on branch to master

    git checkout master

    git merge newbranch

  • Rebase change on branch to master

    git checkout master

    git rebase newbranch

  • Delete branch on master or remote repository

    git push origin :newbranch

  • Delete branch in local repository

    git branch -d newbranch

I think that is all I may need to work with git branch, I hope it may help someone else too.