1

When I work on a branch, before to push the changes, I squash my single local commit in this way:

$ git add .
$ git commit -m 'x'
$ git rebase -i HEAD~2
# here I choose the option 'f' in the VI editor for the commit 'x' 
# to keep the comment from the first and save `:wq`

and finally update remote commit

$ push -f

Is there a way to avoid the above steps commit/rebase/choose-f/save/exit?

Randomize
  • 8,651
  • 18
  • 78
  • 133
  • Does this answer your question? [How do I squash my last N commits together?](https://stackoverflow.com/questions/5189560/how-do-i-squash-my-last-n-commits-together) --> use `git merge --squash` – mkrieger1 Dec 29 '22 at 08:51
  • I use that when I merge from the main branch so I do not think is what I am looking for. – Randomize Dec 29 '22 at 08:59

2 Answers2

2

You can reduce the steps commit+rebase+choose-f+save+exit to this single command:

git commit --amend --no-edit

You can make an alias if you do this frequently:

git config --global alias.fixup "commit --amend --no-edit"

and use it like this:

git fixup
j6t
  • 9,150
  • 1
  • 15
  • 35
0

You would needs to write a script which can chain commands resulting in what you need.

That script would:

  • get the comment of the last commit: git show -s --format=%s
  • git reset --soft @{u} (the remote tracking upstream branch) moving HEAD to the last time you pushed that branch.
    (same idea as in "squash unpushed commits")
  • add, and commit using the saved comment message from above.
  • push -f

That script can then be called from vim as a function mapped to key

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250