美文网首页Git
Git remove all branches except m

Git remove all branches except m

作者: JaedenKil | 来源:发表于2018-09-07 16:06 被阅读87次

    If you use git push --set-upstream someBranch to push a branch to remote, then these branches are merged, you do git branch -a, you may find a lot of branches, which is ugly.
    git branch shows local branches.
    git branch -r shows remote branches.
    git branch -a shows all branches.

    $ git branch -a
    * master
      optimize_by_regex
      optimize_map_store_method
      optimize_use_rect
      remotes/origin/dev
      remotes/origin/master
      remotes/origin/optimize_beautify_setup
      remotes/origin/optimize_beautify_setup_init_method
      remotes/origin/optimize_by_regex
      remotes/origin/optimize_map_store_method
      remotes/origin/optimize_use_rect
    
    • First, I'd like to remove all local branches except master:
      • Have a check by:
    $ git branch | grep -v -E *master*
      optimize_by_regex
      optimize_map_store_method
      optimize_use_rect
    

    And yes, these are exactly the branches you want to remove, so:

    $ git branch -D `git branch | grep -v -E *master*`
    Deleted branch optimize_by_regex (was a69df33).
    Deleted branch optimize_map_store_method (was 94be07e).
    Deleted branch optimize_use_rect (was 6810a37).
    
    • Check what have been removed, and what have remained:
    $ git branch -a
    * master
      remotes/origin/dev
      remotes/origin/master
      remotes/origin/optimize_beautify_setup
      remotes/origin/optimize_beautify_setup_init_method
      remotes/origin/optimize_by_regex
      remotes/origin/optimize_map_store_method
      remotes/origin/optimize_use_rect
    
    • Second, remove all remote branches except master:
      • Have a check by:
    $ git branch -r | grep origin | grep -v -E *master* | grep -v HEAD | cut -d/ -f2 | while read line; do echo $line; done
    dev
    optimize_beautify_setup
    optimize_beautify_setup_init_method
    optimize_by_regex
    optimize_map_store_method
    optimize_use_rect
    

    And if these are exactly the branches to be deleted:

    $ git branch -r | grep origin | grep -v -E *master* | grep -v HEAD | cut -d/ -f2 | while read line; do git push origin --delete $line;done
    To XXX.git
     - [deleted]         dev
    To XXX.git
     - [deleted]         optimize_beautify_setup
    To XXX.git
     - [deleted]         optimize_beautify_setup_init_method
    error: unable to delete 'optimize_by_regex': remote ref does not exist
    error: failed to push some refs to 'XXX.git'
    To XXX.git
     - [deleted]         optimize_map_store_method
    error: unable to delete 'optimize_use_rect': remote ref does not exist
    error: failed to push some refs to 'XXX.git'
    

    If remote branch delete successfully,

    To XXX.git
     - [deleted]         dev
    

    else:

    error: unable to delete 'optimize_use_rect': remote ref does not exist
    error: failed to push some refs to 'XXX.git'
    

    This is weird, do some sync:

    $ git fetch --all --prune
    Fetching origin
    From XXX
     - [deleted]         (none)     -> origin/optimize_by_regex
     - [deleted]         (none)     -> origin/optimize_use_rect
    

    Refer to:

    git-fetch - Download objects and refs from another repository

    git fetch [<options>] [<repository> [<refspec>…​]]
    git fetch [<options>] <group>
    git fetch --multiple [<options>] [(<repository> | <group>)…​]
    git fetch --all [<options>]

    --all
    Fetch all remotes.

    -p
    --prune
    Before 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 --tags 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. Supplying --prune-tags is a shorthand for providing the tag refspec.

    See the PRUNING section below for more details.

    PRUNING
    Git has a default disposition of keeping data unless it’s explicitly thrown away; this extends to holding onto local references to branches on remotes that have themselves deleted those branches.

    If left to accumulate, these stale references might make performance worse on big and busy repos that have a lot of branch churn, and e.g. make the output of commands like git branch -a --contains <commit> needlessly verbose, as well as impacting anything else that’ll work with the complete set of known references.

    git-fetch.

    • Finally,
    $ git branch -a
    * master
      remotes/origin/master
    

    相关文章

      网友评论

        本文标题:Git remove all branches except m

        本文链接:https://www.haomeiwen.com/subject/qkycgftx.html