I have two branches. My master branch and my feature branch.
I'm developing on the feature branch and did several commits there and pushed them to the remote. From time to time I merged the master branch into my develop branch to make sure that I get the newest changes.
Now I'm done with developing on the develop branch and want to merge the develop branch into the master branch.
Ideally, I would like to squash all commits on the develop branch into one single commit. I guess I could do that with
git merge --squash
However, can I still do that when I merged master into develop from time to time?
You have merged master
into develop
from time to time - you mean to say you have updated the master
branch and develop
branch with the changes from master
branch, in your local repo from time to time, correct?
In that case, when you do a git checkout develop
and a git log
, and if you are the only person who has been working on this branch , you should see only your commits till date - in which case you can directly do a git merge --squash develop
, which will take all the commits made to develop
branch and squash it into one.
If you have other people working on this develop
branch too , and their commits are also in between yours, and you want to only squash your commits, then do a git rebase -i
, where you can pick the commits you want to squash by replacing the word 'pick' before the corresponding commits with 'squash' .
Or, you can follow the first method listed here .
The easiest way to turn multiple commits in a feature branch into a single commit is to reset the feature branch changes in the master and commit again, this time - everything as one.
If you use git merge develop --squash
to merge develop
branch into master
, it won't squash commits to a commit on develop
branch, but create a "merge commit" on master branch. Illustrate by below graphs:
Assume the original commit history is (before merging develop
into master
):
A---B---F---G master
\
…---C---D---E develop
After you execute git merge develop --squash
, the new commit H
as if you do a real merge the changes from develop
branch into master
.
A---B---F---G---H master
\
…---C---D---E develop
And of cause you can merge master
into develop
and then merge develop
branch back into master
branch. But it's not a recommend way. You should treat only one branch as the main branch (such as master
branch as main branch). When the other branch ( develop
) has new features/changes, you just need to merge develop
into master
branch. When other new changes committed on develop
branch later, you just need to merge develop
branch into master
branch from time to time.
And if you only want to squash commits on develop
branch, you can use git rebase -i develop~n
, and then edit the commits you want to squash (change drop
to squash
) in interactive window.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.