Introduction to git push force git function
git push force
command is a git operation responsible for making changes to the remote repository by overwriting an earlier change. A git push command without force only works if you have fetched and merged changes from others in a shared project. Git push function with --force
implies that your local commits become the remote latest commits. The downside of this approach is that you will not have records or work from other collaborators. Taking such an approach should be carried out with precaution as other collaborators' recent commits are affected.
In this tutorial about git push force, we will learn how git push force applies to different git scenarios in a shared remote project.
Git push force workflow
Setting up the lab environment
To practice using git push force
command in different situations, you will require to set up a lab environment in git. I will be using Ubuntu-20.04.3 and git version 2.25.1 to run the push force command experiments. To begin working, we will clone a remote repository push-force
to the local station as shown in the sample output below.
$ git clone
Sample Output:
Git push force examples
In this section, we will learn using examples how git push --force
function works with a remote branch, origin master/main and git push force with --lease
.
How to perform git push force to a remote branch
In this example, we have made and committed some changes to mybranch
in the remote repository push-force
as displayed in the sample output below:
$ git push --set-upstream origin mybranch
Sample Output:
Now we shall run git log --oneline
command to view the commits.
$ git log --oneline 5da39bc (HEAD -> mybranch, origin/mybranch) newfile.txt 905b49b (master) myfile.txt a55b205 (origin/master, origin/HEAD) Initial commit
While at this stage, we realize that the committed file 5da39bc
was wrong and we shouldn’t have pushed it to remote. So we will make changes to the commit 5da39bc
and create a new commit with the changes.
Below are the edited changes to the commit 5da39bc
newfile.txt.
$ git commit -m "newfile edited line one.txt"
[mybranch 7f4b5c8] newfile edited line one.txt
1 file changed, 1 insertion(+)
Now, we will again run git log --oneline
command to view the commits as shown in the sample output below.
$ git log --oneline 7f4b5c8 (HEAD -> mybranch) newfile edited line one.txt 5da39bc (origin/mybranch) newfile.txt 905b49b (master) myfile.txt a55b205 (origin/master, origin/HEAD) Initial commit
Before we could push the edited changes to the remote mybranch
, another user, user B has been working on the shared repository push-force mybranch
as well. After we pushed the first commit 5da39bc
, user B also pushed her changes commit 5ced2eb
.
Since the first commit we have made is wrong, we would like to overwrite user B commit 5ced2eb so that we maintain the right initial commit before any other is added. To achieve that we shall run git push --force
command.
But first, let’s run the default git push
command to see what will happen:
$ git push ! [rejected] mybranch -> mybranch (fetch first) error: failed to push some refs to 'https://github.com/Josephine-Techie/push-force.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The push request has been rejected because there were some changes in the remote mybranch
since the last push and must be fetched first for a successful push.
It is in such a situation that running git push --force
command will be a valid option to void rejection.
Let’s, therefore, run the git push --force
command to forcefully commit the local changes in the remote mybranch
as follows:
$ git push --force
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 1014 bytes | 37.00 KiB/s, done.
Total 11 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/Josephine-Techie/push-force.git
+ 5ced2eb...7f4b5c8 mybranch -> mybranch (forced update)
We successfully pushed the new changes we made to remote without having to fetch User B changes using git push ---force
command.
To confirm the current commits, we shall rerun git log --oneline
command
$ git log --oneline 7f4b5c8 (HEAD -> mybranch, origin/mybranch) newfile edited line one.txt 5da39bc newfile.txt 905b49b (master) myfile.txt
The sample results do not include commit 5ced2eb
from user B since we never fetched it and also it's because git push –-force
command overwrites any other remote committed changes.
How to perform git push force origin master
To illustrate how git push force origin master
command works we shall checkout master as shown in the sample output below.
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
You notice from the sample output that we have run a commit and are yet to push it upstream. To push it upstream we shall run the git push
command as follows:
$ git push ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/Josephine-Techie/push-force.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Ops! It’s also rejected.
It seems user B has also made changes and pushed them before we did. To overwrite user B committed changes in the origin master we shall run the git push origin master --force
command.
$ git push origin master --force
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Josephine-Techie/push-force.git
+ 4e78c85...905b49b master -> master (forced update)
Now run git log --oneline
command to view the commits.
$ git log --oneline 905b49b (HEAD -> master, origin/master, origin/HEAD) myfile.txt a55b205 Initial commit
You will notice the commit 4e78c85
isn’t included as it was overwritten by the git push --force
command.
Lastly, to ensure that we now have a clean origin master branch we shall run git status
.
$ git status
On branch master
Your branch is up to date with 'origin/master'.
How to use git push --force-with-lease
git push --force-with-lease
function is recommended for use when you want to consider other team members commit contributions. The push force command with --lease
when used generates an error if there are remote changes done since you last pushed yours unlike what push with --force
does. We shall demonstrate how the git push --force-with-lease
command works in the following example.
After making some edits to the last commit 905b49b
in the master branch, we will now want to push it upstream.
Let's view the newly committed changes by running the git log --oneline
command
$ git log --oneline 7e9b1c2 (HEAD -> master) edited myfile.txt 905b49b (origin/master, origin/HEAD) myfile.txt a55b205 Initial commit
When we run the default push function there’s an error because user B still got ahead and pushed her changes. See the sample output below.
$ git push origin master ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/Josephine-Techie/push-force.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
This time around out of consideration of user B commit contributions, we will use git push --force-with-lease
command as follows:
$ git push --force-with-lease
! [rejected] master -> master (stale info)
error: failed to push some refs to 'https://github.com/Josephine-Techie/push-force.git'
Consequently, the git push --force-with-lease
command has been rejected. Also, the output for git push --force-with–lease
function doesn’t give detailed information same as what a normal git push
command outputs in a similar situation.
Summary
In this tutorial, we have learnt how git push --force
function works in different scenarios under the following topic:
- How git push force command to a remote branch works
- How git push force function to origin master operates
- How git push --force-with-lease works
Further reading