Cook
See the patch of the current commit
git rebase --show-current-patch
Status of rebase
Number of commits in this rebase
cat .git/rebase-apply/last
Out of the number of commits in this rebase, which are we on?
cat .git/rebase-apply/next
Which commit is currently being applied?
cat .git/rebase-apply/original-commit
Cherry-Pit: Remove a specific commit
git rebase -p --onto SHA^ SHA
alias cherry-pit
usage: git cherry-pit <SHA-to-remove>
Include some changes as part of a previous commit
Imagine we realized that we should have included a change (perhaps deleting some old comments) as part of a previous "cleaning commit"
Of course, we will have to change history to do so, with rebase
:
Stash method
Strategy: stash the desired changes, reset back to a commit, pop those changes and amend commit, then complete rebase
- Use
git stash
to store the changes you want to add. - Use
git rebase -i HEAD~10
(or however many commits back you want to see). - Mark the commit in question (a0865...) for edit by changing the word pick at the start of the line into edit. Don't delete the other lines as that would delete the commits.
- Save the rebase file, and git will drop back to the shell and wait for you to fix that commit.
- Pop the stash by using
git stash pop
- Add your file with
git add <file>
. - Amend the commit with
git commit --amend --no-edit
. - Do a
git rebase --continue
which will rewrite the rest of your commits against the new one. - Repeat from step 2 onwards if you have marked more than one commit for edit.
Autosquash method
- Stage the changes that we want to include in a previous commit
- Create a new commit with an identical message of the original commit with the following command
git commit -c <ORIGINAL-COMMIT-SHA>
- pro-tip: use `HEAD~2` syntax for relative
- With the editor open, prepend the name of the new commit with
fixup!
(orsquash!
if we want to edit the commit message)fixup! refactor: clean up payment methods
- run
git rebase -i --autosquash <SHA>
, where<SHA>
is the commit immediately before the original commit we want to amend. - Git will detect the
fixup!
directive, and will look for the commit with the same message. With this information, Git will perform the squash directive (combining the commits), and then continue to rebase the rest of the commits
fixup (manual)
- make the commit
git commit -m fixup
- run
git rebase -i
- move the commit, and squash it into an earlier one
git fixup alias
fixup = "!fn() { git commit --fixup ${1} && GIT_EDITOR=true git rebase --autosquash -i ${1}^; }; fn"
// then add staged changes to commit 3 before HEAD:
git fixup HEAD~3