[GitHub] Branching and Merging

[GitHub] Branching and Merging

What happens when you create a new branch? Simply said, it creates a new pointer for you to move around.

What are Git Branches?

Suppose I'm writing an online novel. The published chapters are the main branch.

I don’t want to publish to the official line until my editor has reviewed and approved it.

So, I can make a secondary branch, work on that branch, and then submit it to be reviewed by my editor. Once they’ve approved the work I can combine that branch into the main branch.

If I hire an assistant, we can work on our own secondary branches. Then, only the work on a secondary branch that has been approved by both the other author and the editor will be combined with the main branch.

A branch represents a line of development. Branches in Git are movable pointers to commits.

New commits are recorded in the history of the current branch, which is called a fork in the history of the project. Every time you commit, the master branch pointer moves forward automatically.

A Git project can have multiple branches (or lines of development). Each of these branches is a standalone version of the project.

Why do we use Branches?

Branches are a powerful tool for managing your code in Git. They allow you to work on different aspects of your project without interfering with each other and allow multiple people to work on the same project at the same time.

You can also use branches to create pull requests on platforms like GitHub or GitLab, where other developers can review and approve your code before merging it to the main branch.

Working on a Branch

Let's go back to the original project we have in the previous post. I modified the colors.txt file and commit it.

❯ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   colors.txt

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

Git tells us that colors.txt is a modified file. It is now listed in the git status output. The colors.txt file is not staged for commit; in other words, it has not been added to the staging area.

Let's stage it and commit it.

❯ git add -A

❯ git commit -m "begin"
[main 2314fbc] modified colors
 1 file changed, 1 insertion(+)

❯ git log
commit 2314fbc16b12a978e455eb852c400425e389aa8a (HEAD -> main)
Author: Nguyen Duc Chinh <susuomlu@gmail.com>
Date:   Mon Aug 14 13:14:54 2023 +0700

commit acdeedbd9540d6a4918d81e7926096a380839dd2
Author: Nguyen Duc Chinh <susuomlu@gmail.com>
Date:   Sat Aug 12 15:00:47 2023 +0700
  first commit

You just made a new commit on a branch, this case is the main branch.

The HEAD indicates which branch you are on by referencing in .git > refs > heads. You can also know which branch you are on using git branch.

❯ git branch
* main

The * symbol indicates which branch you are in.

Create a Branch

You can use the command git branch <name> to create a new branch with the given name. This will not switch you to the new branch but only create it.

❯ git branch chapter#1

❯ git branch
* main

To switch to the new branch, you can use git checkout <name> or git switch <name>.

❯ git checkout chapter#1
Switched to branch 'chapter#1'

❯ git branch
* chapter#1

If I edit colors.txt again, then do git commit -m "new branch" it, the brach is now separated.

Now the HEAD is in another branch called chapter#1.

What is Git Merge?

Merge is a way of combining the changes from two different branches in Git. When you merge, you take the latest commits from one branch and apply them to another branch. This way, you can keep your code up to date and avoid conflicts. Merging is usually done when you want to integrate new features or bug fixes into your main branch.

You can merge using the git merge command, or use a graphical tool like GitHub Desktop or GitKraken.

There are two types of merges:

  • Fast-forward merges

  • Three-way merges

Fast-forward Merge

Let’s assume I work on my book colors.txt and I add commits C and D to the chapter#1 branch.

When I merge using the git merge command, a fast-forward merge would occur.

In this case, we can say the branches have not diverged. During the fast-forward merge, the main branch pointer simply moves forward to point to the commit that the chapter#1 branch points to, which is D.

Three-way Merge

Now suppose I decide to make a chapter#8 branch to work on chapter 8 of my book colors.txt, and I make commits E and F.

If I merge the chapter#8 branch into the main branch, it can’t be a Fast-forward merge because there is no way to just move the branch pointer forward to combine these two development histories.

Instead, a merge commit will be created to tie the two development histories together. This is Three-way Merge.

In this topic, I introduced the concept of branches as different lines of development and showed they are movable pointers to different commits.

We also learn what merging is, know the two types of merges, and explained how the type of merge that will be carried out depends on the development histories of the branches involved in the merge.

In the next topic, we will learn about Pushing and Pulling from a Remote Repository.

Did you find this article valuable?

Support Nguyen Duc Chinh by becoming a sponsor. Any amount is appreciated!