简体   繁体   中英

git - remote branch “apparently” not deleted

Developer A creates remote branch

git checkout -b myBranch
git push -u origin myBranch

Developer B checks it out

git fetch origin myBranch
git checkout myBranch

They both do some work, commit, pull, etc. When they are done, merge back to develop

git checkout develop
git merge --no-ff myBranch
git push origin develop

and delete branch

# Developer A
git branch -d myBranch             # delete local branch
git push origin --delete myBranch  # delete remote branch

# Developer B
git branch -d myBranch
git fetch

At this point, only Developer A who pushed the remote delete can see the correct state of remote. Developer B still sees that myBranch on remote, even though it no longer exists. If Developer B tries to check out myBranch and pull, gets a 'cannot find myBranch ref' error.

#Developer A
git branch -a
  * develop
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop
  remotes/origin/production

#Developer B
git branch -a
  * develop
  remotes/origin/myBranch                 # <<<<< why is this line here??
  remotes/origin/HEAD -> origin/develop
  remotes/origin/develop
  remotes/origin/production

If we go on bitBucket web console and look at the repo, branch 'myBranch' does not exist.

What's going on here and why, how do we solve it?

git branch -a

-a stand for all branches (local + remote) this is why the user see all the branches.

Developer B still sees that myBranch on remote, even though it no longer exists

You should execute: git fetch --all --prune
The prune will remove all local copies of your branches, tags which are removed from the server.


You can set the prune value to be your default so it will remove the deleted branches and tags on every fetch

git config fetch.prune true

-p / --prune

After fetching, remove any remote-tracking references that no longer exist on the remote.

Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a - -tag option.

However, if tags are fetched due to an explicit refspec (either on the command line or in the remote configuration, for example if the remote was cloned with the --mirror option), then they are also subject to pruning .

Developer B needs to use the -p parameter on git fetch. git fetch -p This parameter will remove all references that do not exist on remote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM