How to overwrite commit history on a remote repository?

git push --force

To overwrite commit history on a remote repository with your local commit history use git push --force. This means losing remote commits, so use this command with care.

Layer 1
Terminal Example
git status
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

git push
To github.com:githint/playground-remote.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'git@github.com:githint/playground-remote.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

git push --force
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 277 bytes | 277.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:githint/playground-remote.git
 + 427bea6...5d6b450 main -> main (forced update)

See git push to learn more about it.

Last modified on November 30, 2021.

You might also like