Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Squash feature branch commit after merging from master

I am trying to squash commits in a branch to that when it is finally merged to master (after pull request if aproved) the commit history looks clean.Hence before raising the pull request I do a

git rebase -i

and rewrite the history.

However while developing the feature in its branch I have to merge the content of the master onto the branch because master usually moves ahead due to other feature branches being merged.

I see ones I merged master to feature branch I can not squash he commits any more using interactive rebase. It leads to unusual diff during pull requests i.e. changes which came as part of merges from master.

What is the best way to squash the commits in this case?

like image 943
Neil Avatar asked Feb 28 '17 16:02

Neil


2 Answers

If you just want to sqaush everything, then you have a way simpler way to do that that does not rely on using interactive rebasing. You can just do a soft reset to your master branch and then commit those changes:

git reset --soft master
git commit -m 'All changes from my branch squashed'

This basically resets the branch pointer to the master branch, without changing any of the content in your working directory. So the result is that you have all those changes in your index which you can commit then at once.

like image 165
poke Avatar answered Sep 28 '22 08:09

poke


If you want the feature branch is on the top of master branch, so that your pull request contains changes from other feature branches merged. You can use below commands:

git checkout feature
git pull origin master --rebase

This will rebase feature branch on the top of master branch after fetching from origin master.

like image 23
Marina Liu Avatar answered Sep 28 '22 09:09

Marina Liu