Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid merge commits from Git pull when pushing to remote

Tags:

git

merge

pull

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?

like image 841
jazz199 Avatar asked May 05 '15 11:05

jazz199


People also ask

How do I stop git from auto merging?

Restore the unwanted files then with git checkout -- filename . @marckassy: But you could then git reset HEAD and git add -p to select what you want. To shut off the initial merge completely, add -s ours .

Why does git merge when I pull?

git pull causes merge commits because git is merging. This can be changed by setting your branches to use rebase instead of merge. Using rebase instead of merge on a pull provides a more linear history to the shared repository. On the other hand, merge commits show the parallel development efforts on the branch.

Does git merge automatically pull?

Since your local commit isn't on the remote repository yet, when git pull runs git merge origin/[branch] [branch] , it will automatically do a "recursive" merge and create a commit with the remote changes.


2 Answers

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.
like image 199
Mohanraj Avatar answered Sep 22 '22 12:09

Mohanraj


You can run

git config --global branch.autosetuprebase always 

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

like image 23
MathKid Avatar answered Sep 24 '22 12:09

MathKid