标签云

微信群

扫码加入我们

WeChat QR Code

I want to delete a branch both locally and remotely.Failed Attempts to Delete Remote Branch$ git branch -d remotes/origin/bugfixerror: branch 'remotes/origin/bugfix' not found.$ git branch -d origin/bugfixerror: branch 'origin/bugfix' not found.$ git branch -rd origin/bugfixDeleted remote branch origin/bugfix (was 2a14ef7).$ git pushEverything up-to-date$ git pullFrom github.com:gituser/gitproject* [new branch] bugfix -> origin/bugfixAlready up-to-date.What should I do differently to successfully delete theremotes/origin/bugfix branch both locally and remotely?


Moderator note: If you intend to answer this question, do note that there are already 40 answers posted.Will your new answer add any substantial value?

2019年06月26日29分56秒

Note: for Git 2.5+ (Q2 2015), the exact message will be "deleted remote-tracking branch": see github.com/git/git/commit/…

2019年06月26日29分56秒

Good lord, I just want to know how many pts the OP ended up getting for this question?

2019年06月26日29分56秒

If you know the syntax git push origin local_branch:remote_branch, then the syntax to delete a branch with git push origin :remote_branch is kind of cute. There's a void before the :

2019年06月26日29分56秒

Don't forget to do a git fetch --all --prune on other machines after deleting the remote branch on the server. ||| After deleting the local branch with git branch -d and deleting the remote branch with git push origin --delete other machines may still have "obsolete tracking branches" (to see them do git branch -a). To get rid of these do git fetch --all --prune.

2019年06月26日29分56秒

in addition to TrevorBoydSmith's git branch -a to view all branches, you can also use git branch -r to view remote branches only. see also git remote show origin - source: gitready.com/intermediate/2009/02/13/list-remote-branches.html

2019年06月26日29分56秒

I had to run git branch -D Branch_Name to get rid of the local branch

2019年06月26日29分56秒

KolobCanyon You only have to use -D if the branch has not been merged into another branch.

2019年06月26日29分56秒

megido well -D force deletes, -d gives you a warning if it's not already merged in.

2019年06月26日29分56秒

If your local branch is not merge with master and ran 'git branch -d your_branch then you will error like error: The branch 'your_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D your_branch'.

2019年06月26日29分56秒

I would suggest using -d instead of -D because it is safer. If -d fails due to unmerged commits then you need to assess that and if it is definitely OK to remove then use -D.

2019年06月26日29分56秒

Others with repository clones where remote branches have been removed should run git remote prune <name> (e.g. git remote prune origin) in order to locally remove stale branches that no longer exist in the remote.

2019年06月26日29分56秒

I would like to add that -d gives a warning if it isn't merged in with the current HEAD. If you need clarity I recommend this command git branch -a --merged origin/master It will list any branches, both local and remote; that have been merged into master. Additional information here

2019年06月26日29分56秒

From your illustration, I can see there are local clone repo and remote origin repo. So there are at least two physical branches. Where is the third branch to delete? Is the third branch only a pointer pointing to a commit in the local clone repo?

2019年06月25日29分56秒

huggie that's pretty much correct. Branches in Git are just bookmarks attached to commits. So in my graphs above, there are X and origin/X bookmarks in the local clone (2 branches), and then there is X on the remote (making 3 branches).

2019年06月26日29分56秒

+1 for the remote tracking branch. This branch is what causes issues when you clone someone else's branch. It keeps on tracking your commits and asking you if you want to push to that person's branch.

2019年06月26日29分56秒

git branch -a will display local and remote branches.It will be help for you diagram introduce.

2019年06月26日29分56秒

note that if you are setting on the branch you want to delete, you need to checkout a branch other than the one you need to delete (eg: master) before deleting the local branch.

2019年06月26日29分56秒

When branches get deleted on origin, your local repository won't take notice of that.You'll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches.You can clean up that information locally like this:git remote prune origin Your local copies of deleted branches are not removed by this.The same effect is achieved by using git fetch --prune

2019年06月26日29分56秒

...and safer to use :O

2019年06月26日29分56秒

You forgot the part about deleting the local branch which can be done by: git branch -d <local_branch> orgit branch -D <local_branch> for force deleting

2019年06月26日29分56秒

You should clarify that the above git push operation deletes the local branch and the remote branch.

2019年06月26日29分56秒

Note that git remote prune is a somewhat obsolete way to remove obsolete remote-tracking branches, the newer way to do it is to use git fetch --prune or git fetch -p.

2019年06月26日29分56秒

RRMadhav, indeed you won't see the deleted branch after deleting it since the reference to the remote branch will be removed for you locally. Anyone else on your team that has checked out that branch will still have that reference and will still see it unless they prune the branch.

2019年06月26日29分56秒

Note that -D forces the deletion. It's always better to use -d, which will remind if you need to do something dangerous.

2019年06月26日29分56秒

ahahah :) it's up to you: use -d if you want to see git crying or -D if you want to cry.

2019年06月26日29分56秒

git push --prune origin didn't do anything for me on gitlab: git clone git://repo.git; git branch -d -r origin/some-branches; git push --prune origin; yields: Everything up-to-date; git fetch; brings locally deleted branches back; git push --mirror; now they are really gone!

2019年06月26日29分56秒

I ended up just add the alias "shoot" into my .gitconfig shoot = push origin --delete

2019年06月26日29分56秒

If your origin is a Atlassian Stash and the branch is set as the default, you will get an error "By default, deleting the current branch is denied...". I had to change the default branch in Stash to point to another branch before I could delete.

2019年06月26日29分56秒

This is perfectly simple as you've done it, but fyi git also lets you make custom commands. Put git push origin --delete $1 in a file on your path called git-shoot and git shoot branchname will work too.

2019年06月26日29分56秒

I only started using Github this year, so I was wondering why this was such a highly rated question, and why none of the top answers were suggesting to just delete it from the Github web interface! Interesting that it's only a recent addition.

2019年06月26日29分56秒

I was going to point this one out. Note that the button won't delete your local branch... see this answer for how to do that: stackoverflow.com/a/10999165/901641

2019年06月26日29分56秒

This is what I was looking for. My own shell function alias didn't work (Unexpected EOF) and I couldn't figure out why, but this works great! The only change I made was replacing && with ; so that even if the first command fails the second will still execute (sometimes only local or only remote exists).

2019年06月26日29分56秒

⚠️Use git branch -D carefully in a script, since it force-deletes a branch without checking it has been merged. Use -d to be safe.

2019年06月26日29分56秒

Does the remote branch deletion requires "git push" afterwards ?

2019年06月26日29分56秒

SamithaChathuranga no, git push origin :<branchname> already pushes an 'empty` branch to the remote (hence deletes the remote branch)

2019年06月26日29分56秒

This works if its your own branch. But if you are pruning all unneeded branches in the repo (some of which aren't yours) it wouldn't suffice

2019年06月26日29分56秒

I didn't downvote, but my thinking is that it isn't substantively helping. The question is obviously asking for a more commandline type answer without having to use an external program, if people were clicking into here, they likely won't be looking for a github for desktop ways.

2019年06月26日29分56秒

Daemedeor , I dissagree.In 2010 when the OP asked the question, the UI way of doing it didn't exist and the only option was command line.To indicate that you want a command line only option it should be stated in the question or with the tag, command-line-interface, which in this case is no present.

2019年06月25日29分56秒

The git command for deleting a remote branch sucks and I tend to forget it (both new and old). Luckily there are GUI tools that have the option. Git Gui, TortoiseGit and GitHub Desktop have it - I wish Git Extensions had this functionality too. Anyway, what I remember is to start Git Gui from within Git Extensions when I need to delete a remote branch.

2019年06月26日29分56秒

This is fine, but people really shouldn't be naming their branches and tags with the same name and same naming scheme in the first place.

2019年06月26日29分56秒

Well, my scenario was that I was converting a branch to a tag and it made sense for the tag to have the same name as the branch. By converting I mean merging branch B to A and tagging the last commit in branch B with tag B so that after deleting branch B it can still be easily restored by simply checking out tag B.

2019年06月25日29分56秒

expanding on this, --delete "$" and -D "$" instead of $1 will handle it for multiple branches.

2019年06月26日29分56秒

I suggest running git branch -d (with lowercase 'd') first to ensure changes have been merged, and then push if successful (put && in between commands)

2019年06月26日29分56秒

one-letter get courtesy of this answer: stackoverflow.com/a/8072675/8047

2019年06月26日29分56秒

chhh then you need to extend this functionality to make this a variable instead of an assumption.

2019年06月26日29分56秒

sorry, but install Ruby for that kind of work ? More logical is implementation on bash, which will work out of box.

2019年06月26日29分56秒

Reishin Ruby is installed on the box just like Bash, at least on OSX. Please see: stackoverflow.com/questions/2342894/…, where this topic has been discarded as opinion-based by SO.

2019年06月26日29分56秒

Yar this link is out of the context and have a more broader scope. I tell only about git and as topic is not originated only for OSX, that choose is strange for other systems (e.g. *UNIX, Windows)

2019年06月26日29分56秒