5/18/2023 0 Comments Git delete remote branch![]() Issue the git push origin –delete branch-name command, or use the vendor’s online UI to perform a branch deletion.In review, the steps to delete remote Git branches are: With the local branch deleted, the remote tracking branch deleted, and all instances of the remote git branch deleted as well, the remote Git branch should be gone forever. The prune option removes any remote tracking branch in your local repository that points to a remote branch that has been deleted on the server. However, if the branch has already been deleted from the GitHub or BitBucket server, a simpler approach is to call the git fetch command with the prune option. You can delete a remote tracking with the following git branch command: git branch -delete -remotes origin/branch-name Use at most 1 argument per command line.So a thorough branch removal strategy may requires the explicit deletion of this remote tracking branch as well. put it at the end) of the command.Įach line contains the name of a branch, so each of the branches listed are deleted from origin.Įach line should be used only up to the first space (i.e. Take each line we currently have and use it as the argument (i.e. Keep only the third section and anything after that (i.e. (See grep Matching Control) cut -d "/" -f 3-ĭivide each line at the / character ( -d "/") Take the results thus far and include only those lines that match “remotes/origin/”. (See grep Matching Control) grep "remotes/origin/" Take the results thus far and remove any lines that match “HEAD”. (See grep Matching Control) grep -invert-match HEAD Take the results thus far and remove any lines that match “master”. (See git branch –merged) grep -invert-match master Show only branches that have been merged into remotes/origin/master. List both remote-tracking branches and local branches. Explanation of the Command git branch -all -merged remotes/origin/master By setting up this project, this command and many other time saving features I use are available. The git delete-merged-on-origin command is one of the commands available with the Iron Code Studio Git Configuration I use. gitconfig file (which is found at ~/.gitconfig on a Mac). The git config -global line above writes this new alias to your. Now, whenever we type git delete-merged-on-origin our command will be run. ![]() We can add the alias to our global configuration by running the following line: git config -global lete-merged-on-origin '!git branch -all -merged remotes/origin/master | grep -invert-match master | grep -invert-match HEAD | grep "remotes/origin/" | cut -d "/" -f 3- | xargs -n 1 git push -delete origin' Fortunately, Git allows use to create an alias. While this is a really helpful command, it is too long to type out each time. This answer is a modified version of this Stack Overflow Answer. git branch -all -merged remotes/origin/master | grep -invert-match master | grep -invert-match HEAD | grep "remotes/origin/" | cut -d "/" -f 3- | xargs -n 1 git push -delete origin While it is convenient that most of the Git hosting I use makes it easy to show only unmerged branches, this also makes it easier to forget about these merged branches.Įventually, I find I do need to clean up after myself and rather than manually going through all these branches and delete them, I’ve found the following command will delete all branches on the remote origin that have been merged into origin/master. However, I often forget about my remote branches after they are merged. ![]() When I’m working on a project using Git, I’m good about deleting local branches that I don’t need anymore.
0 Comments
Leave a Reply. |