Sachin Jain
Sachin Jain

Reputation: 21842

How to get rid of Merge commits in git

I have a repository which is shared by more than 1 person So often I see there are a lot of merge commits in commit history. I want to get rid of them to make my commit history to look cleaner.

What is the practice we should follow to avoid these merge commits? Is it doable ?

I read about merging using --ff-only switch. Can this switch help me ?

Upvotes: 5

Views: 14489

Answers (3)

mrutyunjay
mrutyunjay

Reputation: 8350

If you have your local changes which are not pushed to remote yet, and if you pull latest updates from remote it will create a merge commit. To avoid this try

git pull --rebase

Upvotes: 11

Sachin Jain
Sachin Jain

Reputation: 21842

I have got one more approach to avoid merge commits. This is what I am following now.

  1. Before making any commit, always pull latest code from your upstream.

  2. Sometimes, it may say please commit your changes/stash them and will not let you get the latest commits. This is because you have edited some file and which was also edited in one of the commits in upstream. So stash your changes using git stash

  3. Again use git pull origin master

  4. This time, there won't be any problem and you have the latest code from upstream. Now, regain your code from stash using git stash apply

  5. There might be some automerge conflict, resolve them and make your commit and push to server using git push origin master

There might be a case, that during process of merging and making commit, someone else also makes a commit on upstream/server. In this case, your push will fail and ask you to pull code from server/upstream again. If you pull instantly, you will be a victim of merge commit. So to avoid this use git reset head^

Don't worry your code won't get lost and now you are again back to Step 0. Follow the same steps again. You are good to go.

Upvotes: 3

linquize
linquize

Reputation: 20396

No, --ff-only can only make git merge fail if cannot be resolved as a fast-forward, but failing a merge does not solve the problem.

To avoid having too many merge commits, use git rebase.

See http://git-scm.com/book/en/Git-Branching-Rebasing to know how rebase works.

Upvotes: 8

Related Questions