git branch and git merge

2022-07-17 hit count image

Let's see how to create a branch and how to merge the Branch to manage the source code with Git.

Outline

When we manage the source code with Git, we normally manage the last version of source code(Production release version) in the main branch. And then, we create a new branch instead of the main branch to develop a new feature. After developing, we merge it to the main branch.

In this blog post, you’ll see how to use git branch and git merget to create a new branch and merge it. Also, I’ll introduce how to use git log and git diff to compare between branches.

Check branch

You can see the branches on your local(Working directory) by executing the following command.

git branch

When you execute the git branch command, you can see the branch list like the below, and you can see the * mark that means the branch is the current branch(Checkout branch).

* main
(END)

Create branch

You can create a new branch by executing the following command.

git branch BRANCH_NAME

After you create a new branch, you should use git checkout to selec the branch.

git checkout BRANCH_NAME

When you execute the command below, you can create and checkout the branch at the same time.

git checkout -b BRANCH_NAME

Check branch histoy and compare branches

After creating a new branch and modifying it on Git, you can check the history of the branch, or compare between branches.

git log

When you execute the following command, you can see the history of the current branch.

git log

You can see the history of the current branch like the below.

commit 18be2fdd7c312cb834610baff4b9388b9d719dce (HEAD -> develop)
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:53:13 2021 +0900

    Modify example file

commit da360f74c8227716fa6f005808480bef8811c6b8
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:52:07 2021 +0900

    Add a file

You can use the --branches option to see the history of all branches in the local.

git log --branches

You can see the history of all branches like the below.

commit 8c591d99fd58229d6b600a2e9560b21b8e2181be (main)
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:55:42 2021 +0900

    Add a new file

commit 18be2fdd7c312cb834610baff4b9388b9d719dce (HEAD -> develop)
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:53:13 2021 +0900

    Modify example file

commit da360f74c8227716fa6f005808480bef8811c6b8
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:52:07 2021 +0900

    Add a file

You can use the --graph option to see a more understandable history of the branch.

git log --branches --graph

You can see the graph like the below.

* commit 8c591d99fd58229d6b600a2e9560b21b8e2181be (main)
| Author: dev-yakuza <[email protected]>
| Date:   Sat Jul 17 19:55:42 2021 +0900
|
|     Add a new file
|
| * commit 18be2fdd7c312cb834610baff4b9388b9d719dce (HEAD -> develop)
|/  Author: dev-yakuza <[email protected]>
|   Date:   Sat Jul 17 19:53:13 2021 +0900
|
|       Modify example file
|
* commit da360f74c8227716fa6f005808480bef8811c6b8
  Author: dev-yakuza <[email protected]>
  Date:   Sat Jul 17 19:52:07 2021 +0900

      Add a file

Lastly, you can use --oneline option to see the branch and simple message.

git log --branches --graph --oneline

You can see simply the branch name and message like the below.

* 8c591d9 (main) Add a new file
| * 18be2fd (HEAD -> develop) Modify example file
|/
* da360f7 Add a file

git log to compare branches

When you execute the following command, you can see the difference between the current branch and the target branch.

git log main..BRANCH_NAME

You can see the difference between the main branch and the target branch like the below.

commit 8c591d99fd58229d6b600a2e9560b21b8e2181be (main)
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:55:42 2021 +0900

    Add a new file

commit 18be2fdd7c312cb834610baff4b9388b9d719dce (HEAD -> develop)
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:53:13 2021 +0900

    Modify example file

You can use the -p option to see the modification.

git log -p main..BRANCH_NAME

You can see the modification like the below.

commit 18be2fdd7c312cb834610baff4b9388b9d719dce (HEAD -> develop)
Author: dev-yakuza <[email protected]>
Date:   Sat Jul 17 19:53:13 2021 +0900

    Modify example file

diff --git a/example.txt b/example.txt
index d00491f..1191247 100644
--- a/example.txt
+++ b/example.txt
@@ -1 +1,2 @@
 1
+2

git diff to compare branches

You can use the git diff command to see the difference between the current branch and the target branch more simply.

git diff main..BRANCH_NAME

You can see the file name and modification like the below.

diff --git a/example b/example
deleted file mode 100644
index d00491f..0000000
--- a/example
+++ /dev/null
@@ -1 +0,0 @@
-1
diff --git a/example.txt b/example.txt
index d00491f..1191247 100644
--- a/example.txt
+++ b/example.txt
@@ -1 +1,2 @@
 1
+2
diff --git a/example2.txt b/example2.txt
deleted file mode 100644
index e69de29..0000000

git merge

After creating a new branch and developing a new feature, you want to merge the branch to the main branch(Production release) to launch the new feature to the users.

So, let’ see how to merge the BRANCH_NAME, which includes a new feature, to the main branch. To merge the BRANCH_NAME to the main branch, fisrt, you need to checkout the main branch.

git checkout main

And then, execute the git merge command to merge the branch that you want to merget to the main branch.

git merge BRANCH_NAME

If the branch is merged well, you can see the message like the below.

Merge made by the 'recursive' strategy.
 example.txt | 1 +
 1 file changed, 1 insertion(+)

Lastly, execute the command below to delete the merged branch.

git branch -d BRANCH_NAME

In here we’ve used the -d option to delete the merged branch. If you want to delete not merged branch, you can use the -D option.

git branch -D BRANCH_NAME

Conflict

When you use git merge to merge the branch, if someone modified the same file of yours, you can see the conflict like the below.

Auto-merging example2.txt
CONFLICT (content): Merge conflict in example2.txt
Automatic merge failed; fix conflicts and then commit the result.

You can see the files, that can’t be merged because of the conflict, when you execute the git status command.

On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
  both modified:   example2.txt

no changes added to commit (use "git add" and/or "git commit -a")

When you open the file, you can see the conflict contents between <<<<<<< HEAD, ======= and >>>>>>> develop.

# vi example2.txt
<<<<<<< HEAD
main
=======
develop
>>>>>>> develop

HEAD means the current branch. In other words, the branch is that you execute the git merge command(main). >>>>>>> develop is the target branch of the git merge command(BRANCH_NAME when you execute git merge BRANCH_NAME).

We should check the contents and modify or delete the contents. In here, I’ll keep the HEAD contetns.

main

After modifying, execute the command below to commit the modified contents.

git commit

You can see the auto-generated message like the below.

Merge branch 'develop' into main

# Conflicts:
#       example2.txt
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# All conflicts fixed but you are still merging.

Just, enter :wq key to save the commit message, and then you can see the merging is succeeded.

Completed

Done! we’ve seen how to create a new branch in Git, and how to merge the branch. Now, you can make various branch strategies to develop your project.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts