85

I have a repository and some local changes to commit. Before committing, I pulled the changes onto my local using Egit in Eclipse.

It creates a merge commit and I submit my commit over it.

Now when I am trying to push to origin, it is showing that it will push my commit as well as merge commit. But ideally, merge commit should not be a part of remote repository.

How to avoid this?

2

6 Answers 6

112

Use rebase option whenever you pull from remote repository. Please follow the below steps,

  1. Commit your changes - It will create a new commit in your local.
  2. Now do git pull --rebase <remote-name> <branch-name>.
  3. Basically the rebase take out your commits that you committed on the current branch HEAD as a patch. Then it will apply all the remote commits on top of HEAD and then applies your commits on top of it.
  4. So best practice is to commit changes then pull remote commits by using rebase option.
1
  • 13
    Careful though, as your commits will no longer be in chronological order. You have effectively rewritten the history, and this can cause a lot of nasty bugs unless you have very good test coverage. Safer to use merge.
    – samthebest
    Commented Dec 11, 2018 at 16:50
25

You can run

git config --global branch.autosetuprebase always

to make git pull --rebase the default behaviour for git pull.

18

When you have uncommitted changes, you can do,

git stash
git pull --rebase <remote> <branch>
git stash apply
5
  • Can I replace the second command with git pull --rebase (referring to this link. What will be the difference?
    – vineeshvs
    Commented May 11, 2020 at 10:40
  • 2
    @vineeshvs if you're pulling changes from the same branch that you're working on then you could just git pull --rebase. Otherwise, you have to explicitly mention the remote name and the branch name. Commented May 13, 2020 at 6:38
  • 9
    Or better: git pull --rebase --autostash.
    – FelipeC
    Commented Dec 11, 2020 at 1:20
  • @FelipeC your comment should be an answer so anyone can easily see it, so awesome, thank man,
    – Thinh NV
    Commented Jul 2, 2021 at 6:39
  • 1
    @ThinhNV Done. I have added an answer as well as the configurations if you always want to do this.
    – FelipeC
    Commented Jul 2, 2021 at 23:38
14

If you don't want a merge commit the best way to synchronize your local branch to the remote branch is to do a rebase. It is recommended that you do first a git fetch and then git rebase, however, as others have mentioned you can do both at the same time:

git pull --rebase --autostash

If you always do this you can configure git pull to do this automatically:

git config --global pull.rebase true
git config --global rebase.autostash true
6

The usual strategy is to work on a branch. When the remote master changes, pull the changes to master and instead of merging, rebase the branch.

See Git Rebase at Atlassian.

5

Scenario:

Suppose a is PR raised in GIT repo : from feature/my_bug_fix into release/project-007. But GIT is not allowing merge due to conflicts between above branches.

Then do this:

$ git checkout feature/my_bug_fix

$ git pull --rebase origin release/project-007

$ # resolve any conflicts encountered during the process

$ git rebase --continue

$ git push origin feature/my_bug_fix  --force

It is an effective & clean way to resolve conflicting branches. Additionally, by using --rebase you will not get merge commits which would otherwise appear if you used git merge.

Moreover:

pull          = fetch + merge

pull --rebase = fetch + rebase

So, choose the way the branch is to be handled.

You should now better understand the difference between merge and rebase :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.