1

I'm new to GIT and I want to how to avoid intermediate commits say D1-D2-D3 from develop getting added to master branch on git merge?

For e.g.

A-B-C(master) & D1-D2-D3-D4(develop) 

_on merge becomes_ 

A-B-C-D1-D2-D3-D4(master)

It should look like A-B-C-D4(master) on merge. Ideally I prefer A-B-C-E(master) where E is a new commit that is a merge commit, merge of develop onto master.

I tried git merge --no-ff develop and though it adds the new commit however all the intermediate commits also gets added to master.

appu
  • 471
  • 1
  • 8
  • 26

2 Answers2

1

Use squash merge: git merge --squash develop.

phd
  • 82,685
  • 13
  • 120
  • 165
0

Method 1

Easier (albeit risky) way is to soft reset all the newer commits and create one commit containing all the changes.

git checkout develop
git reset HEAD~n   # n is the number of commits you want to merge together.
git commit -am "D4"

Method 2 (preferable)

Commonly used approach is to squash commits in develop branch, before merging/rebasing it into master.

git checkout develop
git rebase -i master

# This will open the editor and let you squash/edit the commits as applicable. For instance
pick 8705209 D1
squash 7979797 D2
squash 9793757 D3 # Use squash to squash commits into the previous one.
# add the final commit message

git checkout master
git rebase develop

Rewriting git history

hspandher
  • 15,934
  • 2
  • 32
  • 45