If you found a bug in your last commit, you can simply recommit the last commit with
git commit --amend
But if you already committed another commit, how do you resubmit the commit before that one?
Note: I assume, you are aware, that you should only use --amend
if you are sure no other developer already used your commit upstream
Git commit --amend commit --amend is used to modify the most recent commit . It combines changes in the staging environment with the latest commit , and creates a new commit . This new commit replaces the latest commit entirely.
use git commit --amend to make changes, or. use git reset @~ to discard the last commit, but not the changes to the files (i.e. take you to the point you were at when you'd edited the files, but hadn't committed yet).
Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.
commit your fixup with
git commit -a -m "fix commit (this one will be shifted up one line)"
(the commit message is not important, it will be obsolete once you are finished)
Now the magic:
rebase the HEAD to the second last commit but edit the commit message before in that way, that you swap the last and the last but one line in the message editor:
git rebase -i HEAD~3
The editor will show the last 3 comits like this:
pick 2a06f16 this was the last commit pick 0dbc5ce the last-but-one commit pick 2e30418 fix commit (this one will be shifted up one line) # Rebase 011f3d0..2e30418 onto 011f3d0 # …
swap the last two lines so the commit message will look for example like this:
pick 2a06f16 this was the last commit fixup 2e30418 fix commit (this one will be shifted up one line) pick 0dbc5ce the last-but-one commit # Rebase 011f3d0..2e30418 onto 011f3d0 # …
Check your log with
git log HEAD~3
push the corrected commit-line with +
to force a new push to the already existing branch with
git push origin +branchname
Well, I landed on this page while searching for the same. Found a better way with many other options
git rebase -i HEAD~2
An editor will open up with the following details
pick 4f4f96f Added git ignore pick d01e18c Added sample Blog # Rebase 60e1cd3..d01e18c onto 60e1cd3 (2 commands) # # 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>]
press i, change pick
to r
or reword
, something like below
pick 4f4f96f Added git ignore r d01e18c Added sample Blog
PRESS esc + : + wq
Another window will open up, change the commit message
PRESS esc + : + wq
git push -f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With