DEV Community

Nina Ricci
Nina Ricci

Posted on

Git Conflicts; Fixing it with Git Rebase

Getting past the basic git clone, commit, add, pull and push, I have learned a few good to know git commands over the years because of repeatedly common problems I've faced when doing version control. I'm planning to create a series for this topic in hopes that this can also help you.

Here's a very short story of how a Git conflict can happen and how to fix it with Git rebase.

Git Conflicts

Usually, these problems are faced when the team gets bigger and a lot more devs are contributing to the same project on the same file.

2 devs working on the same file.

This is very common and it's okay, so don't worry! What I usually do is to plan ahead with the other dev so we know both of us are working on this specific same file. To minimize merge conflicts and efficiently fix them, we want to always rebase from develop whenever there are any changes so we always have an updated version of the base branch.

DAY 1 ๐ŸŒž

Dev 1 and 2 creates feature branches

dev 1 creates and works on feature A

git checkout develop
git branch -b feature/feature-a
# work on file A
Enter fullscreen mode Exit fullscreen mode

dev 2 creates and works on feature B

git checkout develop
git branch -b feature/feature-b
# work on file A
Enter fullscreen mode Exit fullscreen mode

Feature B is merged to develop branch and Dev 1 commits all their changes

in between the day, dev 2 who was working on feature B asks for a code review and merges their branch to develop. dev 1 who is working on feature A git add and commits all their changes for the day.

DAY 2 ๐ŸŒž
since, dev 2's branch (feature B) was merged to develop and we know that both devs had worked on the same file, dev 1 (feature A) will now rebase from develop branch to have an updated copy on their current feature branch and fix the conflicts.

dev 1, feature A rebases from develop and fixes conflicts

# don't forget to git add and commit your changes first
git checkout develop
git pull origin develop
git checkout feature/feature-a
git rebase develop
# by this time, git will write your commits on top of develop 
# and you will be seeing the conflicted files
Enter fullscreen mode Exit fullscreen mode

go to the conflicted file and if needed, consult with dev 2 and make sure you save the file where both changes are written.

# resolve conflicted file/s
git add path/to/file
git rebase --continue
Enter fullscreen mode Exit fullscreen mode

git rebase interactive should be done after you finish fixing the conflicted file and you are good to go. ๐Ÿ‘

Feature A rebases on top of develop

Notice that Feature A now is on top of both develop and Feature B that was merged the day before.

Thank you for reading all the way here! I hope you've learned something new and that this can help you on your productivity and collaboration with your team ๐Ÿค

Top comments (0)