Home > other >  How to rebase without intermediate commit on Git?
How to rebase without intermediate commit on Git?

Time:12-29

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?

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • git
  • Related