What is Git branching and how do I do branch?

Branching is one of the fundamental functions of Git. It allows code to be developed in a safe way (in terms of updating/editing/deleting) and allows more than one developer to work on different features at the same time without interfering with each other.

If we think of Git like a set of train tracks then it becomes easier to understand how it works. Each time you commit code to a branch it’s like making a station along a track. At any point another track can split from the current track and create it’s own set of stations and at any point a track can rejoin with another. Mostly the re-join is seamless and goes without a hitch, but on occasion it may be more complicated and require a human touch just to get things smoothed out. A branch does’t even need to ever rejoin. Now imagine that we have a branch that something goes horribly wrong on, or that we start heading off in one direction, only to find that the direction has changed completely. We can just stop developing and hop back onto a main line branch like master and branch again and start working in the right direction. Git acts like a map of the railways and keeps a log of all branches and all stations you are tracking so you can jump to any that you want at any time, you can even go back in time (just in case you suddenly realise that code you worked on a week ago might be useful).

How do I create a Git branch?

A git branch is always created of the current branch you are on. You should make a practice of always branching off master so that everything uses the latest working code to start from. Firstly we need to make sure we have the latest copy of the master branch. If you have any changes that are not committed at this point you should commit them to your branch. You should never have changes on master to commit because WE NEVER WORK DIRECTLY ON MASTER!

To check your current branch type:

git branch

That should come back saying ‘master’. If it’s not master then commit current changes and:

git checkout master
git pull

Once we have that up to date code it’s a simple matter of making a new branch. It’s a good idea to group your branches by prefixing them with the same thing. Git branches can take a slash in the name so a folder like structure is the best way go.

git branch feature/super-cool-things

Now we have created the branch we need to switch to it:

git checkout feature/super-cool-things

There is a shortcut you can use that creates and then checks out the branch

git checkout -b feature/another-feature

This branch exists entirely on your machine at this point. To make it accessible to other developers you need to add it to the remote git server commonly known as ‘origin’.

git push -u origin feature/super-cool-things

Now you can work on your files and commit them as needed, then push the branch to the origin again. When you do a git push its advisable to specify the branch that should be pushed in case other branches you are working on are not ready to be shared yet.

git add that-new-file.css
git commit -am ‘added file for demo purposes'
git push origin feature/super-cool-things

For reference if you want to push all local branches that have a remote counterpart you can simply do:

git push

One other thing to not when pushing a branch is that you can only push when your local branch is ahead (that is has a more recent commit) that the origin branch. If you get a message about it being rejected then you need to do a git pull first and then retry the git push.

How do I get a remote git branch?

Once a branch has been pushed to origin it’s available for other developers to pull to their machines.

Note: While its recommended to work on your own branches, and then merge with a common or master branch, sometimes its necessary to work on a branch someone else has been working on – e.g. they are off ill or on holiday or have priorities on another project.

To start with it’s a good idea to get a list of all remote branches that are available:

git branch -a

remotes/origin/HEAD -> origin/master
remotes/origin/article_changes
remotes/origin/auto_url
remotes/origin/diff_test_branch

We can see a list of any remote branches available so we know what we can checkout. Then it’s simply a matter of checking it out:

git fetch
git checkout feature/super-cool-things

And there you have it. You should now be able to create a repo and commit files and changes as well as creating branches and pushing it all to the origin so other developers can work on your projects with you.

Published on

Discuss!

comments powered by Disqus