DEV Community

Cover image for Tiny Tips : git pull --rebase
Thibaut Andrieu
Thibaut Andrieu

Posted on

Tiny Tips : git pull --rebase

By default, git pull will merge changes into your branches. When your Pull Request will be merged, you will end up to this:

git pull

Doing git pull --rebase will rebase your branch on top of upstream branch. When your Pull Request will be merged, you will end up to this:

git pull --rebase

It makes history much more readable. You clearly see where start your branch and what is in feature branches. It makes also interactive rebase or git bisect much easier.

Top comments (5)

Collapse
 
mbrehin profile image
Maxime Bréhin

Hi Thibaut, I recommend git pull --rebase=merges to preserve the "bumps" of local merges or better yet, do it automatically at pull by configuring it by default: git config --global pull.rebase merges.

Collapse
 
tandrieu profile image
Thibaut Andrieu

Oh gosh, I wish I had knew this option sooner 😀

I try to avoid local merge. If you have a local merge, it means you have a dependency on another branch, so it means a feature branch has lived too much time on its own and should have been merged sooner. But yeah, theory and practice...

Collapse
 
zeffk profile image
Huzaif Qazi • Edited

Hello have doubt regarding git rebase .

I have two branches one main and another my feature branch .

I made 3 commit on feature branch at 1pm 2pm and 3pm respectively, now I did the 4th commit on main-dev at 4pm , When I am on feature branch and running git rebase main-dev , on top of 4th commit my 3 commits are there absolutely as expected , but here comes the problem as soon as I am running git push my feature branch is getting diverged and then getting merged into feature again .

Can you help here , why it is happening.

Below screenshot for reference

Image description

Collapse
 
tandrieu profile image
Thibaut Andrieu

Hi,

That's unexpected, indeed. Do you use command line or a GUI FrontEnd like SourceTree or GitKraken ? GUI Frontend tends to do some extra stuff behind the scene.

You can try to do it fully by hand:

git fetch  # retrieve server modifications
git checkout feature/cart-page  # ensure you are in your feature branch
git rebase origin/main # rebase current branch onto origin/main branch
Enter fullscreen mode Exit fullscreen mode

Ensure your tree is as expected (with gitk or any gui tool), and then

git push origin feature/cart-page:feature/cart-page
Enter fullscreen mode Exit fullscreen mode

Now you should have the expected behavior.

Hope this help 😊

Note: "git pull --rebase" is a shortcut for "git fetch; git rebase origin/main"

Collapse
 
zeffk profile image
Huzaif Qazi

Thanks , adding --force while pushing after doing rebase helped me in getting expected result.