Local branches – our day-to-day working branches

References to remote branches – aka origin/branch-name items

Actual remote branches – branches on remote server(e.g.: github, bitbucket, gitorius)

Local branches

$ git branch

$ git checkout master $ git branch --merged

$ git branch -d old-merged-feature

$ git branch --no-merged

$ git branch -D old-abandoned-feature

References to remote branches

git pull

git fetch

$ git branch -r

$ git remote prune origin

Tip

$ git fetch -p

Remote branches

git remote prune origin

$ git checkout master $ git branch -r --merged

$ for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

$ git push origin --delete branch-name

$ for branch in `git branch -r --no-merged | grep -v HEAD`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Tip for Github users

Bonus snippets

$ comm -12 <(git branch --merged|awk '{print($1)}') <(git branch -r --merged|awk '{print($1)}'|awk -F \/ '{print($2)}')

$ for branch in `comm -12 <(git branch --merged|awk '{print($1)}') <(git branch -r --merged|awk '{print($1)}'|awk -F \/ '{print($2)}')`;do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

$ comm -12 <(git branch --no-merged|awk '{print($1)}') <(git branch -r --no-merged|awk '{print($1)}'|awk -F \/ '{print($2)}')

$ for branch in `comm -12 <(git branch --no-merged|awk '{print($1)}') <(git branch -r --no-merged|awk '{print($1)}'|awk -F \/ '{print($2)}')`; do echo -e `git show --format="%ci %cr %an" $branch | head -n 1` \\t$branch; done | sort -r

Moving stuff into .gitconfig

#!/usr/bin/env bash comm -12 <(git branch --merged|awk '{print($1)}') <(git branch -r --merged|awk '{print($1)}'|awk -F \/ '{print($2)}')

chmod 755 git-both-merged

[alias] # ... other aliases both-merged = !git-both-merged

$ git both-merged

After working with branch per feature for a while any Git-repository becomes a mess of outdated and not finished branches. To deal with this issue, we need to clean-up three kinds of branches:In this tutorial we suppose, that “master” – is a default branch, where everything is merged(for someone it could be “develop”, or “release” branch), and “origin” – it’s a name of remote. If you use different names, just change them in appropriate commands.At first, list all local branches:We need to know what branches are already merged in “master” and can be easily removed:Now, remove all outdated branches with:Next, decide what to do with not merged branches:If some of them is just abandoned stuff that you don’t need anymore, remove it with “-D” option:After eachorcommand Git creates references to remote branches in local repository, but doesn’t clean up stale references.List referenced remote branches:Clean-up outdated references:Update repository with:and Git automatically prunes all stale references.Usually, remote repository is a big garbage heap of stale branches, if there is no responsible housekeeping person.After previouswe should have synched list of remote branches.At first, we can find branches which are already merged in “master”:But this command does not provide much information. What if this branch is merged, but still used for feature development. Would be cool to know last commit date and author.This magic snippet provides all required information:Now, you can delete own remote branches, and ask other authors to clean-up theirs:Similar snippet for not merged branches:This list should be reviewed more thoroughly to avoid losing important commits.After the last Github update,page is divided into “Your branches”, “Active branches” and “Stale branches”, and it shows same information as previous commands.Usually, it’s simple to remove local and appropriate remote branches at once.This snippet shows only local merged branches, which have appropriate remote merged branches:More hardcore snippet with date and author information:Same snippets for not merged branches:It’s hard to remember such code, so the best way is to create shell scripts and put them in local “bin” folder. Note that these snippets work only in bash(and zsh). For example, let’s put first snippet into “bin/git-both-merged” file:Don’t forget to make it executable(), and you can also make a git alias for this script. Put next line in .gitconfig:Now you can call it via git command:In the same vein you can create git aliases for all snippets from this article.That’s it, folks. Now, it’s time to clean-up all stale stuff!