DEV Community

Mingming Ma
Mingming Ma

Posted on

Managing GitHub Branches

I want to share my recent developing process of working two separate branches derived from the same main branch of my project txt2html, performing a fast-forward merge, and handling a three-way recursive merge with conflict resolution. Hope it helps!

Branch Creation

$ git checkout main
$ git checkout -b issue-9
$ git checkout -b issue-10
Enter fullscreen mode Exit fullscreen mode

Here, the branch name issue-9 and issue-10 is coming from my new created issues (9, 10), you can choose you preferred branch names for the features.

Working on Separate Branches

Since we have our two feature branches, we can work on them separately.
Note that after creating the two branches, we still at main branch, so, let's move to branch issue-9 from main before we making changes:

$ git checkout issue-9
Enter fullscreen mode Exit fullscreen mode

After making changes about issue-9 feature, we need to commit that:

# files changes
$ git add <changed-files>
$ git commit -m "issue-9 changes log"
Enter fullscreen mode Exit fullscreen mode

Make sure we committed all changes so we can move to another branch , otherwise the other branch will be impacted/polluted.

$ git checkout issue-10
# files changes
$ git add <changed-files>
$ git commit -m "issue-10 changes log"
Enter fullscreen mode Exit fullscreen mode

Merge

Once we done the features, let's merge them to the main branch.

Fast-Forward Merge

Because the issue-9 is originated from the main branch, merging issue-9 back to main will be a fast-forward merge. This should be no conflicts.

$ git checkout main
$ git merge issue-9
Enter fullscreen mode Exit fullscreen mode

Three-Way Recursive Merge

However, when it comes to merging the second feature branch issue-10 back into main, we may encounter conflicts that need resolve due to issue-9 introduced changes. This is where the three-way recursive merge comes into play.

$ git checkout main
$ git merge issue-10
Enter fullscreen mode Exit fullscreen mode

We will need to resolve the conflicts by editing the affected files (I'm using Visual Studio Code and it will highlights the conflicts for you) then committing the changes.

# resolved changes 
$ git add <resolved-changed-files>
$ git commit -m "Resolved conflict and merge feature 2"
Enter fullscreen mode Exit fullscreen mode

Push the changes to GitHub

Since all conflicts are already resolved, we can then push to remote repo main branch:

$ git push origin main
Enter fullscreen mode Exit fullscreen mode

A little more about the Commits

You can see my two feature commits 627a890 and 0418e18 have same parent 8b80c93:

1c

2c

So it means they are created at same point from the main branch as we discussed before.

and the commit 3fab6c9 has two parents:

3c
That's the conflicts resolved commit and so it comes from the two previous commits.

That's it! In this post, we covered the process of creating two separate branches from the same main branch, performing a fast-forward merge when there are no conflicts, and handling a three-way recursive merge with conflict resolution when conflicts arise. Hope it helps! Happy coding!

Top comments (0)