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?
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
You can use soft reset
to squash your branch (say, feature
) commits. Say you have 5 commits in feature
branch. Now you need 5 commits = 1 new commit.
$ git checkout feature
$ git log # see how many commits do you need to squash
$ git reset --soft HEAD~4 # note: if you have N commits then HEAD~{N-1}
Or, git reset --soft <first-commit> # reset to first commit of your branch
$ git add . # add the files
$ git commit --amend -m 'Squash all commits' # squash all the commits
$ git push -f origin feature # force(-f) push to remote since git history is changed
Now, Pull master branch changes.
$ git pull origin master
Create a Pull request now using GitHub GUI (browser).