Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between git push and git push -f

Tags:

git

Please can someone help me explain the difference between git push and git push -f? I do not know which of them is recommended to push your files on github

like image 635
Cash Dogg Avatar asked Dec 02 '22 11:12

Cash Dogg


1 Answers

git push -f is short for git push --force. It forces a push when otherwise git would reject your git push because you changed your repo history in your pushing repository. (EDIT: But that is only half the story. Forced pushes also let you overwrite someone else's commits which have been pushed after your last pull.) Use case:

  1. Your repositories are at commit A. You change something locally and commit it (commit B).
  2. You push B to your remote repo. Both are at B now.
  3. You decide to change something again and replace your commit B by commit B'.
  4. You want to push your commit B' to the remote repository. A plain git push will not work. You can now remove commit B from your remote repo and push B' instead with git push --force.

This is ok, if only you are using that remote repo (Or no one else did a pull between B and B'). So, no, not a good idea on github.

Hope that helps.

(EDIT: You can also check the GIT documentation or the GitHub help)

like image 139
nCessity Avatar answered Dec 21 '22 16:12

nCessity