Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `git push --force-with-lease` failing with "rejected ... stale info" even when my local repo is up to date with remote?

Tags:

I'm trying to force push a rebase of a feature branch to a remote repository. To be a bit safer, I'm trying to use --force-with-lease to make sure no other changes have happened in the branch since I last fetched it.

This is failing for reasons I don't understand:

$ git branch * my-branch   master  $ git push --force-with-lease origin my-branch -u To gitlab.com:example/my-project.git  ! [rejected]        my-branch -> my-branch (stale info) error: failed to push some refs to '[email protected]:example/my-project.git' 

I tried a fetch to see if my local cache had somehow gotten out of sync:

$ git fetch  $ git push --force-with-lease origin my-branch -u To gitlab.com:example/my-project.git  ! [rejected]        my-branch -> my-branch (stale info) error: failed to push some refs to '[email protected]:example/my-project.git' 

I tried simplifying the push command a bit:

$ git push --force-with-lease To gitlab.com:example/my-project.git  ! [rejected]        my-branch -> my-branch (stale info) error: failed to push some refs to '[email protected]:example/my-project.git' 

I tried limiting the check to my branch:

$ git push --force-with-lease=my-branch:origin/my-branch To gitlab.com:example/my-project.git  ! [rejected]        my-branch -> my-branch (stale info) error: failed to push some refs to '[email protected]:example/my-project.git' 

As you can see, it fails the same way every time.

Why is my push failing, and how do I fix it?

like image 889
Laurence Gonsalves Avatar asked May 17 '19 17:05

Laurence Gonsalves


People also ask

What is the problem with git force -- push?

The Risks of Git Push ForceBecause you have failed to pull those changes, they are not reflected in your local repository. In this case, if you perform a Git push force, you will replace the remote repository with a copy of your local repo, effectively deleting your team member's work.

Why is git push being rejected?

A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.

What is git push force with lease?

Introducing Force with Lease Using this flag, git checks if the remote version of the branch is the same as the one you rebase, i.e. did someone push new commits when we were rebasing. The push is then rejected if the remotes branch is changed. It's like taking a lease on the version of the branch you started changing.

How do I force a git push?

To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch).


1 Answers

In this case it turned out that the problem was that the remote branch had been deleted, but there was still a copy of it in my local repo. Fetch doesn't delete local copies by default, which is why it had no effect.

Adding the --prune option to my initial git pull (before doing my rebase) corrects this problem.

like image 113
Laurence Gonsalves Avatar answered Sep 19 '22 06:09

Laurence Gonsalves