Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the different between force push and normal push in git [duplicate]

Tags:

git

push

I want to know what is the different between force push and normal push and at what kind of situations i should do force push in git?Is it a good practice to do force push into master branch?

like image 368
Sajith Herath Avatar asked Apr 23 '17 05:04

Sajith Herath


People also ask

What Is a force push in git?

The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history. This is a rather dangerous process, because it's very easy to overwrite (and thereby lose) commits from your colleagues.

Is force push a good practice?

Force-pushing is a highly threatening and risky method if you're working in a common repository. Using this force pushing you should be able to force your local revision to the remote repository. But forcefully pushing to remote repo is not a good practice.

What is force push with lease?

Introducing Force with Lease Using this flag, git checks if the remote version of the branch is the same as the one you rebase, i.e. did someone push new commits when we were rebasing. The push is then rejected if the remotes branch is changed. It's like taking a lease on the version of the branch you started changing.

Why do I need to force push after rebase?

To push the changes to the branch after a rebase, you need to force push your commits using the -f or --force flag in the git push command on Git. This is because something has changed in the remote branch and the commit history is different for both the remote and your local branch.


1 Answers

You only force a push when you need to replace the remote history by your local history.

This happens when you rewrite the local history, typically through a git rebase.
For instance, if you just pushed an incorrect commit, and amend it locally, using a push --force can help correct a recent push

If you are the only one working on the branch you are force pushing, this is not a big deal.
If you are not the only one, then you need to communicate clearly in order for other users to reset their own local branch to the new remote. Or you need to avoid force pushing in the first place.

Is it a good practice to do force push into master branch?

Generally, it is not a good practice (again unless you are the only one using the remote repo).
And don't forget that once a branch has been force pushed... you cannot know who did the push --force.

like image 112
VonC Avatar answered Sep 28 '22 00:09

VonC