Git Push --force-with-lease vs Git Push --force

John Wachira Jan 30, 2023
  1. the git push --force Command
  2. the git push --force-with-lease Command
Git Push --force-with-lease vs Git Push --force

This article will discuss the difference between the git push --force-with-lease and git push --force commands. Generally, we use the git push command to publish our local changes to our remote repository.

Let us go ahead and examine these commands.

the git push --force Command

When publishing our local changes to our remote repository, we run:

$ git push origin

However, in a case where several developers share our remote repository and publish their changes to the remote repo, Git can reject a push.

To force Git to publish our commits, we add the --force flag to our git push command, as shown below.

$ git push --force origin

The downside of using this command is that it does not consider changes pushed by other developers to the remote repository. The command will override the repo based on the state of your local repository.

This can be dangerous since it can mess up our project timeline. If you want to push changes to a remote repository and preserve the changes made by other developers, here is how you do it.

the git push --force-with-lease Command

This command comes in handy when several developers are sharing a remote repository. We use it when publishing our changes to avoid discarding changes pushed by other developers.

$ git push --force-with-lease origin

Let’s look at an example. Here is the current state of our remote repository.

commit history

We will make some changes to the README.md file and commit the changes while still on GitHub. Note the outlined commit id 097ab8b.

Here is how it looks now.

commit history update

Note the commit id ba29c53.

We will now open the README.md file on our computer, make some more edits, commit the changes, and attempt a git push.

Push attempt

Git has rejected our push attempt since the commit history is different. We can run the git push --force command, but this will discard the changes we had made in GitHub.

We can do git push --force-with-lease as shown below.

$ git push --force-with-lease
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/Wachira11ke/Delftscopetech.git
 + b7b8e6a...8ddd086 main -> main (forced update)

The difference between the git push --force-with-lease and git push --force is the result. Pushing changes with lease helps us avoid discarding changes pushed by other developers.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Push