How to resolve Git conflicts after rebase

After update your develop/master branch with the latest changes, you probably get conflicts with your code in your current feature branch.

The following commands are used in a typical Git workflow to update a feature branch with changes from the main branch (usually the master branch).

git checkout master

This command switches to the master branch. The checkout command is used to navigate between branches in Git. After executing this command, you will be on the master branch.

git pull

The pull command combines two actions: it fetches the latest changes from the remote repository (origin) and automatically merges them into the current branch. If you are on the master branch, this command will fetch and merge the latest changes from the remote master branch into your local master branch.

git checkout your-feature-branch

This command switches back to your feature branch (replace your-feature-branch with the name of your actual feature branch). After executing this command, you will be on your feature branch again.

git rebase master

The rebase command is used to update your feature branch by applying the changes from the master branch on top of your feature branch. It effectively moves your feature branch to the top of the master branch, making your branch’s history linear and up to date with the latest changes from master.

At this point, if you run git branch you’ll see a temporary branch was created while you fix the conflicts:

(no branch, rebasing your-feature-branch)

Check the files with conflicts and update them with the needed changes, that could be, accept the current changes or the incoming changes from the master branch. The chosen option will depend on your case, so be careful.

After update the files that have conflicts run the following commands:

git add .

To stage changes in the working directory for the next commit.

git rebase —-continue

The git rebase command is used to integrate changes from one branch into another. The –continue flag is used in the context of a rebase to tell Git to continue with the rebase operation after resolving conflicts or making other changes.

git branch

Lists all the branches in the repository, indicating the current branch with an asterisk (*). You’ll see that the temporary branch created before is not present anymore.

All conflicts were resolved, now you need to push a new clean branch to your remote repository, so your pull request gets updated.

git push --force origin your-feature-branch

Leave a Reply