Git Merge vs. Rebase: A Comprehensive Guide to Resolving GitHub Commit Warnings

Git Merge vs. Rebase: A Comprehensive Guide to Resolving GitHub Commit Warnings

I’m guessing you’ve encountered a warning message in your GitHub remote repository that tells you that your branch has many commits ahead and behind the main branch.

You may be wondering how it’s possible to have fixed a bug on the main branch when you were only working and pushing changes on the dev branch. Perhaps you fixed the bug on the main branch and then forgot to push the changes to your dev branch. This can easily happen if you’re not careful to keep track of changes across different branches.

Don’t worry, in this brief article, I’ll walk you through how to address these issues using git merge or git rebase.

Git Merge and Rebase what’s the difference?

Git merge is a command that allows you to combine changes from one branch into another branch. This combination using git merge creates one single file of both branch changes called commit.

Git merge uses a non-linear git history graph, which helps keep track of changes made by other developers as the project grows. However, this non-linear pattern can often create a messy and clustered graph, making it difficult to trace commits.

A typical git merge graph:

The pointing arrow indicates the exact location where git merge combined two branches’ changes into one commit file.

To perform a merge commit, you must first save your changes in your working branch, which we’ll call the feature branch. Once you’ve made the changes, stage and commit them. After that, check out the branch where you want to merge your feature branch, which we’ll call the dev branch. Then, open a terminal and paste the command shown below.

git checkout dev
git merge feature-branch

This above command will perform a merge commit by taking your changes and incorporating them into dev branch.

Git rebase

Rebasing integrates changes by moving or replaying the commits from one branch on top of another. It effectively rewrites history. This means that the git rebase will create a duplicate of the commit file from both branch changes with a new ID, while the git merges command retains both branches' commits, git rebase duplicates them by creating a new ID and reference to it.

The git rebase uses a linear git history graph to keep track of file commits, this is particularly good, as it is easier to track file changes.

Typical git rebase history:

After executing the git rebase command, the changes from the rebase branch are applied on top of the other branch, resulting in a linear graph.

Which should you use to resolve GitHub commit warnings?

To resolve the Github warnings indicating that the main branch is ahead of the dev branch, you can manually merge the main unit into the dev branch using the git merge command. This should resolve the issue in most cases. To do this, you should first ensure that you have merged any changes made in dev into the main branch, so that GitHub only warns you about the branch being behind, rather than both behind and ahead.

Here’s an example:

Then checkout to dev branch and merge the main:

git checkout dev
git merge main
git push

Once you execute this command successfully, GitHub will be able to say: This branch is up to date with the main. In worst cases using merge didn’t work, well, this brings us to git rebase.

To fix the main branch ahead of the dev branch using git rebase, make sure you have merged the changes from the dev branch into the main, then checkout to dev and rebase main against the dev branch.

git checkout dev
git rebase main
git push

This command will update the dev branch with changes from the main, and then Github commits warnings will be gone.

Pro Tip

Rebasing can be performed in Vscode with just a few clicks using the Vscode built-in source control tab.

To do this locate and visit the source control tab

Then click on the 3 dotted tabs,

Select Pull, Push in the dropdown, and click on Pull from,

On the next screen select origin/main, this is where git will pull changes from to rebase into the dev branch.

Conclusion

When working on a project, it’s important to be attentive to git and branch management. Failing to do so could lead to losing project data. One of the most commonly used workflows to simplify Git branching and maintain a linear Git history is Git rebase.

It’s important to note that Git rebase is different from Git merge. While Git merge is used to push new changes to the team repository, Git rebase creates new commit references that may confuse other developers in your team. Therefore, it’s best to avoid using Git Rebase too frequently and stick to Git Merge instead.

Git - git-rebase Documentationon

Git - git-merge Documentation

https://www.youtube.com/watch?v=zOnwgxiC0OA

That’s a wrap! If you have thoughts on this, be sure to leave a comment.

If you found this article helpful, leave a comment.

You can follow me on Twitter here.