Git rebase (Merge Squash) my feature branch onto another branch
Asked Answered
B

3

58

I'm looking for a git command to help me with my feature branches when they're ready to go into Master. This git command would squash all my changes on my branch into a single commit on top of master. I do this today with:

git rebase origin/master
git rebase -i HEAD~4

Where 4 is the number of commits to squash. However, this requires me to know how many commits I have. I do this today by running:

git log HEAD...origin/master

and then counting the commits.

I feel as though there should be a better way to do this. Or is this how everyone else does it, too?

Barby answered 8/3, 2012 at 17:36 Comment(5)
I don't understand. Isn't this exactly what git rebase -i origin/master && git checkout master && git merge <feature_branch> does?Camm
... Provided you decide to squash during the interactive rebase that is.Camm
For the specific objective of counting the commits between your feature branch and master, you can do git log --oneline master..FEATURE-BRANCH | wc -lApocalypse
Possible duplicate of How to use git merge --squash?Janycejanyte
@Janycejanyte It is the same solution, but I didn't know about the squash command when I asked this question. Finding that other question requires me to know the answer.Barby
D
95

All you have to do is:

git checkout feature_branch
git rebase master
git checkout master
git merge --squash feature_branch

As the docs for git merge --squash say:

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

After that, you can git commit your changes which are already staged.

Downdraft answered 8/3, 2012 at 22:6 Comment(5)
this looks like it, though that's some pretty deep technical language in the docs. Is it basically saying that the branch you're merging from (feature_branch) remains unchanged? but that a subsequent merge would be really ugly?Barby
@JohnHinnegan: yes, the merged branch remains unchanged.Downdraft
nice. So, tangent question. If head of master is A. Then feturebranch has changes B and C. We do a merge --squash on master, so now it's A->BC. Then we add change D to the feature branch and try to squash again, does Master know that change BC is the same as changes B and C and will only add change D becoming A->BC->D?Barby
@JohnHinnegan: why don't you try for yourself? branching and merging is no expensive stuff. set up a test repo and play.Downdraft
Just for clarity, this will put your "squashed" changes in the staging area from which you make your actual commit to masterMythological
C
17

Here is what I do, gathered from a lot of experience working in larger teams:

# Get latest from master
git checkout master
git pull --rebase

# rebase master into your feature branch
git checkout feature/my-feature
git rebase master --preserve-merges

# merge feature into master
git checkout master

# DO ONLY ONE of the 2 options below
# if you only have one or (maybe) 2 commits in your feature branch
git merge feature/my-feature

# OR
# this forces an empty merge commit (useful if we need to roll things back)
git merge --no-ff feature/my-feature

# if you use Github, this should also close the pull request
git push origin master

Hope this helps!

Curtal answered 5/6, 2014 at 14:55 Comment(1)
this forces an empty merge commit (useful if we need to roll things back) What do you mean by this?Orest
Z
2

I think you are looking for git merge --squash. It should bring in the commits from your feature branch into master and squashes them, so that you can create a single commit.

Zakaria answered 8/3, 2012 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.