JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Git Push --force-with-lease and Git Push --force

Author:JIYIK Last Updated:2025/04/20 Views:

This article will discuss the difference between the git push --force-with-leaseand commands. Usually, we use the command to publish local changes to the remote repository.git push --forcegit push

Let's go ahead and examine these commands.


git push --forceOrder

To publish local changes to the remote repository, we run:

$ git push origin

However, in situations where multiple developers share our remote repository and publish their changes to the remote repository, Git can refuse to push.

To force Git to publish our commits, we git pushadd --forcethe -p flag to our -p command, as shown below.

$ git push --force origin

The disadvantage of using this command is that it does not take into account changes pushed to the remote repository by other developers. This command will overwrite the repository based on the state of your local repository.

This can be dangerous as it can disrupt our project timeline. If you want to push your changes to the remote repository and keep the changes made by other developers, you can do so.


git push --force-with-leaseOrder

This command comes in handy when multiple developers share a remote repository. We use it when publishing changes to avoid discarding changes pushed by other developers.

$ git push --force-with-lease origin

Let's look at an example. This is the current state of our remote repository.

Submission History

We will README.mdmake some changes to the files and commit the changes on GitHub. Note the commit ID outlined 097ab8b.

This is what it looks like now.

Submit history updates

Note the commit ID ba29c53.

We will now open the file on our computer README.md, make some more edits, commit the changes, and then try it out git push.

Push attempt

Git rejected our push attempt because the commit histories are different. We could run git push --forcethe command, but this would discard the changes we made in GitHub.

We can execute git push --force-with-leaseas 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)

git push --force-with-leaseThe difference between and git push --forceis the result. leasePushing changes with helps us avoid discarding changes pushed by other developers.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Ignore everything except certain files in Git

Publish Date:2025/04/20 Views:151 Category:OPERATING SYSTEM

This article outlines the steps to make Git ignore all but a few files in a Git repository. The .gitignore file is a useful Git utility that allows us to tell Git which files to track and which files not to track. If you want your .gitignor

Get the current branch in Git

Publish Date:2025/04/20 Views:57 Category:OPERATING SYSTEM

This article describes how to use git branch the command and git symbolic-ref the command to get the branch you are currently working on in git. Get the current branch Use git branch the command to get a list of all branches. The branch nam

Update branches from master in Git

Publish Date:2025/04/20 Views:142 Category:OPERATING SYSTEM

When working in Git with many developers and analysts working on various branches simultaneously, we may encounter many problems. A common problem is when one team member makes changes in his local branch while others work on that remote br

Commit changes to a Git branch

Publish Date:2025/04/20 Views:65 Category:OPERATING SYSTEM

In this article, you'll learn how to save commits to a new or existing branch in Git. This article explains how to move commits to: A new branch Existing branches You’ll often find yourself committing the same staged changes to different

Tagging an older commit in Git

Publish Date:2025/04/20 Views:115 Category:OPERATING SYSTEM

This article outlines the steps required to tag old commits in Git. We use git tags to mark specific points in our commit history as important. Typically, a git tag marks a stable release or an important milestone in a project. How do you t

List all tags in Git

Publish Date:2025/04/20 Views:120 Category:OPERATING SYSTEM

This article will teach us how to list all tags in Git. Git is a version control system that tracks changes in project directories using a Git repository. Changes made to files are tracked in a Git repository using commits. Using tags, we c

Recovering a reverted Git commit

Publish Date:2025/04/20 Views:197 Category:OPERATING SYSTEM

This article outlines the steps required to revert a reverted Git commit. By the end of this article, you will have the necessary knowledge to recover a reverted commit without rewriting your commit history. Recovering a reverted Git commit

Git merge repository

Publish Date:2025/04/20 Views:160 Category:OPERATING SYSTEM

When working on a project with multiple team members having separate repositories for each team, at some point in time we come across a situation where we want to combine these separate repositories into one master repository to deploy all

Git push to another branch with a different name

Publish Date:2025/04/20 Views:57 Category:OPERATING SYSTEM

git push It has a rich set of options that allow you to use the full power of Git. One of them is its source:destination refspecs parameters. We use these git push to go to a specific branch with a name of our choosing. Finally, we'll see s

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial