You should be able to force your local revision to the remote repo by using
git push -f <remote> <branch>
(e.g. git push -f origin master). Leaving off <remote> and <branch> will force push all local branches that have set --set-upstream.
Just be warned, if other people are sharing this repository their revision history will conflict with the new one. And if they have any local commits after the point of change they will become invalid.
Update: Thought I would add a side-note. If you are creating changes that others will review, then it's not uncommon to create a branch with those changes and rebase periodically to keep them up-to-date with the main development branch. Just let other developers know this will happen periodically so they'll know what to expect.
Update 2: Because of the increasing number of viewers I'd like to add some additional information on what to do when your upstream does experience a force push.
Say I've cloned your repo and have added a few commits like so:
D----E topic
/
A----B----C development
But later the development branch is hit with a rebase, which will cause me to receive an error like so when I run git pull:
Unpacking objects: 100% (3/3), done. From <repo-location> * branch development -> FETCH_HEAD Auto-merging <files> CONFLICT (content): Merge conflict in <locations> Automatic merge failed; fix conflicts and then commit the result.
Here I could fix the conflicts and commit, but that would leave me with a really ugly commit history:
C----D----E----F topic
/ /
A----B--------------C' development
It might look enticing to use git pull --force but be careful because that'll leave you with stranded commits:
D----E topic A----B----C' development
So probably the best option is to do a git pull --rebase. This will require me to resolve any conflicts like before, but for each step instead of committing I'll use git rebase --continue. In the end the commit history will look much better:
D'---E' topic
/
A----B----C' development
Update 3: You can also use the --force-with-lease option as a "safer" force
push, as mentioned by Cupcake in his
answer:
Answer from Trevor Norris on Stack OverflowForce pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own:
git push <remote> <branch> --force-with-leaseYou can learn more details about how to use
--force-with-leaseby reading any of the following:
git pushdocumentation- Git: How to ignore fast forward and revert origin [branch] to earlier commit?
You should be able to force your local revision to the remote repo by using
git push -f <remote> <branch>
(e.g. git push -f origin master). Leaving off <remote> and <branch> will force push all local branches that have set --set-upstream.
Just be warned, if other people are sharing this repository their revision history will conflict with the new one. And if they have any local commits after the point of change they will become invalid.
Update: Thought I would add a side-note. If you are creating changes that others will review, then it's not uncommon to create a branch with those changes and rebase periodically to keep them up-to-date with the main development branch. Just let other developers know this will happen periodically so they'll know what to expect.
Update 2: Because of the increasing number of viewers I'd like to add some additional information on what to do when your upstream does experience a force push.
Say I've cloned your repo and have added a few commits like so:
D----E topic
/
A----B----C development
But later the development branch is hit with a rebase, which will cause me to receive an error like so when I run git pull:
Unpacking objects: 100% (3/3), done. From <repo-location> * branch development -> FETCH_HEAD Auto-merging <files> CONFLICT (content): Merge conflict in <locations> Automatic merge failed; fix conflicts and then commit the result.
Here I could fix the conflicts and commit, but that would leave me with a really ugly commit history:
C----D----E----F topic
/ /
A----B--------------C' development
It might look enticing to use git pull --force but be careful because that'll leave you with stranded commits:
D----E topic A----B----C' development
So probably the best option is to do a git pull --rebase. This will require me to resolve any conflicts like before, but for each step instead of committing I'll use git rebase --continue. In the end the commit history will look much better:
D'---E' topic
/
A----B----C' development
Update 3: You can also use the --force-with-lease option as a "safer" force
push, as mentioned by Cupcake in his
answer:
Force pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own:
git push <remote> <branch> --force-with-leaseYou can learn more details about how to use
--force-with-leaseby reading any of the following:
git pushdocumentation- Git: How to ignore fast forward and revert origin [branch] to earlier commit?
You want to force push
What you basically want to do is to force push your local branch, in order to overwrite the remote one.
If you want a more detailed explanation of each of the following commands, then see my details section below. You basically have 4 different options for force pushing with Git:
git push <remote> <branch> -f
git push origin master -f # Example
git push <remote> -f
git push origin -f # Example
git push -f
git push <remote> <branch> --force-with-lease
If you want a more detailed explanation of each command, then see my long answers section below.
Warning: force pushing will overwrite the remote branch with the state of the branch that you're pushing. Make sure that this is what you really want to do before you use it, otherwise you may overwrite commits that you actually want to keep.
Force pushing details
Specifying the remote and branch
You can completely specify specific branches and a remote. The -f flag is the short version of --force
git push <remote> <branch> --force
git push <remote> <branch> -f
Omitting the branch
When the branch is omitted, Git will figure it out based on your config settings. In Git versions after 2.0, a new repo will have default settings to push the currently checked-out branch:
git push <remote> --force
while prior to 2.0, new repos will have default settings to push multiple local branches. The settings in question are the remote.<remote>.push and push.default settings (see below).
Omitting the remote and the branch
When both the remote and the branch are omitted, the behavior of just git push --force is determined by your push.default Git config settings:
git push --force
As of Git 2.0, the default setting,
simple, will basically just push your current branch to its upstream remote counter-part. The remote is determined by the branch'sbranch.<remote>.remotesetting, and defaults to the origin repo otherwise.Before Git version 2.0, the default setting,
matching, basically just pushes all of your local branches to branches with the same name on the remote (which defaults to origin).
You can read more push.default settings by reading git help config or an online version of the git-config(1) Manual Page.
Force pushing more safely with --force-with-lease
Force pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own:
git push <remote> <branch> --force-with-lease
You can learn more details about how to use --force-with-lease by reading any of the following:
git pushdocumentation- Git: How to ignore fast forward and revert origin [branch] to earlier commit?
How do I push a new local branch to a remote Git repository and track it too? - Stack Overflow
Am I using git push --force the right way?
How do I properly force a Git push? - Stack Overflow
git push force on my branch
Videos
In Git 1.7.0 and later, you can checkout a new branch:
git checkout -b <branch>
Edit files, add and commit. Then push with the -u (short for --set-upstream) option:
git push -u origin <branch>
Git will set up the tracking information during the push.
If you are not sharing your repo with others, this is useful to push all your branches to the remote, and --set-upstream tracking correctly for you:
git push --all -u
(Not exactly what the OP was asking for, but this one-liner is pretty popular)
If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.
Hello everyone,
I would like to know if I'm using git push --force the right way. Here is my scenario:
-
I branched off master (let's call my branch dev1) which I did some work on and then pushed up remotely
-
A ton of work has been done on `master` I've been working on dev1 locally
-
I want my local dev1 branch to have all the updated changes that master has so I rebased
-
At this point, my local dev1 is way out of sync with remote dev1. Git is saying if I want to sync them up then I need to do a git pull (but I definitely don't want to pull the changes down from the remote branch to my newer, local branch).
Assuming no one else works on dev1, am I supposed to git push --force to sync local `dev1` to remote `dev1`? If not, what are my alternatives?
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.
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
forcepush on a public repository. - Don't do this or anything that can break someone's
pull. - Don't ever
resetorrewritehistory 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.