Delete multiple remote branches in git

前端 未结 18 2466
轮回少年
轮回少年 2020-12-07 07:11

I have a team member who inadvertently pushed over 150 of his local branches to our central repo. Thankfully, they all have the same prefix. Using that prefix, is there a gi

相关标签:
18条回答
  • 2020-12-07 07:44

    Neevek's solution is elegant, but it can be better: the solution as proposed calls 'git push' once per branch, which means an additional network round-trip per branch to be deleted. Since you're using awk anyway, why not use it to prefix the ':' and then xargs can call 'git push' exactly once and delete all the branches at once:

    Dry-run to list the branches that would be deleted:

    git branch -r | awk -F/ '/\/PREFIX/{print ":" $2}'
    

    Final solution to actually push the deletes:

    git branch -r | awk -F/ '/\/PREFIX/{print ":" $2}' | xargs git push origin
    
    0 讨论(0)
  • 2020-12-07 07:45

    Thanks to Steve and Neevek, I found a solution that worked pretty well for me I figured worth sharing:

    Steve's solution worked for me with one minor adjustment. My remotes were named origin/feature/some-feature-name so I trimmed your awk:

    git branch -r | awk -Forigin/ '/\/feature/ {print $2 $3}' | xargs -I {} git push origin :{}
    

    It's now doing a nice little delete flow:

    To github.com:project/project-name.git
    - [deleted]         feature/search-min-chars
    To github.com:project/project-name.git
    - [deleted]         feature/search-placeholder
    To github.com:project/project-name.git
    - [deleted]         feature/server-error-message
    To github.com:project/project-name.git
    - [deleted]         feature/six-point-asterisk
    

    Was wondering if anyone had any ideas for a more elegant solution, though, that might output something like this (my CLI scripting is pretty poor, so it'd take me awhile to figure this out):

    git push origin :feature/search-min-chars :feature/search-placeholder :feature/server-error-message :feature/six-point-asterisk
    

    This would result in a nice single output with one network request:

    To github.com:project/project-name.git
    - [deleted]         feature/search-min-chars
    - [deleted]         feature/search-placeholder
    - [deleted]         feature/server-error-message
    - [deleted]         feature/six-point-asterisk
    
    0 讨论(0)
  • 2020-12-07 07:46

    Dry run:

    git branch -r --list 'origin/your-branch-name/*' | sed "s/origin\///" | xargs -I {} echo {}
    

    Delete remote branches:

    git branch -r --list 'origin/your-branch-name/*' | sed "s/origin\///" | xargs -I {} git push origin --delete {}
    

    Delete only fully merged remote branches:

    git branch -r --merged --list 'origin/your-branch-name/*' | sed "s/origin\///" | xargs -I {} git push origin --delete {}
    

    Explanation:

    sed "s/origin\///" will remove origin/ from the branch name. Without stripping that away I got: remote ref does not exist

    0 讨论(0)
  • 2020-12-07 07:50

    I realize this is for git command, but if you looking for an alternate solution to do the similar or same result:

    You can do it from here (Git Remove Remote Branches):

    Then select the branches you want:

    Make sure you have the permissions to remove the remote branches.

    0 讨论(0)
  • 2020-12-07 07:52

    Thanks to Neevek for great and elegant solution!

    But i have some troubles with slashes in branch names (i'm using Git Flow), because of awk field separator / (-F option)

    So my solution is based on Neevek's, but correctly parses branch names with /. In this case i presume that your remote called origin. Command for deleting remote branches with names staring with PATTERN:

    git branch -r | awk -Forigin/ '/\/PATTERN/ {print $2}' | xargs -I {} git push origin :{}
    

    And don't forget to check what you are going to delete:

    git branch -r | awk -Forigin/ '/\/PATTERN/ {print $2}'
    

    USEFUL TIP: If your branch names (without origin/ prefix) stored in a text file (one branch name per line), just run:

    cat your_file.txt | xargs -I {} git push origin :{}
    
    0 讨论(0)
  • 2020-12-07 07:52

    resource https://coderwall.com/p/eis0ba

        1 - List all your remote branches:
    
        $ git branch -r
    
        2 - Filter the branches by some regular expression. In this case I'm interested in deleting any branch with the 'feature-' prefix:
    
        $ git branch -r | awk -F/ '/\/feature-/{print $2}'
        3 - Pipe the last command to git push to delete them:
        # **Edit** - Removed extra colon, which is not needed
        $ git branch -r | awk -F/ '/\/feature-/{print $2}' | xargs -I {} git push origin {}
        4 - Grab a beer.
    
        5 - Remove any local reference to those branches:
    
        $ git remote prune origin
    
    0 讨论(0)
提交回复
热议问题