Managing GitHub Branches
Published: 2023-09-30 03:45:31

I want to share my recent developing process of working two separate branches derived from the same main branch of my project [txt2html](https://github.com/mingming-ma/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
```
Here, the branch name issue-9 and issue-10 is coming from my new created issues ([9](https://github.com/mingming-ma/txt2html/issues/9), [10](https://github.com/mingming-ma/txt2html/issues/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
``` 
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"
```
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"
```

## 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
```

### 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
```

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"
```

## Push the changes to GitHub 
Since all conflicts are already resolved, we can then push to remote repo main branch:
```
$ git push origin main
```

## A little more about the Commits 
You can see my two feature commits [627a890](https://github.com/mingming-ma/txt2html/commit/627a89046a578d49d1cff45dfde12d20ef5d37b5) and [0418e18](https://github.com/mingming-ma/txt2html/commit/0418e1899161cc81ee750a5e3145c2a70e8f48e3) have same parent `8b80c93`: 

![1c](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnp3c4iqoep17laz0wu9.png)


![2c](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3xt6n20h98olrcpbmb1.png)

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

and the commit [3fab6c9](https://github.com/mingming-ma/txt2html/commit/3fab6c9619e8876f7da7183522db445ac82cfb53) has two parents:

![3c](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1223fs4ign6h5rxg240d.png)
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!