Git Conflicts; Fixing it with Git Rebase
Tags: git, productivity, tips
Published: 2022-02-07 09:43:48

---
title: Git Conflicts; Fixing it with Git Rebase
published: true
description: A short story with visualisation on how to fix git conflicts with git rebase
tags: git, productivity, tips
cover_image: 
---

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zec5ys9xgujbw8tv72r.png)

####dev 1 creates and works on **feature A**
```shell
git checkout develop
git branch -b feature/feature-a
# work on file A
```

####dev 2 creates and works on **feature B**
```shell
git checkout develop
git branch -b feature/feature-b
# work on file A
```
![Feature B is merged to develop branch and Dev 1 commits all their changes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5lq3dcbrs9lfqlhshy8.png)

*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
```shell
# 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
```
go to the conflicted file and if needed, consult with dev 2 and make sure you save the file where both changes are written.

```shell
# resolve conflicted file/s
git add path/to/file
git rebase --continue
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9khlmol10iak9lta4bb.png)

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 🤝