Editing the latest commit¶
To edit the latest commit on the server, you can just execute git commit --amend
: this will recommit any changes that were made after the commit, if some reasonable conditions are satisfied.
Recommit: general case¶
In a general way, you can easily use git rebase --interactive
to recommit. It will look like this:
$ git rebase -i commit^ [The editor lists all available commits. Replace "pick" with "edit" before the commit you want to edit. Save and exit. ] You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue [The current commit is now the commit you wanted to edit.] $ vim correct/file # make any changes $ git commit --am correct/file $ git rebase --continue
The same can be done manually.
Manual recommit, without git-rebase¶
Sometimes you would not want to recommit in case of minor changes. We suggest the following rule of using git push --force: commits that were published before the latest public tag, or before the commit bound for compilation, should not be replaced (otherwise the next compilation will be turned down if you do not mend the situation).
Suppose that this rule is respected, but you do not want to commit minor or trivial corrections separately. Let us see how it works, for instance, with editing the last but two commit.
First of all, save the current state to the temporary branch:
$ git branch save
Reset to the last but one commit:
$ git reset --hard HEAD^^
Now edit this last but two commit that has become the current one:
$ vim ... $ git commit -a --am
Roll on the last but one and the last commits from the saved branch over the current commit:
$ git cherry-pick -r save^ $ git cherry-pick -r save
You now have to delete the temporary branch:
$ git branch -D save
You can finally execute:
$ git push --force git.alt