1864

I've set up a remote non-bare "main" repo and cloned it to my computer. I made some local changes, updated my local repository, and pushed the changes back to my remote repo. Things were fine up to that point.

Now, I had to change something in the remote repo. Then I changed something in my local repo. I realized that the change to the remote repo was not needed. So I tried to git push from my local repo to my remote repo, but I got an error like:

To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of git push --help for details.

I thought that probably a

git push --force

would force my local copy to push changes to the remote one and make it the same. It does force the update, but when I go back to the remote repo and make a commit, I notice that the files contain outdated changes (ones that the main remote repo previously had).

As I mentioned in the comments to one of the answers:

[I] tried forcing, but when going back to master server to save the changes, i get outdated staging. Thus, when i commit the repositories are not the same. And when i try to use git push again, i get the same error.

How can I fix this issue?

7
  • 4
    You will soon (git1.8.5, Q4 2013) be able to do a git push -force more carefully.
    – VonC
    Commented Sep 10, 2013 at 8:42
  • 1
    Related: Force git to overwrite remote files on push.
    – user456814
    Commented Jul 15, 2014 at 21:42
  • 14
    As I detail in my own answer, git push --force is indeed another valid way to force push, and will push branches just as well as git push origin master --force with Git's default push.default config settings, though which branches specifically get pushed differs between Git versions prior to 2.0 versus after 2.0.
    – user456814
    Commented Aug 5, 2014 at 17:51
  • 8
    git push --force works fine these days, FWIW...
    – rogerdpack
    Commented Apr 10, 2018 at 20:25
  • 11
    git push --force-with-lease works even better :), it will refuse to update a branch unless it is the state that you expect. (see developer.atlassian.com/blog/2015/04/force-with-lease)
    – spoorcc
    Commented Jan 14, 2019 at 7:58

12 Answers 12

3227
+150

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.

11
  • 78
    You can use git push origin +master instead, which allow you push multiple refspecs without forcing them all.
    – nickgrim
    Commented Nov 20, 2013 at 10:36
  • 12
    Be aware that, if you accidentally do just git push --force, you might end up messing you master branch (depending on your push default behavior).. Which might suck.. a bit.. :D
    – Jeewes
    Commented Aug 6, 2014 at 7:18
  • 21
    @Jeewes starting with Git version 2.0, the default behavior of git push --force is basically to force push the currently checked-out branch to its remote-counter part, so if you have the master branch checked out, then it's identical to git push origin master --force. It'll be different if you're using the matching setting for push.default, which is the default for Git versions prior to 2.0. matching pushes all locals branches to remote ones that have the same name, so force pushing then could definitely be not what you want to do...
    – user456814
    Commented Aug 12, 2014 at 14:22
  • 3
    push -f is good but not recoomended for master since most corporate repositories have -f disabled for master. the merge -s ours worked for me
    – mihai
    Commented May 26, 2016 at 21:27
  • 2
    Don't forget that your remote repo permissions (read: Azure DevOps) for your account, might not grant permission to force push. In that case, you'll need an administrator to set your permissions. Commented Apr 27, 2021 at 17:30
323

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.

11
  • 1
    The problem, @rogerdpack, isn't if it's doable. It is. But it can sum up to a big disaster. The more someone do it (force push) and the less often you update (pull) from the public repo, the bigger the disaster. It can dismantle the world as you know it!!!111 At least the world comprising of that particular repository.
    – cregox
    Commented Sep 24, 2013 at 19:14
  • 6
    If you have sensitive data, force push it
    – linquize
    Commented Nov 26, 2013 at 4:58
  • 3
    @Cawas: I think he means that if you are trying to remove sensitive data from the repository, then you want to rewrite history. If you revert, the sensitive data is still there in the earlier commit. That said, if someone else has already pulled from the repository, then rewriting history won't help you prevent them from accessing the sensitive data - it's already too late at that point. Commented Dec 12, 2013 at 9:38
  • 3
    git push origin master --delete # do a very very bad bad thing git push origin master # regular push this actually solved my problem perfectly (on a repo with only me and my friend). maybe it's wrong for public repos but for a private one this is a life saver. Commented Jan 26, 2014 at 23:44
  • 7
    this happens automatically with some repo managers, a.k.a. auto-squash etc. force pushing after finishing a feature branch to reduce commits is common and expected. Commented Jan 11, 2019 at 20:37
29

If I'm on my local branch A, and I want to force push local branch B to the origin branch C I can use the following syntax:

git push --force origin B:C
2
  • 3
    I found out that even I'm on my local branch B, I still need to do git push --force origin B:C. In my case, it seems that git push --force origin C will only push from local master to remote C branch, regardless of which branch I'm currently on. git version 2.3.8 (Apple Git-58)
    – Weishi Z
    Commented Oct 10, 2015 at 5:28
  • 1
    This helped me when moving a repository from the previous master branch to the new main branch. Just used master:main and it worked as I needed!
    – Ícaro
    Commented Jan 4, 2022 at 19:58
27

use this following command:

git push -f origin master
3
  • 3
    Maybe give some more explanation about why this answer is preferable to the other ones, and what makes it different.
    – Adam
    Commented Apr 10, 2017 at 15:41
  • 1
    oh ,sorry for inconvenience , I was having the same problem and this command solve it , i thought i should share it . Commented Apr 11, 2017 at 14:30
  • 25
    It's just the same as the others, you just changed the position of the -f flag...
    – svelandiag
    Commented May 31, 2017 at 15:38
20

First of all, I would not make any changes directly in the "main" repo. If you really want to have a "main" repo, then you should only push to it, never change it directly.

Regarding the error you are getting, have you tried git pull from your local repo, and then git push to the main repo? What you are currently doing (if I understood it well) is forcing the push and then losing your changes in the "main" repo. You should merge the changes locally first.

4
  • 1
    yes i tried a pull but i'm losing losing data because of that pull. I want to make my main repos as my local is, without first updating from the main.
    – Spyros
    Commented Apr 1, 2011 at 6:28
  • 3
    In that case use git push -f, but then if you change your main repo again, you have to go back to your local repo and git pull, so that it gets in sync with the latest changes. Then you can do your work, and push again. If you follow this "push-pull" workflow, you won't get the kind of error you were complaining about.
    – ubik
    Commented Apr 1, 2011 at 17:22
  • yeah, i understand that this was my fault :/ I will try that and get back in a little while thanx
    – Spyros
    Commented Apr 1, 2011 at 18:56
  • 1
    tried forcing, but when going back to master server to save the changes, i get outdated staging. Thus, when i commit the repositories are not the same. And when i try to use git push again, i get the same error.
    – Spyros
    Commented Apr 2, 2011 at 18:11
14

git push --force would do the job, although git push --force-with-lease is a safer command

git push --force overwrites the remote branch, while git push --force-with-lease only overwrites the remote branch if your local copy is aware of all of the commits on the remote branch. This difference makes it significantly more difficult to destroy someone else’s changes on the project.

10

I would really recommend to:

  • push only to the main repo

  • make sure that main repo is a bare repo, in order to never have any problem with the main repo working tree being not in sync with its .git base. See "How to push a local git repository to another computer?"

  • If you do have to make modification in the main (bare) repo, clone it (on the main server), do your modification and push back to it

In other words, keep a bare repo accessible both from the main server and the local computer, in order to have a single upstream repo from/to which to pull/pull.

2
10

I had the same question but figured it out finally. What you most likely need to do is run the following two git commands (replacing hash with the git commit revision number):

git checkout <hash>
git push -f HEAD:master
8

Using --force-with-lease might be a better option:

git push <remote> <branch> --force-with-lease

It makes sure (with some caveats - read below) no one has updated the branch before you modified it, so you don't overwrite their changes.


Read more on Git documentation:

This option allows you to say that you expect the history you are updating is what you rebased and want to replace. If the remote ref still points at the commit you specified, you can be sure that no other people did anything to the ref. It is like taking a "lease" on the ref without explicitly locking it, and the remote ref is updated only if the "lease" is still valid.

--force-with-lease alone, without specifying the details, will protect all remote refs that are going to be updated by requiring their current value to be the same as the remote-tracking branch we have for them.

A general note on safety: supplying this option without an expected value, i.e. as --force-with-lease or --force-with-lease=<refname> interacts very badly with anything that implicitly runs git fetch on the remote to be pushed to in the background, e.g. git fetch origin on your repository in a cronjob.

The protection it offers over --force is ensuring that subsequent changes your work wasn’t based on aren’t clobbered, but this is trivially defeated if some background process is updating refs in the background. We don’t have anything except the remote tracking info to go by as a heuristic for refs you’re expected to have seen & are willing to clobber.

If your editor or some other system is running git fetch in the background for you a way to mitigate this is to simply set up another remote

2
  • I wouldn't way that's what --force-with-lease does. If you do a fetch (not pull) before you push with --force-with-lease, git won't stop you from overwriting updates from other people. Commented May 9 at 10:50
  • 1
    @DanielHjertholm, I added a footnote with some information on it. Thank you. Commented May 15 at 13:40
7

if you are authenticating with Github access token, try this:

  • git remote set-url origin https://[email protected]/UserName/ProjectName

  • git push --force --set-upstream origin master

5

This was our solution for replacing master on a corporate gitHub repository while maintaining history.

push -f to master on corporate repositories is often disabled to maintain branch history. This solution worked for us.

git fetch desiredOrigin
git checkout -b master desiredOrigin/master // get origin master

git checkout currentBranch  // move to target branch
git merge -s ours master  // merge using ours over master
// vim will open for the commit message
git checkout master  // move to master
git merge currentBranch  // merge resolved changes into master

push your branch to desiredOrigin and create a PR

3

My issue was--I did:

git checkout arbitrary_commit
git push origin master --force

which was not the right thing to do. Instead I had to:

git reset HEAD~3
git push origin master --force

Note: the number 3 is just an example. You should put your own number.

Not the answer you're looking for? Browse other questions tagged or ask your own question.