Git force pushing | The Git Push Command
Force pushing should be used carefully because by force pushing your changes, you can overwrite changes made by others. It should never be used on your main branches like master or development. Instead, you can use force pushing on your own branches.
When do you need to force push?
You will need to force push your changes when you changed your commit history which was already pushed to the remote branch. You could have done this by pushing to the remote branch, then deciding to use the Git reset command and adding your changes to a new commit.
Let’s assume that we have the following commits:
You will now use the Git reset command to soft reset your changes to commit C. This will lead to the commits D and E being deleted but the changes made being kept. The result is the following Git log:
Now, you will create a new commit D’ which will contain the changes which beforehand have been part of commit D and E:
Assuming that you pushed the initial scenario to the remote beforehand and want to push the new history, Git will complain
! [rejected] my-new-feature -> my-new-feature (non-fast-forward)
error: failed to push some refs to 'git@some-git:some-user/some-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
What Git is telling you here is that your local branch is behind the remote branch. The local branch is on commit D’ whereas the remote branch is on commit E.
What you can do now to push your changes is force pushing as follows:
git push -f
How can force pushing overwrite changes made by others?
When you are on one of your main branches, you can easily overwrite commits by force pushing your changes. Let’s have a look at an example:
Let’s assume that you are on the master branch. You pulled from the master branch and therefore have the latest commits locally. You start working on your stuff, create commits and want to push. Meanwhile, someone else might have pushed some changes to the master branch. When you are force pushing now, you will overwrite the commits the other team member did meanwhile. This happens because you did not pull the latest changes before force pushing. You might say that you can pull before force pushing. But: What if you both push almost simultaneously? In case that the other team member pushes just slightly before you do: You would overwrite the changes the other team member made. In this scenario you would not even have a chance to pull the latest commits before pushing because both pushes happen at the same time.
Let’s have a closer look at this scenario. The initial history on master is as follows:
You and your team mate pull this latest state from the master so that you have this state locally. Both of you work on their stuff and create a commit each:
You created the commit D and your team member created the commit D’.
Let’s assume that your team member pushes his changes to master. This will lead to master having commit D’ as the latest commit:
When you want to push your changes now, Git will complain with the above mentioned message because your local master branch is behind the remote master. When you are force pushing your changes now, the remote master branch will look as follows:
As you can see: The result is the commit of your team member being deleted because you overwrote it.
I hope you are not afraid of force pushing now. Instead, keep in mind what can happen when force pushing and to use it on your own branches, solely.
How to force push
You can force push your changes by using the Git push command with the -f flag:
git push -f
Deleting a remote branch
You might be surprised but the Git push command is also used to delete a remote branch. In case that we wanted to delete a remote branch called "featureXYZ", the command would look as follows:
git push origin :featureXYZ
git push -d origin featureXYZ