Skip to content

Instantly share code, notes, and snippets.

@Bhacaz
Last active February 28, 2024 14:39
Show Gist options
  • Save Bhacaz/4f289c9d1962145619ef9c9f874afdce to your computer and use it in GitHub Desktop.
Save Bhacaz/4f289c9d1962145619ef9c9f874afdce to your computer and use it in GitHub Desktop.
Personal Git alias
[alias]
s = status
branch-name = "!git rev-parse --abbrev-ref HEAD"
uncommit = !git reset --soft HEAD~1 && git s
discard-all = !git add . && git reset --hard
publish = !git push --set-upstream origin $(git branch-name)
chb = !sh -c \"git branch -a | grep $1 | head -n 1 | cut -d/ -f3- | xargs git checkout\"
nb = checkout -b
c = checkout
cm = !git add . && git commit -m
diff-master = diff --name-only origin/master...
diff-master-commit = cherry -v --abbrev=7 HEAD origin/master
alias = ! git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /
mine-review = !mine `git diff-master`
l = cherry -v --abbrev=7 master
master-revert = checkout origin/master
wip = "!git cm 'WIP' -n"
master = !git checkout master && git pull
main = !git checkout main && git pull
mm = "!git fetch && git merge origin/master --no-edit"
ammend = !git add . && git commit --amend
rm = !git fetch && git rebase origin/master
wip-branches = for-each-ref --sort='-authordate:iso8601' --count 10 --format='%(color:green)%(authordate:relative)%09%(color:yellow)%(refname:short)%(color:white) \n%09%09%(contents:subject) (%(objectname:short))' refs/heads
sync-master = "!f() { git chb \"$1\"; git mm; }; f"
fpush = push --force-with-lease
file-diff =diff --name-only origin/master...
prune-merged-branch = "!bash $HOME/Documents/code/dotfiles/git_prune_branch_merged.sh"
#!/usr/bin/env bash
# Source : https://github.com/dougthor42/dotfiles/blob/6b65e53822391e150e6163d1648c8947f6f9bd05/git_prune_squash_merged.sh
#
# This script deletes all local branches that were squashed and merged (such
# as from the GitHub / GitLab web UI).
#
# Modified from https://stackoverflow.com/a/56026209/1354930
#
# It's really only used in a git alias 'prune-squash-merged' (see
# https://stackoverflow.com/a/46435987/1354930).
#
# It supports a "dry run" mode with either the '-n' or '--dry-run' arg.
# Parse args
# See https://stackoverflow.com/a/33826763/1354930
DRY_RUN=0
HEAD_BRANCH=$(git rev-parse --abbrev-ref HEAD)
while [[ "$#" -gt 0 ]];
do
case $1 in
-n|--dry-run) DRY_RUN=1; shift ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
echo "Pruning local branches that were squashed and merged onto $HEAD_BRANCH..."
if [[ $DRY_RUN -eq 1 ]]
then
echo ""
echo "DRY RUN - no branches will be deleted.";
echo ""
fi;
# Prune local branches that we squashed and merged.
# See https://stackoverflow.com/a/56026209/1354930
git checkout -q $HEAD_BRANCH &&
git for-each-ref refs/heads/ "--format=%(refname:short)" |
while read branch;
do mergeBase=$(git merge-base $HEAD_BRANCH $branch) &&
[[ $(git cherry $HEAD_BRANCH $(git commit-tree $(git rev-parse "$branch^{tree}") -p $mergeBase -m _)) == "-"* ]] &&
if [[ $DRY_RUN -eq 1 ]]
then
LAST_COMMIT_TIME=$(git log -n 1 --pretty=format:%cr $branch)
echo "$branch ($LAST_COMMIT_TIME) - is merged into $HEAD_BRANCH and can be deleted";
else
git branch -D $branch;
fi;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment