Git rebase conflict? Try continuing and aborting with one file

If git rebase stops at a conflict, start with git status to find the file. Decide what the file should say, then fix it and continue. To go back to where you started, abort the rebase. Try both choices with one line of opening hours.

Create a practice folder

A rebase takes the changes saved on your current branch and applies them again, one commit at a time, on a new starting point. Here you will put the change from feature on top of the latest change in main. Use a new practice folder on your computer.

Open Git Bash on Windows or a terminal on macOS or Linux. Run git --version to check that Git is available. Then create the folder and enter it. If a folder with that name already exists, pick a different name.

mkdir rebase-practice && cd rebase-practice

Once you are in the new folder, run the commands below. The name and email settings apply only to this practice repository.

git init -b main
git config user.name "Git Practice"
git config user.email [email protected]

echo "Hours: 09:00-17:00" > hours.txt
git add hours.txt
git commit -m "Base hours"
git branch feature

echo "Hours: 08:00-17:00" > hours.txt
git add hours.txt
git commit -m "Open earlier"

git switch feature
echo "Hours: 09:00-18:00" > hours.txt
git add hours.txt
git commit -m "Close later"

git log --oneline --graph --all

In the history, you will see two branches split from Base hours, the original record. The main branch has Open earlier, and feature has Close later.

* a14b103 (HEAD -> feature) Close later
| * b23cd04 (main) Open earlier
|/
* c31de05 Base hours

A commit is a saved record of a set of changes. The letters and numbers on each line are its ID. You will see different IDs when you run the exercise.

Find where Git stopped and read both changes

For the exercise, make sure you are on feature and run git rebase main. If your rebase has already stopped at a conflict, start with git status.

git rebase main
git status

Both branches changed the same line. Git stops because it cannot choose what the final line should say. In the git status output, both modified tells you which file was changed on both branches.

Unmerged paths:
  both modified:   hours.txt

Open hours.txt in a text editor. Above ======= is what the rebase has put in place so far. In this example it is the main version, 8 a.m. to 5 p.m. Below the divider is the change from the feature commit Git is trying to apply, 9 a.m. to 6 p.m.

<<<<<<< HEAD
Hours: 08:00-17:00
=======
Hours: 09:00-18:00
>>>>>>> a14b103 (Close later)

Read the two parts between the markers and decide what to keep. To try both choices, try aborting first. You can then run the same rebase again and try continuing.

Keep both changes and continue

Keep the earlier opening time and the later closing time. Replace everything in hours.txt, including the markers, with this one line and save the file.

Hours: 08:00-18:00

Run git add hours.txt to tell Git the file is ready, then run git rebase --continue to move on.

git add hours.txt
git rebase --continue

If an editor opens for the commit message, keep the message, then save and close the editor. If the editor is Vim, press Esc, type :wq, and press Enter.

Use git status to check that the conflict is clear. If Git stops at another conflict while applying more commits, repeat the steps for the files it lists.

git status
git log --oneline --graph --all

In this example, Close later on feature now sits above Open earlier on main. The hours are 8 a.m. to 6 p.m., and Git has given the reapplied Close later commit a new ID.

* d42ef06 (HEAD -> feature) Close later
* b23cd04 (main) Open earlier
* c31de05 Base hours

Abort and go back to where you started

While the rebase is still in progress, git rebase --abort takes you back to where you started. This also undoes edits you made to resolve the conflict. Copy any edits you want to keep somewhere else before running it.

git rebase --abort
git status
git log --oneline --graph --all

In this example, you are back on feature and hours.txt shows 9 a.m. to 6 p.m. again. The Close later ID and the split history return to the state you saw at the start. To try continuing too, run git rebase main again, then follow the steps to continue.

git rebase --skip moves on without applying the current commit. Here it would leave out the Close later change, so closing time would still be 5 p.m. To cancel the whole rebase, choose --abort.

Check your understanding

What should the file say if you want to open at 8 a.m. and close at 6 p.m.?

Keep the single line Hours: 08:00-18:00. Remove the conflict markers and save the file. Then run git add hours.txt and git rebase --continue.

What happens to the opening hours and history if you abort at the conflict?

The feature hours go back to 9 a.m. to 6 p.m. Close later keeps its original ID, and main and feature split from Base hours again.