Skip to content

Instantly share code, notes, and snippets.

@una
Last active April 12, 2022 03:29
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save una/7c0e69ca122a28921ecfed9665078d2a to your computer and use it in GitHub Desktop.
Save una/7c0e69ca122a28921ecfed9665078d2a to your computer and use it in GitHub Desktop.
Cleanup Branches
  1. Go to the remote repo and delete outdated branches

  2. Then either:

    • Locally cleanup only merged branches: git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
    • Locally remove fetched, outdated branches: git for-each-ref --format='%(refname:short) %(upstream)' refs/heads/ | awk '$2 !~/^refs\/remotes/' | xargs git branch -D
@timrchavez
Copy link

timrchavez commented Apr 25, 2017

I might add...

  1. Convince people to work against branches on a fork of the repo, so cluttering the project repo is never an issue.
# Setup remotes
$ git remote add upstream git@github.com:org/repo.git
$ git remote set-url upstream --push OMG_NO!
$ git remote -v
origin	git@github:username/repo.git (fetch)
origin	git@github:username/repo.git (push)
upstream	git@github.com:org/repo.git (fetch)
upstream	OMG_NO! (push)

# Create a branch on your fork and do some things
$ git checkout -b my_devel_branch
$ git commit -a -m "Wow, -a? Terrible habit"

# Sync your master with upstream master
$ git fetch upstream
$ git checkout master
$ git merge upstream/master # If your master HEAD is a common ancestor of the upstream HEAD, this becomes just a fast-forward merge

#  Rebase your branch on master
$ git checkout my_devel_branch
$ git rebase master
First, rewinding head to replay your work on top of it...
Fast-forwarded my_devel_branch to master.

# Push your branch back to github
$ git push origin my_devel_branch

# GitHub will detect the new branch and give you a link to create a PR against
# org/master (or whatever the default branch is) and name/master for repo.git

Of course this is git, so there are variations on that^^ workflow. Like using git branch --set-upstream-to && git pull on master

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