Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

These aught to go into gitalias

strip spaces

[filter "trimWhitespace"]
    clean = git-stripspace

To find commit SHA by tag (on current branch)

git rev-list -n 1 rc-2018.6.2.11

To comapre commits between two branches use:

git log --left-right --graph --cherry-pick --oneline feature...branch

remove that are merged

Ref: https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged?page=1&tab=votes#tab-top

You can add other branches to exclude like master and dev if your workflow has those as a possible ancestor. Usually I branch off of a "sprint-start" tag and master, dev and qa are not ancestors.

First, list all branches that were merged in remote.

git branch --merged

You might see few branches you don't want to remove. we can add few arguments to skip important branches that we don't want to delete like master or a develop. The following command will skip master branch and anything that has dev in it.

git branch --merged| egrep -v "(^\*|master|dev)"

If you want to skip, you can add it to the egrep command like the following. The branch skip_branch_name will not be deleted.

git branch --merged| egrep -v "(^\*|master|dev|skip_branch_name)"

To delete all local branches that are already merged into the currently checked out branch:

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

You can see that master and dev are excluded in case they are an ancestor.

You can delete a merged local branch with:

git branch -d branchname

If it's not merged, use:

git branch -D branchname

To delete it from the remote in old versions of Git use:

git push origin :branchname

In more recent versions of Git use:

git push --delete origin branchname

Once you delete the branch from the remote, you can prune to get rid of remote tracking branches with:

git remote prune origin

or prune individual remote tracking branches, as the other answer suggests, with:

git branch -dr branchname

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.