Skip to content

Instantly share code, notes, and snippets.

@nivb52
Last active June 22, 2022 13:26
Show Gist options
  • Select an option

  • Save nivb52/6f66f4ec58e85267758d40219aa390e8 to your computer and use it in GitHub Desktop.

Select an option

Save nivb52/6f66f4ec58e85267758d40219aa390e8 to your computer and use it in GitHub Desktop.
git-commands

CLEAN BRANCHES

in the git config: git config --global alias.clean-branches "!git branch | grep -v master | xargs git branch -D" or by edit the config file : [alias] # remove all but the mention git trim = !git branch | grep -v -E 'master|main|development|integration|production' | xargs git branch -D

How can I delete all Git branches which have been merged?

https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged # remove all branches which not merged to master cleanup = "!git branch --merged master | grep -v '\*\|master\|main|production' | xargs -n 1 -r git branch -d"

Linux

  • Deletes everything except master and the branch I am currently in, just in case.
    alias gbDA='git branch | egrep -v "(master|\*)" | xargs git branch -D
  • This will remove branches named (e.g.) test-master, master-test:
 git branch | grep -ve " master$" | xargs git branch -D

Win

git branch | select-string -notmatch master | foreach {git branch -d ("$_").Trim() --force}

more than 1 branch can be added to the grep expression like "master|develop|current_branch"

Some git Config Commands

  rename = branch -m
  branches = branch --list	
  • squash: get all the comments, usage: git squash 3
  • squash-m: spesific comment, usage: git squash 3 "feature branch"
  • squash-all: use all branch commits, usage: git squash "all my feature branch commits"
  squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
  squash-m = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"${2}\"; };f"
  squash-all = "!f(){ git reset --soft HEAD~$(git count-commits) && git commit --edit -m\"$(1))\"; };f"
#	squash-all-branch = "!f(){ git reset --soft HEAD~$(git count-commits) && git commit --edit -m\"$(sed 's/[A-Z]\+/\ &/g' <<< $(git branch --show-current)))\"; };f"
	count-commits = rev-list @{1}..HEAD --count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment