How to push an amended commit to a remote Git repository
Codemia Knowledge Hub ·
If the original commit was never pushed, run a normal git push. If it was already pushed, the amended commit has a different history from the remote branch, so you must force push, preferably with git push --force-with-lease.
What amending a commit does
git commit --amend replaces the most recent commit. It is handy to:
- correct a typo in the commit message
- add forgotten files to the commit
- combine small changes into one cohesive commit
Amending a commit that has not been pushed is safe. Amending one that others already have can cause conflicts and requires a force push.
Amending the last commit
Stage the extra changes with git add, then run:
This opens your configured editor so you can change the message. To keep the message as it is and only update the contents:
Pushing when the commit was not pushed yet
The remote branch has no conflicting history, so a regular push works:
Force pushing when the commit was already pushed
Git rejects a normal push because the histories diverged. Force push with care:
or, more safely:
--force-with-lease is preferable because it checks that your local copy of the remote branch is up to date before overwriting anything.
Force pushing rewrites history on the remote and can disrupt collaborators working on the branch. Tell your team before you do it, and know exactly which commits you are replacing.
Amending older commits with interactive rebase
To amend, squash or reorder several commits, use an interactive rebase over the range:
Follow the instructions in the editor to amend specific commits.
Command reference
| Action | Git Command | Notes |
| Stage changes | git add . or specific file paths | Prepares files for commit |
| Amend last commit | git commit --amend [--no-edit] | Optionally skip editing the commit message with --no-edit |
| Push normally | git push | Use if the commit was not already pushed |
| Force push | git push --force | Required if the commit was already pushed; use with caution |
| Safer force push | git push --force-with-lease | Checks the latest remote state before pushing, reducing risks |
| Interactive rebase | git rebase -i HEAD~<num> | Replace <num> with the number of last commits to include in rebase |
Related reading
- How do I rebase while skipping a particular commit?
- How do I recover a dropped stash in Git?
- How do I remove a directory from a Git repository?
- How do I remove a single file from the staging area (undo git add)?
- How do I remove a submodule?
- How do I remove files saying old mode 100755 new mode 100644 from unstaged changes in Git?
- How do I remove msysgit's right click menu options?
- How do I remove the old history from a git repository?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.