Skip to content

Instantly share code, notes, and snippets.

@colossus9
Created August 3, 2016 20:44
Show Gist options
  • Save colossus9/a6802a74ee6f7e05b98a382f97360b1a to your computer and use it in GitHub Desktop.
Save colossus9/a6802a74ee6f7e05b98a382f97360b1a to your computer and use it in GitHub Desktop.
Local git branch list "synchronization" with remote

Git Remote Branch Sync

There are some folks who want to keep their local git branch list to be in sync with the branches available on the remote. This typically happens after a Pull Request on GitHub Enterprise, when the remote branch is merged and removed, but the branch still exists locally.

To clean up the branches that no longer exist on the remote, there are a couple of options (as well as more that are not listed here):

  • The quick way:

    git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
    
  • Make sure we keep master:

    • You can ensure that master, or any other branch for that matter, doesn't get removed by greping for more. In that case you would go:
    git branch --merged | grep -v "\*" | grep -v "master" | xargs -n 1 git branch -d
    
  • If you want to keep multiple branches:

    • So if we wanted to keep master, develop and staging for instance, we would use:
    git branch --merged | grep -v "\*" | grep -Ev "(\*|master|develop|staging)" | xargs -n 1 git branch -d
    

Reference: Delete local Git branches after deleting them on the remote repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment