Let's suppose I need to perform some average-length (several days) refactoring.
I create "mybranch" from master, do the work, 开发者_运维百科sometimes I perform merges from master to mybranch (some other team members supposed to continue the work, and probably I'll need to get their changes). And after a while I'm ready to merge my changes back to master.
Will I be able to squash only my changes (excluding the changesets with merges from master, since they aren't mine) and cherry-pick a single result commit of my works into the master? Is it a possible to do scenario?
Yep. You can confirm it with a small test repository. Just do:
git rebase --interactive master mybranch
and choose squash
for all the lines except the first. Then you just checkout master
and do a regular fast-forward merge of mybranch
.
After rebasing, there will be no reference on those commits in previous "mybranch"
What I will do and could be more safety is that
git checkout mybranch
Create a branch (it is actually a temporary branch for rebasing purpose)
git checkout -b movingToMaster
Try to rebase the movingTomaster
to master
git rebase -i master
An editor will be prompted out asking you which commit should be picked, or squashed or edit...
Edit that files, in your case, the first line should keep remain the same and the other line should change "pick" into "squash", you can amend your commit message if you want to. Save it after you make enough change on that file.
i.e.
pick 0a81405 Bug Fix 1
pick 91be655 Bug Fix 2
pick 1200fc7 Bug Fix 3
pick 1211fb7 Bug Fix 4
pick ba77fdf Bug Fix 5
Change to
pick 0a81405 Bug Fix 1
squash 91be655 Bug Fix 2
squash 1200fc7 Bug Fix 3
squash 1211fb7 Bug Fix 4
squash ba77fdf Bug Fix 5
Save it. Then
git checkout master
Move your master
branch to movingTomaster
branch by using Fast forward merge
git merge movingTomaster
Note: it will be no harm since it is only a Fast-Forward Merge and would not clutter your history.
Delete your movingTomaster branch if you want to
git branch -D movingTomaster
精彩评论