git delete remotes: remote refs do not exist

孤街浪徒 提交于 2019-12-04 07:29:32

问题


In short;

  • How can I delete remote multiple merged remotes?

More background;

I have a git repo with tens of remotes which have been merged into master. I can delete these remotes one at a time by using:

git push --delete origin myBranch-1234

However this is a slow and tedious process for all remotes. So I'm trying this command:

git branch -r --merged | grep origin | grep -v master | xargs git push origin --delete

git branch -r --merged lists all merged remotes.
grep origin tells the command to include origin.
grep -v master tells the command to exclude master.
xargs git push origin --delete tells the command to delete the list of remotes.

All together, I expect this to gather all merged remotes and delete them.

When I run the above command, I receive the following for every merged remote;

error: unable to delete 'origin/myBranch-1234': remote ref does not exist
error: unable to delete 'origin/myBranch-1235': remote ref does not exist
error: unable to delete 'origin/myBranch-1236': remote ref does not exist
error: unable to delete 'origin/myBranch-1237': remote ref does not exist
... etc

However these remotes do exist and I can checkout each of them. Many sites and people recommend that I run git fetch --prune to clean up missing references. This does nothing because all of these remotes exist.

So I ask you, dear stack exchange;

  • Why can I delete one remote, but not many?
  • Is my command correct?

I think I'm missing something small. Every time I research this, it seems like I'm doing this correctly, but I'm getting the above errors.

  • Delete local and Remote Branches
  • gist Delete Merged Remotes

回答1:


You may need to prune your local "cache" of remote branches first. Try running:

git fetch -p origin

before deleting.




回答2:


Are those branches removed from the remote (origin)? If yes, you can simply do

git fetch --prune origin

Otherwise they might return even after you delete them locally.

Update: Looking at your command again, it looks like you're building it incorrectly. You probably want

git push origin --delete myBranch-1234

but instead you are doing something like

git push origin --delete origin/myBranch-1234



回答3:


Use sed to remove 'origin/' part and change a lttile xargs part.

git branch -r --merged | grep origin | grep -v -e master | sed s/origin\\/// |  xargs -I{} git push origin --delete {}


来源:https://stackoverflow.com/questions/32147093/git-delete-remotes-remote-refs-do-not-exist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!