Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase (Merge Squash) my feature branch onto another branch

People also ask

Can I merge a feature branch into another feature branch?

Merge branchesMerging your branch into master is the most common way to do this. Git creates a new commit (M) that is referred to as a merge commit that results from combining the changes from your feature branch and master from the point where the two branches diverged.

Is squash and merge the same as rebase?

Merge squash merges a tree (a sequence of commits) into a single commit. That is, it squashes all changes made in n commits into a single commit. Rebasing is re-basing, that is, choosing a new base (parent commit) for a tree.

How do you squash commits on a new branch?

Git squash with a commit id The last command opens the interactive Git rebase tool which lists all of the commits in the branch. You must type the word pick next to the commit you want all others to be squashed into. Then type 'squash', or just the letter 's', next to each commit to squash.


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.


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!


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.