Git
Commit Amendments
Remote Repository
Git Commands
Programming

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:

bash
git commit --amend

This opens your configured editor so you can change the message. To keep the message as it is and only update the contents:

bash
git commit --amend --no-edit

Pushing when the commit was not pushed yet

The remote branch has no conflicting history, so a regular push works:

bash
   git push

Force pushing when the commit was already pushed

Git rejects a normal push because the histories diverged. Force push with care:

bash
   git push --force

or, more safely:

bash
   git push --force-with-lease

--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:

bash
git rebase -i HEAD~3  # Adjust the number for how many commits to include.

Follow the instructions in the editor to amend specific commits.

Command reference

ActionGit CommandNotes
Stage changesgit add . or specific file pathsPrepares files for commit
Amend last commitgit commit --amend [--no-edit]Optionally skip editing the commit message with --no-edit
Push normallygit pushUse if the commit was not already pushed
Force pushgit push --forceRequired if the commit was already pushed; use with caution
Safer force pushgit push --force-with-leaseChecks the latest remote state before pushing, reducing risks
Interactive rebasegit rebase -i HEAD&#126;<num>Replace <num> with the number of last commits to include in rebase

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions