How to Merge Multiple Commit to the One
January 3, 2025 · 2 min read
If you have any questions, feel free to comment below. Click the block can copy the code.
And if you think it's helpful to you, just click on the ads which can support this site. Thanks!
Sometimes we make too many small commits during development, and it’s better to squash them into one clean commit before pushing. Here’s how to use git rebase -i to merge multiple commits.
rebase #
git rebase -i HEAD~n
# git rebase -i [startpoint] [endpoint]
# this means (startpoint, endpoint] if the endpoint is not specified, it means the HEAD
Example #
git log --oneline -n 5
# ================================================================================================
# 4047b887fc (HEAD -> master, origin/master, origin/HEAD) swscale/swscale_unscaled: add more unscaled planar RGB to planar RGB coverage
# c5ebd56500 swscale/swscale_unscaled: add unscaled XV{36,48}LE <-> XV{36,48}BE
# 271aea60a4 fate/pixfmts: extend the high bit depth test
# ================================================================================================
# c8438546ff avfilter/vsrc_testsrc: add support for RGB48/RGBA64
# e7382b4d01 swscale/swscale_unscaled: add unscaled x2rgb10le to packed RGB
git rebase -i HEAD~3 # last 3 commits
pick 271aea60a4 fate/pixfmts: extend the high bit depth test
pick c5ebd56500 swscale/swscale_unscaled: add unscaled XV{36,48}LE <-> XV{36,48}BE
pick 4047b887fc swscale/swscale_unscaled: add more unscaled planar RGB to planar RGB coverage
# Rebase c8438546ff(#the last 4th commit)..4047b887fc(#HEAD) onto c8438546ff (3 commands)
# # Indeed it means make last 3 commits on the basis of the last 4th commit
#
# Commands:
# p, pick <commit> = use commit # retain the commit and the message
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# 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
squash #
Then we squash the 2th commit c5ebd56500 into the 3th commit 271aea60a4:
pick 271aea60a4 fate/pixfmts: extend the high bit depth test
s c5ebd56500 swscale/swscale_unscaled: add unscaled XV{36,48}LE <-> XV{36,48}BE
pick 4047b887fc swscale/swscale_unscaled: add more unscaled planar RGB to planar RGB coverage
Edit commit message #
then you can edit the commit message, eg. still fate/pixfmts: extend the high bit depth test, note that the commit id will all be changed.
- Also if there is some conflict, you need to resolve it and
git add .andgit rebase --continue - If you want to abort the rebase, you can use
git rebase --abort
Finally #
# ================================================================================================
# a17f66b05d (HEAD -> master) swscale/swscale_unscaled: add more unscaled planar RGB to planar RGB coverage
# 9b39ec9d7e fate/pixfmts: extend the high bit depth test
# ================================================================================================
# c8438546ff avfilter/vsrc_testsrc: add support for RGB48/RGBA64
# e7382b4d01 swscale/swscale_unscaled: add unscaled x2rgb10le to packed RGB
# ae8ef645ec swscale/swscale_unscaled: add unscaled x2rgb10le to planar RGB
If you want to follow my updates, or have a coffee chat with me, feel free to connect with me: