Is there a way to squash and merge master into the same branch? Effectively taking all the pending commits and putting them into 1 commit?
My original idea is a script that takes my-branch
and does a git checkout master && git pull && git checkout my-branch-squashed
and then git merge --squash my-branch
(deal with any merge conflicts) and then finally delete my-branch
and rename my-branch-squash
to my-branch
This seem very round-about and possibly bad, so I am trying to see if there is any other way. The intent I am trying to solve is that when I put branches on github and they are "squashed and merged" into master, the branch that exists on the local machine doesn't match the branch that was merged into master, so when using git branch --merged ${1-master} | grep -v " ${1-master}$" | xargs -r git branch -d;
it doesnt correctly delete the branches that have already been merged into master. What I want is a way to auto-delete old branches that have been merged into master
When you select the Squash and merge option on a pull request on GitHub.com, the pull request's commits are squashed into a single commit. Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit and merged into the default branch.
Squash merges, as it's proponents argue, are more valuable than merge commits because entire new features or bug fixes can be compressed into a single commit and therefore easier to code review and read at some point in the future.
The steps to merge master into any branch are: Open a Terminal window on the client machine. Switch to the feature branch. Use git to merge master into the branch.
You can do that using git rebase
, and fixup the commits you want to merge:
$ git rebase -i HEAD~5
pick c2e2c87 commit 1
f 689d474 commit 2
f aa9d9b4 commit 3
f 888a009 commit 4
f d396e75 commit 5
# Rebase 2f7f53e..d396e75 onto 2f7f53e (5 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
You can use git rebase -i --root
in order to rebase from the first commit.
If all you really want is to squash your local development history before submitting a pull-request, the simplest way is to just develop on a local feature branch which is different from whatever upstream branch you want to affect.
Then, the procedure for squashing it onto master is
git checkout master
git merge --squash feature
(replace master
with integration
or whatever).
I'd use rebase -i
for fine control, but for this simple case we can use git's knowledge of your history to figure out the last common ancestor automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With