Basic branch and merge
Git Branch (source: sitepoint.com)
Branch concept
When we create a new repository, a main branch will automatically be created which we can see by running git status
.
> git init
> git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
In this example we see that the main branch that is automatically created after git init
is branch master
.
By creating a new branch while we are in the main branch, we will automatically have all the previous commits in the branch master
, with which we can create various commits that are independent from those in master
, and in the future, we can merge all commits again using the merging concept.
With the ability to separate from the main branch, and later combine it again, we can create a feature individually first before we decide to put it on the main branch.
Creating a New Branch
Before creating a new branch, we try adding a commit first so that later we can study the git log
.
> git add .
> git commit -m "Adding authentication"
[master (root-commit) c0e5bfa] Adding authentication
1 file changed, 1 insertion(+)
create mode 100644 auth.sh
> git log
commit c0e5bfa0259090c4919b5604c86bbd1e5fa45eaa (HEAD -> master)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:14:22 2022 +0700
Adding authentication
Next we will learn how to create a new branch. We do this with the command git checkout -b <branch name>
, where this command will create a new branch, and we automatically checkout
that branch:
> git branch
*master
> git checkout -b fix-authentication-bug
Switched to a new branch 'fix-authentication-bug'
> git branch
* fix-authentication-bug
masters
We see that we have a new branch, which if we compare it to the master
branch, will have the same history as the existing history in master
.
> git log
commit c0e5bfa0259090c4919b5604c86bbd1e5fa45eaa (HEAD -> fix-authentication-bug, master)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:14:22 2022 +0700
Adding authentication
> git log master
commit c0e5bfa0259090c4919b5604c86bbd1e5fa45eaa (HEAD -> fix-authentication-bug, master)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:14:22 2022 +0700
Adding authentication
Now we add a new commit in this branch.
> git add .
> git commit -m "Fixing bug in auth"
> git log
commit c2507da9d997be98873c14095205dbc430078874 (HEAD -> fix-authentication-bug)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:21:46 2022 +0700
Fixed bug in auth
commit c0e5bfa0259090c4919b5604c86bbd1e5fa45eaa (master)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:14:22 2022 +0700
Adding authentication
> git log master
commit c0e5bfa0259090c4919b5604c86bbd1e5fa45eaa (master)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:14:22 2022 +0700
Adding authentication
So it can be seen that fix-authentication-bug
will have one commit different from master
.
Then we can also conclude above that HEAD
is a position that points to the branch we are in now.
How To Move Between Branches & Merging
Merging is the process of bringing together commits from two different branches. A common example is when we want to merge the master
branch with the branch containing the new feature we are working on.
Merging can be done by running git merge <name of branch to be merged>
.
To merge the repository that we have previously worked on above, we need to first enter the branch that we will merge with with git checkout master
, then run git merge fix-authentication-bug
.
> git branch
* fix-authentication-bug
masters
> git checkout master
Switched to branch 'master'
> git merge fix-authentication-bug
Updating c0e5bfa..c2507da
Fast forward
auth.sh | 1+
1 file changed, 1 insertion(+)
> git log
commit c2507da9d997be98873c14095205dbc430078874 (HEAD -> master, fix-authentication-bug)
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:21:46 2022 +0700
Fixed bug in auth
commit c0e5bfa0259090c4919b5604c86bbd1e5fa45eaa
Author: Imam Assidiqqi <[email protected]>
Date: Tue Aug 2 14:14:22 2022 +0700
Adding authentication
So we will see that now the commits that were in fix-authentication-bug
, will also be in master
.
Git Merge Conflict Resolution
The merge process is handled automatically by Git, but there are times when the merge we do fails, this is called Git Merge Conflict. For this reason, we need to check and make changes manually. For more details, let’s watch the following video: