Branching and Merging in Git
Git is a popular source control system (software which tracks changes to code so you can revert to older versions), and branching is one of its most useful features. In this article, I'll cover the basics of creating and using branches in git, and try to explain why it's so useful! (Note: you'll probably need to be familiar with the basics of git to get the most out of this article).
This will save you from having to edit your path manually, and means you'll be able to run git from the command line.
Create a repository
In this example, I'll create a new folder from the command line to contain the files in my project and initialise a git repository to store changes. On Windows, open a command prompt (I prefer using PowerShell, which is a built-in part of Windows 7) and create a new folder called myproject, then initialise the git repository:
mkdir myproject #create new folder cd myproject #move into folder git init #set up git
Create some files, add text to them and save them, then open up the command line again and commit them to the git repo:
[.. create files etc ..] git add . #Adds files to the repo git commit -m "Commit message" #A message to remind you what changed in this commit
Now we have some changes saved in the repository, we can start creating new branches.
Branches are useful for working on new features without worrying about modifying the existing (working) code. The general workflow would be something like:
- The existing website code is in the 'master' branch;
- You want to write code for a new feature without changing the existing code, so you create a 'development' branch;
- Once the 'development' code is ready, merge the two so the website has the new feature.
Creating a new branch to work on new features is important since it means:
- You'll always have code which works in the 'master' branch, even if you haven't finished the new feature;
- Multiple people can work on different things (on different branches) without overwriting each other's work.
To create a new branch, run the following command:
git checkout -b development
This creates a new branch called 'development' and switches to it - you can modify and commit files to this new branch without affecting the master branch. You won't be able to see the old files in the filesystem - git tracks the changes you make then restores the files to the state they were in when you created the new branch.
You can see which branch you're currently on, as well as all existing branches, by typing
which would result in:
* development master
These are all the branches currently set up in the repository, with an asterisk marking the active branch (the one to which you're currently commiting changes). To switch between branches, use:
git checkout master
This will switch you to the 'master' branch, with all files back in their pristine (working) state; any changes you made on the development branch will no longer appear in the filesystem until you switch back to the 'development' branch. The advantage of using branches to develop on should be clear - you always have a working copy of the code in your 'master' branch which you can revert to at any time, while on other branches you can change or add code and not have to worry about whether you've broken everything!
After you've written your new feature (and made sure it works), you'll need to add your new code to the main (master) branch. Switch to the main branch, then run the 'merge' command:
git checkout master #change to main branch git merge development #merge changes from 'development' into the main branch
Git merge will attempt to update all the files in the master branch with the changes made in the development branch; if no changes have been made to the master branch while you were working on the development branch, then this should be automatic. If the master branch has been updated, though, git will complain about conflicts which will need to be sorted out before the branches can be merged.
To resolve conflicts, you'll need to install or set up a merge tool (a programme which will show any conflicting changes between branches). I like kdiff3:
While it looks confusing, it's fairly easy to get to grips with: the image above shows a file which has been changed in two different branches (the middle top and middle right boxes), and also shows the file before it had been edited (top left box). You can then choose which change you want to keep.
Once you've installed a merge tool, run
git config --global merge.tool kdiff3 #where kdiff3 is the name of the diff tool
to tell git which programme to use to resolve conflicts, then run the programme using
This will launch the tool - in this case kdiff3 - where you can choose which edits you want to keep. This is the tricky part - you have to decide which version of each conflict is correct, and make sure that it won't break anything!
Once you're happy that you've fixed all the conflicts, save the file (Ctrl+S) and close kdiff3 to return to the command line, which will complete the merge.