Patricia Sauer - Authentic Programming

Git force pushing | The Git Push Command

A sign saying 'push to reset the world'

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:

The initial commits A, B, C, D, and E

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:

The commits after resetting: A, B, and C

Now, you will create a new commit D’ which will contain the changes which beforehand have been part of commit D and E:

The commits after resetting: A, B, C, and D'

Assuming that you pushed the initial scenario to the remote beforehand and want to push the new history, Git will complain

git push
To some-git:some-user/some-repo.git
 ! [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:

The commits: A, B, and C

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:

The commits you made: A, B, C, and D

The commits your team member made: A, B, C, and D'

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:

The remote master after your team member pushes his changes: A, B, C, and D'

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:

The remote master after force pushing your changes: A, B, C, and D

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

or

git push -d origin featureXYZ

Reference

For full reference see this post about the Git push command or the Git push documentation.

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer