Skip to content

Commit

Permalink
cleanup, add comments, add "primary branch" support
Browse files Browse the repository at this point in the history
  • Loading branch information
nonrational committed Nov 1, 2024
1 parent 2e12791 commit 4a6dce8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
13 changes: 8 additions & 5 deletions .gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# http://michael-kuehnel.de/git/2014/11/21/git-mac-osx-and-german-umlaute.html
precomposeunicode = false
[alias]
opb = "!. ~/.githelpers && remote_origin_primary_branch"
pb = "!. ~/.githelpers && remote_origin_primary_branch | sed 's|origin/||g'"

pp = push
pro = "!hub pr show || hub browse"

Expand All @@ -46,13 +49,16 @@
ci = commit -v
co = checkout

wip = commit -v -m'wip'

df = "diff --name-only"
dfo = "df origin/master"
dfo = "!git df $(git opb)"
dc = diff --cached

f = "fetch --tags --force --prune"
ri = "rebase -i"
rio = "!git f && git rebase -i origin/master"
# use "primary branch" to play nice with origin/main and origin/master
rio = "!git f && git rebase -i $(git opb)"
mov = "branch -m"

head = !git r -1
Expand All @@ -67,9 +73,7 @@
bs = "!. ~/.githelpers && pretty_git_branch_sorted"
brr = branch --sort=-committerdate


lt = "!git tag | sort -n -t. -k1,1 -k2,2 -k3,3"
stag = "!git tag | sort -n -t. -k1,1 -k2,2 -k3,3 -k4,4"

amend = commit --amend
aa = add --all .
Expand All @@ -94,7 +98,6 @@
broom = "!. ~/.githelpers && delete_remoteless_branches_interactive"
spring-cleaning = "!. ~/.githelpers && delete_all_my_branches_interactive"

wtf = "!. ~/.githelpers && what_the_hell_just_happened"
nuke = "!git reset --hard HEAD && git clean -fd"

aww = "!. ~/.githelpers && add_wildcard"
Expand Down
16 changes: 14 additions & 2 deletions .githelpers
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Based on Gary Bernhardt's .githelpers
# https://github.com/garybernhardt/dotfiles/blob/master/.githelpers
# https://github.com/garybernhardt/dotfiles/blob/main/.githelpers

# Log output:
#
Expand Down Expand Up @@ -37,6 +37,12 @@ BRANCH_CONTENTS="%(contents:subject)"

BRANCH_FORMAT="$BRANCH_PREFIX^$BRANCH_REF^$BRANCH_HASH^$BRANCH_DATE^$BRANCH_AUTHOR^$BRANCH_CONTENTS"

# Print the name of the "primary" branch, be it main or master.
remote_origin_primary_branch() {
git branch -a | grep 'remotes.origin.\(main\|master\)' | sed 's|.*remotes/||g'
}

# Show a diff of the most recent commit.
show_git_head() {
pretty_git_log -1
git show -p --pretty="tformat:"
Expand Down Expand Up @@ -73,18 +79,21 @@ git_page_maybe() {
fi
}

# Accepts a pattern and attempts to stage all files that match it.
add_wildcard(){
git ls-files --modified | grep -i $1 | xargs -I{} git add {}
git ls-files --others --exclude-standard | grep -i $1 | xargs -I{} git add {}
git ls-files --deleted | grep -i $1 | xargs -I{} git rm {}
}

# Accepts a pattern and attempts to unstage all files that match it.
reset_wildcard(){
git ls-files --modified | grep -i $1 | xargs -I{} git checkout {}
git ls-files --others --exclude-standard | grep -i $1 | xargs -I{} git checkout {}
git ls-files --deleted | grep -i $1 | xargs -I{} git checkout HEAD {}
}

# Accepts a pattern and switches to the first branch that matches it.
switch_wildcard() {
if [ -z "$1" ]; then
echo "No pattern provided"
Expand All @@ -93,10 +102,12 @@ switch_wildcard() {
fi
}

# Delete local-only branches with confirmation.
delete_remoteless_branches_interactive(){
python3 $HOME/bin/git-broom.py
/usr/bin/python3 $HOME/bin/git-broom.py
}

# Delete all personally-prefixed non-primary branches with confirmation.
delete_all_my_branches_interactive(){
for branch in $(git branch | egrep '^\W*(aln|anorton|alan)/'); do
printf "Delete $branch, both local and origin? ";
Expand All @@ -105,6 +116,7 @@ delete_all_my_branches_interactive(){
done
}

# Accepts a sed substituion (e.g., s/foo/bar/) and applies it to the current branch, also unsetting the upstream branch.
rename_branch_regex(){
if [ -z "$1" ]; then
echo "No pattern provided"
Expand Down

0 comments on commit 4a6dce8

Please sign in to comment.