Create and merge git branches

Following the philisophy of git, branching is an essential and much used part of the development procedure. When a new feature is developed or a bigger change is to be implemented, a branch is created to work on and later when the work is complete, it can be merged back to the original branch. The following will be a short introduction to creating and merging a branch with git.

Create a git branch

When a new branch is created, this new branch is based on the current state of the currently selected branch. This means when a new branch is created while being in the master branch, the newly created branch will be based on the master branch in its current state. To switch to the branch that the new branch should be based on use the “checkout” command.

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

The above command switches the git repository to the master branch. This will be used as the starting point for the new branch. To create a new branch named “feature_A”, the “branch” command can be used.

$ git branch feature_A

If run without a branch name, the “branch” command will only show a list of local branches. After executing the above command, the branch list shows two branches. The currently active branch “master” is marked with an asterisk in the branch list.

$ git branch
  feature_A
* master

To switch to the newly created branch, use the “checkout” command again followed by the new branch name.

$ git checkout feature_A
Switched to branch 'feature_A'

With the new branch created, changes can be made to the “feature_A” branch keeping the “master” branch unchanged. Any changes can be commited as usual to the new branch.

Merge git branches

To merge the branch “feature_A” back to the “master” branch after some changes where commited, the “merge” command can be used. Before starting the merge command, switch to the destination branch, which in this case will be the “master” branch. To switch to the destination branch “master”, execute the following.

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

After switching back to the master branch, your already commited changes to the “feature_A” branch will remain in the “feature_A” branch and the local working copy (the files you see in your file explorer/terminal) will be changed to reflect the state of the master branch.

The following command will start the merge procedure from the source-branch “feature_A”, as specified by the argument, to the current branch “master”.

$ git merge feature_A
Auto-merging changed_file.txt
Merge made by the 'recursive' strategy.
 changed_file.txt | 6 ++++++
 1 file changed, 6 insertions(+)

The result of the merge operation will be a new commit in the destination branch (master) with the details of the merge. Git will ask for the commit message for the merge (by default git suggests per default a commit message similar to this).

Merge branch 'feature_A' into master

Read more of my posts on my blog at https://blog.tinned-software.net/.

This entry was posted in Version control system and tagged , , . Bookmark the permalink.