Skip to content

Instantly share code, notes, and snippets.

@zmunk
Last active December 16, 2025 19:42
Show Gist options
  • Select an option

  • Save zmunk/26bfd3b112befc09a7f1474f0babaef2 to your computer and use it in GitHub Desktop.

Select an option

Save zmunk/26bfd3b112befc09a7f1474f0babaef2 to your computer and use it in GitHub Desktop.
Resolving merge conflicts during rebase

Say we come across a merge conflict when rebasing a feature branch onto main.

We can see the conflict with git diff.

Example output:

diff --cc config.txt
index 0e7703e,904fdf4..0000000
--- a/config.txt
+++ b/config.txt
@@@ -1,7 -1,5 +1,15 @@@
++<<<<<<< HEAD
 +database_host=prod-db.example.com
 +database_port=3306
 +cache_enabled=false
 +log_level=warning
 +api_key=secret123
 +rate_limit=1000
 +backup_enabled=true
++=======
+ database_host=localhost
+ database_port=5432
+ cache_enabled=true
+ cache_ttl=300
+ log_level=info
++>>>>>>> 436c102 (Feature: Enable caching)

We first need to view the current commit's original diff in relation to its parent. This will allow us to see what the aim of the original commit was.

To find the commit we can do git rev-parse REBASE_HEAD. Or we can do git blame <branch> -- file and find the commit hash corresponding to the changed line.

We then look at the diff of that commit:

git log <commit-hash> -1 -p

The argument -1 tells git to show only the most recent commit.

The argument -p tells git to show the diff.

Output:

commit 436c1020e63b3e4e797b916adf86b21aa4b1b0f4
Author: zmunk <>
Date:   Fri Oct 10 12:36:04 2025 +0300

    Feature: Enable caching

diff --git a/config.txt b/config.txt
index 7a58ab2..904fdf4 100644
--- a/config.txt
+++ b/config.txt
@@ -1,4 +1,5 @@
 database_host=localhost
 database_port=5432
-cache_enabled=false
+cache_enabled=true
+cache_ttl=300
 log_level=info

In the above example, cache_enabled was set to true and cache_ttl=300 was added.

We want to edit the file config.txt, making sure to keep all the changes on HEAD except for the changes that were specifically made in the original commit.

So, in this example, we would keep the following from HEAD (everything except cache_enabled):

 database_host=prod-db.example.com
 database_port=3306
 log_level=warning
 api_key=secret123
 rate_limit=1000
 backup_enabled=true

Then, we would add the following from the original commit:

cache_enabled=true
cache_ttl=300

This is what the diff looks like after the change (git diff):

diff --cc config.txt
index 0e7703e,904fdf4..0000000
--- a/config.txt
+++ b/config.txt
@@@ -1,7 -1,5 +1,8 @@@
 -database_host=localhost
 -database_port=5432
 +database_host=prod-db.example.com
 +database_port=3306
- cache_enabled=false
+ cache_enabled=true
+ cache_ttl=300
 -log_level=info
 +log_level=warning
 +api_key=secret123
 +rate_limit=1000
 +backup_enabled=true

Now, since we've resolved the conflict, we run git add config.txt then git rebase --continue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment