Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - When to use force push [closed]

Tags:

git

GIT - FORCE PUSH

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

like image 554
raj keviv Avatar asked Jun 21 '17 08:06

raj keviv


1 Answers

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.

like image 123
VonC Avatar answered Oct 17 '22 14:10

VonC