Git Clean-Up Commands

Sometimes you may find a git repository with a lot of branches and not all of them necessary.

Because of that, you may want to do some cleanup, so I’ve prepared a list of very useful commands:

  • Remove Merged Branches

just to see them / or number them

git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: | xargs -n 1 echo

git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: | wc -l

and to really delete those branches

git branch -r --merged origin/master | grep -v "^.*master" | sed s:origin/:: | xargs -n 1 git push origin --delete 

* these commands take care of the `master branch. If you need to keep another branch, just change the grep -v “^.*master”

  • Remove every remote (beside ‘master)

even much simple + you can use similar commands to see/number these branches

git branch -r | grep -v "^.*master" | sed s:origin/:: | xargs -n 1 git push origin --delete 

How to get the diff between all the commits that occurred on a specified timeframe in Git?

Sometimes you find out from QA department that starting with a specific date an endpoint from your application started to work a little bit slowly

if that date is last week, or I don’t know, it happen last month, the simple solution it will be to do an export of all commit between two specific days

git log -p master@{2016-06-02}..master@{2016-06-02}

git log -p master@{2016-06-02}..master@{now}

git log -p master@{1.hour.ago}..master@{now}

for more info see this page
git-log