DEV Community

Cover image for How to Use Git Rebase in a Practical Situation
crazyoptimist
crazyoptimist

Posted on • Updated on

How to Use Git Rebase in a Practical Situation

Imagine that there are two pull requests open on a project repository.
Each change has its own branch like this:

  • master
  • feature/add-base64-endpoint
  • feature/add-user-agent-endpoint

The challenge is to use git rebase to add both changes to master. When you finished, your master branch should have three commits in the following order:

  • feat: add user-agent endpoint
  • feat: add base64 endpoint
  • init

Okay, let’s go!

git clone repo_url
git status
git checkout feature/add-base64-endpoint
git rebase master
git status
git checkout master
git merge feature/add-base64-endpoint
git status
git checkout feature/add-user-agent-endpoint
git rebase master
Enter fullscreen mode Exit fullscreen mode

Oops! You should see rebase conflict!
You need to check the conflicted parts and fix it. Fortunately VS Code provides some great hints for you to do that.
I personally use Vim as my IDE because I'm a huge fan of Vim!
Do not forget to check if the code base is functionable even after you resolve the conflicts, i.e. all the tests should be green, app should be functional eventually.
Once you finish resolving conflicts, follow these steps:

git add .
git rebase --continue
git checkout master
git status
git checkout master
git merge feature/add-user-agent-endpoint
git status
git log
Enter fullscreen mode Exit fullscreen mode

Boom! You must be done!
Happy gitting! πŸ˜‰

Top comments (0)