2

Can someone help give me a a sequence of git commands to help squash any commits that I have in my pull request?

Let's assume that I have pushed changes (or just committed) and would like to squash past commits so it only shows as one.

Is this a reasonable chain of commands?

git checkout master
git pull origin master

git checkout [branch]
git rebase master 

git push --force-with-lease

Thanks

Jack_Frost
  • 169
  • 2
  • 8

1 Answers1

0

Instead of

git rebase master

you can alter this to

git rebase -i master HEAD~n

where n is the number of commits on your branch.

You will then see something like this:

pick f7f3f6d Change my name a bit
pick 310154e Update README formatting and add blame
pick a5f4a0d Add cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
# p, pick <commit> = use commit
# 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

(source: Git Documentation)

As stated in the description you will then edit the commands. Instead of pick you need to write squash or s for commits you want to squash into the previous one.

Dominic Frei
  • 327
  • 1
  • 5
  • Completely unnecessary. If the goal is to squash all commits to one, just reset soft to before the starting commit, and now commit. – matt Sep 23 '20 at 00:58
  • Never said, it's the only or best way. It's just one way. What I like about it is control. I can see each commit I'm squashing in that list and therefore make sure everything works as expected. – Dominic Frei Sep 23 '20 at 01:01