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

"force" git commit on local to external via push? - Stack Overflow
Possible Duplicate: Github first push problem… how to merge remote changes? My external repo is fairly outdated. I want to push all my local changes up stream. However, when I attempt to... More on stackoverflow.com
🌐 stackoverflow.com
[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 Tower
git-tower.com › learn › git faq › how to force push in git
How to force push in Git | Learn Version Control with Git
3 weeks ago - Only when you are up-to-date will you be able to push your own new commits to the remote. The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history.
🌐
Evil Martians
evilmartians.com › blog › git push --force and how to deal with it—martian chronicles, evil martians’ team blog
git push --force and how to deal with it—Martian Chronicles, Evil Martians’ team blog
July 30, 2024 - So all you need is to… force push (we’re fighting fire with fire!) this commit back to the main branch, on top of the bad one. ... And congratulations! You’ve saved the day. Just be sure to learn from your mistakes! So, next, let’s say that just before you performed git push --force, someone had closed a bunch of pull requests, and so main now looks nothing like your local copy.
🌐
Atlassian
atlassian.com › git › tutorials › syncing › git-push
Git Push | Atlassian Git Tutorial
Git prevents you from overwriting the central repository’s history by refusing push requests when they result in a non-fast-forward merge. So, if the remote history has diverged from your history, you need to pull the remote branch and merge it into your local one, then try pushing again. This is similar to how SVN makes you synchronize with the central repository via svn update before committing a changeset. The --force flag overrides this behavior and makes the remote repository’s branch match your local one, deleting any upstream changes that may have occurred since you last pulled.
Find elsewhere
🌐
Graphite
graphite.com › guides › git-force-push
How to Git force push - Graphite
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:
🌐
Linux Hint
linuxhint.com › push-force-changes-in-git
How to Push Force Changes in Git – Linux Hint
To push force changes in Git, clone the remote repo and update its content. Then, commit changes and run the “$ git push origin main --force” command.
🌐
Medium
noaabarki.medium.com › dont-underestimate-the-push-force-5cba944a246d
Don’t Underestimate the push — force | by Noaa Barki | Medium
January 24, 2020 - One of the common mistakes using this command is when Bob forgets to update (git pull) his local tracked branch, in this case, using the force might cause Bob a lot of trouble. you must wonder why? Well… force pushes the changes with no regard to the state of the tracked branch, therefore commits might get lost in the process.
🌐
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.
🌐
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.
🌐
GitKraken
gitkraken.com › home › learn › problems & solutions › git push force
How to Git Push Force | Solutions to Git Problems
August 5, 2022 - Git push force overwrites the remote repository to match exactly what your local repo looked like when you ran the command. This means you need to make sure your local repository is entirely up-to-date with the latest changes from the remote ...
🌐
JetBrains
jetbrains.com › help › rider › Commit_and_push_changes.html
Commit and push changes to Git repository | JetBrains Rider Documentation
1 week 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).
🌐
DataCamp
datacamp.com › tutorial › git-push-force
Git Push Force: How it Works and How to Use it Safely | DataCamp
July 24, 2025 - When you do this, your local branch ... protects the remote branch from loss of work. Using git push --force tells Git to skip the safety check and overwrite the remote branch with what is in your local branch....
🌐
Scaler
scaler.com › home › topics › git › git push force
Git Push Force- Scaler Topics
May 4, 2023 - This is because git allows you to push the changes only if the history of commits on both the local branch and the remote branch of the repository is the same and up to date. It will not allow you to push if the commits on any end are ahead of another end. However, there might arise conditions when you actually want to overwrite the commit history of the remote repo with the commit history of your local repo. Then, for this, we use the concept of force pushing.
🌐
GitHub
github.com › simonw › til › blob › main › git › remove-commit-and-force-push.md
til/git/remove-commit-and-force-push.md at main · simonw/til
I didn't want this to stick around in the history forever, and no-one else was pulling from the repo, so I decided to use force push to remove the rogue commit entirely. I figured out the commit hash of the previous version that I wanted to restore and ran: git reset --hard 1909f93 ·
Author   simonw
🌐
GoLinuxCloud
golinuxcloud.com › home › devops › git push force explained [with examples]
git push force Explained [With Examples] | GoLinuxCloud
September 14, 2021 - git push force is used to overwrite commits on the remote repository. Alternatively we can use git force with leave to make sure commits are not overwritten
🌐
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 › book › en › v2 › Customizing-Git-An-Example-Git-Enforced-Policy
8.4 Customizing Git - An Example Git-Enforced Policy
To begin, you should check your commit message just before each commit is recorded, so you know the server won’t reject your changes due to badly formatted commit messages. To do this, you can add the commit-msg hook. If you have it read the message from the file passed as the first argument ...