12

GIT - FORCE PUSH

Can anyone tell me about when to use git push and when to git push -f with an example?

raj keviv
  • 149
  • 1
  • 2
  • 11
  • Could you let us know what situation you are currently facing? The general answer about when to force or not force is a bit broad. – Tim Biegeleisen Jun 21 '17 at 08:57
  • 2
    In general, you force push `git push -f` when you must change the git history of the branch. Example is when you rebase your local branch with another branch, then you must force push. Use `git push` when your local branch has the same history as the remote branch (meaning local branch is the same as remote branch + new commits) – Dat Nguyen Jun 21 '17 at 08:59
  • 7
    In general, if you're a newbie in git, I would strongly suggest you **never** use git force until you fully understand the consequences. Too many questions here on Stack Overflow get a very happy and glib answer "Ah, just use force push", followed by a new question from the same person somewhat later "HALP! I LOST COMMITS". In other words, don't learn **when** to use force push, instead learn **what** force push does, then decide for yourself when this makes sense. – Lasse V. Karlsen Jun 21 '17 at 09:04
  • @TimBiegeleisen I haven't use `git push -f` but i used `git push`, so i want to know the difference between them? – raj keviv Jun 21 '17 at 09:08
  • @DatNguyen Has the closest thing that answer your question, it tells you *what* force push does but it comes with a clause, "when you must change the git history of the branch", in other words, he hasn't explicitly told you when either, just given you a rule to evaluate. – Lasse V. Karlsen Jun 21 '17 at 09:08
  • @LasseV.Karlsen can you tell what the force push will do? – raj keviv Jun 21 '17 at 09:08
  • The difference is that, let's say that you commit something, and push, so now your remote also has the same commit. Then you **change** this commit, say with a "git commit --amend", which alters the commit. Now your commit is not the same as on the remote. If you now force-push, your remote will lose the original commit, and get your new one. – Lasse V. Karlsen Jun 21 '17 at 09:09
  • 2
    However, here's something else. If you pull from the remote, and start working, and a colleague of yours also work, commits, and pushes, and then you try to push, and you get a message saying you need to pull before pushing, this is because your history doesn't match the remote history. If you now do a force push, you will remove the commits your colleague made. – Lasse V. Karlsen Jun 21 '17 at 09:10
  • Ok.. is there any conditions like if i use this command(s), then i should use force push? – raj keviv Jun 21 '17 at 09:11
  • 1
    @rajkeviv After rebasing a branch on something else, you would need to force push it out to the remote. This is the biggest completely legitimate use of force push which comes to mind. But as Lasse mentioned, most of the time using force push has a bad smell. – Tim Biegeleisen Jun 21 '17 at 09:14
  • Ok thank you Lasse V. Karlsen, TimBiegeleisen, DatNguyen – raj keviv Jun 21 '17 at 09:16
  • friends what about ` --force-with-lease` ?? – raj keviv Jun 21 '17 at 09:29

1 Answers1

23

There is a case for push --force, even for beginners: when you are updating a pull request.

A pull request means

  • you fork a repo on GitHub (for instance)
  • clone it locally
  • make a branch and add some patches/new commits
  • push that branch to your fork (that you own)
  • triggers a Pull Request which notifies the owner of the original repo that you want your PR branch to be merged.

BUT: if that original repo has made new commits of its own, you need to rebase (replay your commits) on top of the updated "upstream" repo

git remote add upstream /url/original/repo
git checkout my_pr_branch
git rebase upstream/master
# test everything is still working

By rebasing, you are changing the SHA1 of your new commits: you need to replace the published (pushed) commits of your PR branch by your rebased commits:

git push --force

That will update the existing Pull Request, which will take into account the new versions of those commits.
Since you are force pushing to your own repo (the fork), and your own branch (the PR branch), you can use --force as many time as you want.


I presented force-with-lease in 2013, as a way to detect if anything happened to the remote repo you want to force push.
Note that it became more robust recently with Git 2.13.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250