Easy Tutorial
❮ Git Rm Git Server ❯

Git Branch Management

Almost every version control system supports branching in some form, where a branch represents an independent line of development.

Using branches means you can diverge from the main line of development and continue to work without affecting that main line.

Git branches are essentially pointers to snapshots of changes.

Some refer to Git's branching model as a killer feature, and it is precisely this that sets Git apart from other version control systems.

Branch creation command:

git branch (branchname)

Branch switching command:

git checkout (branchname)

When you switch branches, Git replaces the contents of your working directory with the snapshot of the last commit on that branch, so multiple branches do not require multiple directories.

Branch merging command:

git merge

You can merge multiple times into the same branch, and you can choose to delete the merged branch immediately after merging.

Before we start, let's create a test directory:

$ mkdir gitdemo
$ cd gitdemo/
$ git init
Initialized empty Git repository...
$ touch README
$ git add README
$ git commit -m 'first commit'
[master (root-commit) 3b58100] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

Git Branch Management

Listing Branches

Basic command to list branches:

git branch

Without parameters, git branch lists your local branches.

$ git branch
* master

This example means we have a branch called master, and it is the current branch.

When you execute git init, Git creates the master branch by default.

If we want to manually create a branch, we can do so by running git branch (branchname).

$ git branch testing
$ git branch
* master
  testing

Now we can see that we have a new branch called testing.

When you create a new branch after the last commit, if there are further commits and then you switch to the testing branch, Git will revert your working directory to the state it was in when you created the branch.

Next, we will demonstrate how to switch branches. We use git checkout (branch) to switch to the branch we want to modify.

$ ls
README
$ echo 'tutorialpro.org' > test.txt
$ git add .
$ git commit -m 'add test.txt'
[master 3e92c19] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ ls
README        test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README

When we switch to the testing branch, the newly added file test.txt is removed. When we switch back to the master branch, it reappears.

$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

We can also use the git checkout -b (branchname) command to create a new branch and immediately switch to it, allowing us to operate within that branch.

$ git checkout -b newtest
Switched to a new branch 'newtest'
$ git rm test.txt 
rm 'test.txt'
$ ls
README
$ touch tutorialpro.php
$ git add .
$ git commit -am 'removed test.txt, added tutorialpro.php'
[newtest c1501a2] removed test.txt, added tutorialpro.php
 2 files changed, 1 deletion(-)
 create mode 100644 tutorialpro.php
 delete mode 100644 test.txt
$ ls
README        tutorialpro.php
$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

As you can see, we created a branch, removed some files (test.txt) on that branch, and added a new file (tutorialpro.php). When we switched back to our main branch, the deleted test.txt file reappeared, and the newly added tutorialpro.php file was not present in the main branch.

Using branches allows us to separate work, enabling us to work in different development environments and switch between them.

Deleting Branches

Branch deletion command:

git branch -d (branchname)

For example, to delete the testing branch:

$ git branch
* master
  testing
$ git branch -d testing
Deleted branch testing (was 85fc7e7).
$ git branch
* master

Branch Merging

Once a branch has its own content, you will eventually want to merge it back into your main branch. You can use the following command to merge any branch into the current branch:

git merge
$ git branch
* master
  newtest
$ ls
README        test.txt
$ git merge newtest
Updating 3e92c19..c1501a2
Fast-forward
 tutorialpro.php | 0
 test.txt   | 1 -
 2 files changed, 1 deletion(-)
 create mode 100644 tutorialpro.php
 delete mode 100644 test.txt
$ ls
README        tutorialpro.php

In this example, we merged the newtest branch into the main branch, and the test.txt file was deleted.

After merging, you can delete the branch:

$ git branch -d newtest
Deleted branch newtest (was c1501a2).

After deletion, only the master branch remains:

$ git branch
* master

Merge Conflicts

Merging is not just about adding and removing files; Git also merges changes.

$ git branch
* master
$ cat tutorialpro.php

First, we create a branch called change_site, switch to it, and change the content of tutorialpro.php to:

<?php
echo 'tutorialpro';
?>

Create the change_site branch:

$ git checkout -b change_site
Switched to a new branch 'change_site'
$ vim tutorialpro.php
$ head -3 tutorialpro.php
<?php
echo 'tutorialpro';
?>
$ git commit -am 'changed the tutorialpro.php'
[change_site 7774248] changed the tutorialpro.php
 1 file changed, 3 insertions(+)

We commit the changes to the change_site branch. Now, if we switch back to the master branch, we can see that the content reverts to what it was before our modifications (an empty file with no code). We modify the tutorialpro.php file again.

$ git checkout master
Switched to branch 'master'
$ cat tutorialpro.php
$ vim tutorialpro.php    # Modify the content as follows
$ cat tutorialpro.php
<?php
echo 1;
?>
$ git diff
diff --git a/tutorialpro.php b/tutorialpro.php
index e69de29..ac60739 100644
--- a/tutorialpro.php
+++ b/tutorialpro.php
@@ -0,0 +1,3 @@
+<?php
+echo 1;
+?>
$ git commit -am 'modified code'
[master c68142b] modified code
 1 file changed, 3 insertions(+)

Now these changes are recorded in my "master" branch. Next, we will merge the "change_site" branch.

$ git merge change_site
Auto-merging tutorialpro.php
CONFLICT (content): Merge conflict in tutorialpro.php
Automatic merge failed; fix conflicts and then commit the result.

$ cat tutorialpro.php     # Open the file to see the conflict content
<?php
<<<<<<< HEAD
echo 1;
=======
echo 'tutorialpro';
>>>>>>> change_site
?>

We merged the previous branch into the master branch, and a merge conflict occurred. Next, we need to manually resolve it.

$ vim tutorialpro.php 
$ cat tutorialpro.php
<?php
echo 1;
echo 'tutorialpro';
?>
$ git diff
diff --cc tutorialpro.php
index ac60739,b63d7d7..0000000
--- a/tutorialpro.php
+++ b/tutorialpro.php
@@@ -1,3 -1,3 +1,4 @@@
  <?php
 +echo 1;
+ echo 'tutorialpro';
  ?>

In Git, we can use git add to tell Git that the file conflict has been resolved.

$ git status -s
UU tutorialpro.php
$ git add tutorialpro.php
$ git status -s
M  tutorialpro.php
$ git commit
[master 88afe0e] Merge branch 'change_site'

Now we have successfully resolved the merge conflict and committed the result.

❮ Git Rm Git Server ❯