Just do:

git push origin <your_branch_name> --force

or if you have a specific repo:

git push https://git.... --force

This will delete your previous commit(s) and push your current one.

It may not be proper, but if anyone stumbles upon this page, thought they might want a simple solution...

Short flag

Also note that -f is short for --force, so

git push origin <your_branch_name> -f

will also work.

Answer from Katie on Stack Overflow
🌐
Reddit
reddit.com › r/git › how can i "force commit" the changes i'm making? i don't want to merge...
r/git on Reddit: How can I "force commit" the changes I'm making? I don't want to merge...
May 10, 2016 -

Hello! I have a repository in GitHub, and I'm the only one working on it. I use it to be able to work at my home and office computers.

For some reason (stuff like this happens when I forget to send my work to GitHub sometimes), I cannot commit right now. I get the "hint: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes (e.g., 'git pull ...') before pushing again." Error. But I don't want to merge anything:

All I want is for Git to totally ignore what is in the remote repository, and replace it with what I have at the moment in my computer, because I am 100% sure that's what I want to keep!

Can you please help me do this? Any help appreciated!

Edit: As is obvious, I am pretty ignorant in Git. My workflow for working on two computers is:

  • Begin session with git pull

  • Work on the files (I don't even do 'branches')

  • when finished, I run the shell script

    echo "Insert git commit comments"

    read COMM

    git add -A

    git commit -m "$COMM"

    git push

  • it works fine, and no, Dropbox is no good for me (I have reasons). :)

Top answer
1 of 12
3320

Just do:

git push origin <your_branch_name> --force

or if you have a specific repo:

git push https://git.... --force

This will delete your previous commit(s) and push your current one.

It may not be proper, but if anyone stumbles upon this page, thought they might want a simple solution...

Short flag

Also note that -f is short for --force, so

git push origin <your_branch_name> -f

will also work.

2 of 12
327

And if push --force doesn't work you can do push --delete. Look at 2nd line on this instance:

git reset --hard HEAD~3  # reset current branch to 3 commits ago
git push origin master --delete  # do a very very bad bad thing
git push origin master  # regular push

But beware...

Never ever go back on a public git history!

In other words:

  • Don't ever force push on a public repository.
  • Don't do this or anything that can break someone's pull.
  • Don't ever reset or rewrite history in a repo someone might have already pulled.

Of course there are exceptionally rare exceptions even to this rule, but in most cases it's not needed to do it and it will generate problems to everyone else.

Do a revert instead.

And always be careful with what you push to a public repo. Reverting:

git revert -n HEAD~3..HEAD  # prepare a new commit reverting last 3 commits
git commit -m "sorry - revert last 3 commits because I was not careful"
git push origin master  # regular push

In effect, both origin HEADs (from the revert and from the evil reset) will contain the same files.


edit to add updated info and more arguments around push --force

Consider pushing force with lease instead of push, but still prefer revert

Another problem push --force may bring is when someone push anything before you do, but after you've already fetched. If you push force your rebased version now you will replace work from others.

git push --force-with-lease introduced in the git 1.8.5 (thanks to @VonC comment on the question) tries to address this specific issue. Basically, it will bring an error and not push if the remote was modified since your latest fetch.

This is good if you're really sure a push --force is needed, but still want to prevent more problems. I'd go as far to say it should be the default push --force behaviour. But it's still far from being an excuse to force a push. People who fetched before your rebase will still have lots of troubles, which could be easily avoided if you had reverted instead.

And since we're talking about git --push instances...

Why would anyone want to force push?

@linquize brought a good push force example on the comments: sensitive data. You've wrongly leaked data that shouldn't be pushed. If you're fast enough, you can "fix"* it by forcing a push on top.

* The data will still be on the remote unless you also do a garbage collect, or clean it somehow. There is also the obvious potential for it to be spread by others who'd fetched it already, but you get the idea.

Discussions

I accidentally force pushed to my repo
We can see from the reflog above ... sometimes called “SHA”) for that commit. So in order to restore test-branch to the state we want, we can issue the command: ... Then we can force push again to restore the repository on GitHub to where it was before.... More on github.com
🌐 github.com
8
60
[Git] How to 'git reset' a remote branch?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
13
5
February 12, 2024
Resetting Git Repo to Before First Commit
rm -fr .git git init ... git commit More on reddit.com
🌐 r/gitlab
15
1
July 3, 2024
Our team has been forced into using git cherry-pick in order to promote changes to other environments, and it's not going well, any alternatives?
First option would be protected branches. Ban direct pushes and only enable changes via merge requests. Another good option would be feature branches. Third option.. environment specific release tags. I tried this recently and it worked for me. More on reddit.com
🌐 r/devops
68
127
March 20, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-force-commit-in-git
How to Force Commit in Git? - GeeksforGeeks
June 4, 2024 - The primary commands associated with force commits are git commit --amend, git push --force, and git push --force-with-lease.
🌐
Git
git-scm.com › docs › git-commit
Git - git-commit Documentation
Note that we deliberately chose not to re-code the commit log message when a commit is made to force UTF-8 at the commit object level, because re-coding to UTF-8 is not necessarily a reversible operation.
🌐
Medium
noaabarki.medium.com › dont-underestimate-the-push-force-5cba944a246d
Don’t Underestimate the push — force | by Noaa Barki | Medium
January 24, 2020 - Your only option is to fight fire with fire and push — force this commit back to the branch on top of the bad one: ... Imagine working on a feature branch, you pulled some changes, created a few commits and completed your part of the feature and pushed your changes up to the main repository. Then you squashed the commits into one, using git rebase — i and pushed again using push — force.
Find elsewhere
🌐
Git Tower
git-tower.com › learn › git faq › how to create and push an empty commit in git
How to Create and Push an Empty Commit in Git | Learn Version Control with Git
1 month ago - Creating an empty commit in Git is a very simple process. All you need to do is add the --allow-empty flag to the git commit command.
🌐
Claude
code.claude.com › docs › en › settings
Claude Code settings - Claude Code Docs
April 6, 2026 - Claude Code adds attribution to git commits and pull requests.
🌐
Git
git-scm.com › docs › git-cherry-pick
Git - git-cherry-pick Documentation
Note also, that use of this option only keeps commits that were initially empty (i.e. the commit recorded the same tree as its parent). Commits which are made empty due to a previous commit will cause the cherry-pick to fail. To force the inclusion of those commits, use --empty=keep.
🌐
Datree
datree.io › resources › git-force-pull
Git Force Pull Tutorial - Datree.io | Datree.io
For the Googlers: there’s no such thing as `git force pull` -- instead, you want to run these two commands: ... Together, these commands will discard your local changes (saving them for later) and replace them with the latest commit from your remote branch.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-push-an-empty-commit-with-git
How to Push an Empty Commit in Git
September 1, 2024 - By Chaitanya Prabuddha In this article, we will discuss how to push a commit in Git without making any changes. Git makes this process of pushing an empty commit super simple. It's like pushing a regular commit, except that you add the --allow-empty ...
🌐
JetBrains
jetbrains.com › help › rider › Commit_and_push_changes.html
Commit and push changes to Git repository | JetBrains Rider Documentation
2 weeks ago - Under the hood, when you choose to force push, JetBrains Rider performs the git push --force-with-lease operation which is a safer option that helps you ensure you do not overwrite someone else's commits (refer to git push for more details on the push options).
Top answer
1 of 1
11

If I’m understanding correctly, you’re talking about a scenario like the following:

  1. You have a repo named ben3eee/some-repo
  2. You create a few commits on branch some-branch
  3. You push some-branch to ben3eee/some-repo on GitHub using git push
  4. You squash the commits into one using git rebase -i
  5. You  force push some-branch to ben3eee/some-repo on GitHub using git push -f
  6. You now want to restore some-branch to the way it was before step #4

The great thing about Git though is that it does it’s very best to never lose data, so the version of the repository before step #4 is still available as long as too much time hasn’t passed. The exact definition of “too much time” is kind of fuzzy and depends on how many other changes you’ve made between step #4 and step #6. For this example, I’m going to assume that you realize your mistake right away and no other actions were taken other than the ones in the list above.

The command that can help is called git reflog. You can enter git reflog and see all of the actions that have changed your local repository, including switching branches and rebases, going back quite a ways. I’ve created a simple reflog that shows the scenario I described above:

b46bfc65e (HEAD -> test-branch) HEAD@{0}: rebase -i (finish): returning to refs/heads/test-branch
b46bfc65e (HEAD -> test-branch) HEAD@{1}: rebase -i (squash): a
dd7906a87 HEAD@{2}: rebase -i (squash): # This is a combination of 2 commits.
a3030290a HEAD@{3}: rebase -i (start): checkout refs/heads/master
0c2d866ab HEAD@{4}: commit: c
6cab968c7 HEAD@{5}: commit: b
a3030290a HEAD@{6}: commit: a
c9c495792 (origin/master, origin/HEAD, master) HEAD@{7}: checkout: moving from master to test-branch
c9c495792 (origin/master, origin/HEAD, master) HEAD@{8}: pull: Fast-forward

You can see at HEAD@{7} I performed a checkout moving from master to test-branch. I then created three commits, “a”, “b” and “c”. Then I rebased them, arriving at HEAD@{0}. The notation HEAD@{number} is the position of HEAD at “number” changes ago. So HEAD@{0} is HEAD where HEAD is now and HEAD@{4} is HEAD four steps ago. We can see from the reflog above that HEAD@{4} is where we need to go in order to restore the branch to where it was before the rebase and 0c2d866ab is the commit ID (also sometimes called “SHA”) for that commit.  So in order to restore test-branch to the state we want, we can issue the command:

git reset --hard HEAD@{4}

Then we can force push again to restore the repository on GitHub to where it was before.

🌐
Git Tower
git-tower.com › blog › force push in git - everything you need to know
Force Push in Git - Everything You Need to Know | Tower Blog
August 18, 2021 - If there is a linear path, the command succeeds. When it’s not allowed (most likely because the remote repo looked different from yours, apart from your new commits), the command fails. The command will always succeed, however, if you resort to the --force flag.
🌐
Graphite
staging-graphite-splash.vercel.app › guides › git-force-push
How to Git force push
Following the instructions, run git pull to integrate the most recent remote changes then try your force push again. To force push changes from one branch to another (e.g., from feature-branch to main), you'd check out to the branch you want to push into and then force push the other branch's commits:
🌐
Git
git-scm.com › docs › git-rebase
Git - git-rebase Documentation
Instead of using the current time as the committer date, use the author date of the commit being rebased as the committer date. This option implies --force-rebase.
🌐
Readthedocs
isshub.readthedocs.io › en › develop › git › commits › 4467a1f65e3ad02747eb4f433e3d7b197e598aec.html
style(git): Force format of git commit messages — IssHub documentation
To instruct git to use it, run:: + + git config commit.template .gitmessage + + +To check if the last commit is valid, you can run:: + + make check-commit + +If you want to validate an other commit message than the last one, check `ci/check_commit_message.py -h` + + '''''' Coding ''''''
🌐
Git
git-scm.com › book › en › v2 › Git-Basics-Tagging
Git - Tagging
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false ...