Skip to content

Instantly share code, notes, and snippets.

@Wisdawms
Last active June 2, 2024 19:58
Show Gist options
  • Save Wisdawms/5a621892ea977df52ca75d1bbc5fc52f to your computer and use it in GitHub Desktop.
Save Wisdawms/5a621892ea977df52ca75d1bbc5fc52f to your computer and use it in GitHub Desktop.
my custom .bashrc and .gitconfig commands/aliases for git/github
# [put in ~/.bashrc (open with vim or nano)]
alias ghrmr='gh repo delete'
# create repo then push it automatically
alias ghmkr='gh repo create && git push;'
# use this command run custom sub-commands on the backup repo
bkup() {
orig_dir=$(pwd)
if cd $(pwd).bak; then
"$@"
cd $orig_dir
fi
}
# [put in ~/.gitconfig (open with vim or nano)] # for github codespaces the .gitconfig file is located in repo-path/.git/config
[alias]
# add, commit, push
acp = "!f () { git add *; git commit -a -m \"${1:-$(git status --porcelain)} # $(date '+%Y-%m-%d -%l:%M %p')\"; git push -f; }; f"
# goes back to previous commit, and makes a new commit with the reverted changes (revert previous commit into new commit)
backmk = !git revert HEAD --no-edit
# goes back to previous commit, but keeps the local files
backrm = "!git reset HEAD~1"
# goes back to previous commit and pulls those changes into local
backrmf = "!git reset --hard HEAD~1"
# goes back to the previous commit and pulls from remote into local
backrmpull = "!git reset --hard HEAD~1 && git pull"
# goes back to the previous commit and pushes those changes to the remote repo
backrmpush = "!git reset --hard HEAD~1 && git push origin HEAD --force"
# unadd staged changes
untrack = "!git restore --staged *"
# pulls head and merges it with local repo // basically updates your local repo to the remote HEAD
pullhead = "! rm ./* -rf && git restore * && echo "Pulled from HEAD and merged!""
# push local commits log to head
pushhead = "! git push -f"
# overwrites the latest commit with the current changes
backow = "!git add --all && git commit --amend --no-edit --allow-empty; git push -f"
# create backup repo from current repo
bkup = "!orig_repo=https://github.com/$(gh repo view | awk '/name:/ {print $2}'); f() { if [ -d .git ]; then echo; echo '(!) Make sure to select the second option for the first prompt.'; echo '(!) By default, the backup repo is set as private. Use (gh repo edit --visibility public) without parantheses to make it public again.'; echo; if [ ! -d ../$(basename \"$(pwd)\").bak ]; then mkdir ../$(basename \"$(pwd)\").bak && cp -r /$(pwd)/* ../$(basename \"$(pwd)\").bak && cd ../$(basename \"$(pwd)\").bak && git init && git add * && git commit -m init_backup && gh repo create; cd $(pwd); gh repo edit $(git config --get user.name)/$(basename `git rev-parse --show-toplevel`) --visibility private; gh repo edit -d 'backup repo for: '\"$orig_repo\"; echo '> Repository set to private. Use (gh repo edit --visibilty public) without parantheses to make it public again.'; echo '> See backup repository here: '\"$(tput bold)\"\"https://github.com/$(gh repo view | awk '/name:/ {print $2}')\"; else echo '(!) Backup directory already exists.'; fi else echo 'No .git directory found'; fi }; f"
# removes backup repo while in current repo
bkuprm = "!f() { if [ -d ../$(basename \"$(pwd)\").bak ]; then read -p \"$1(!) Are you sure you want to delete the backup repo entirely? [y/n]: \" response; case \"$response\" in [Yy]*) cd ../$(basename \"$(pwd)\").bak && gh repo delete --confirm && dir_name=$(basename \"$(pwd)\") && cd .. && rm -r -f $dir_name && echo '(!) Deleted backup repo.'; cd ..;; *) echo \"Operation canceled.\" ;; esac; else echo '(!) Backup directory does not exist. Please make one first using (git bkup)'; fi; }; f"
# make repo public/private
private = "!orig_repo=https://github.com/$(gh repo view | awk '/name:/ {print $2}'); \
f() \
{ ispriv=$(gh repo view --json isPrivate | jq -r '.isPrivate'); \
if ($ispriv); \
then \
echo 'This repo is already private!'; \
else \
gh repo edit --visibility private && echo '> '\"$orig_repo\"' has been set to'\"$(tput bold)\"' private.'\"$(tput sgr0)\"; \
fi \
}; \
f"
public = "!orig_repo=https://github.com/$(gh repo view | awk '/name:/ {print $2}'); \
f() \
{ ispriv=$(gh repo view --json isPrivate | jq -r '.isPrivate'); \
if ($ispriv); \
then \
gh repo edit --visibility public && echo '> '\"$orig_repo\"' has been set to'\"$(tput bold)\"' public.'\"$(tput sgr0)\"; \
else \
echo 'This repo is already public!'; \
fi \
}; \
f"
# updates the backup repo with the current uncommitted changes in the main repo
bkupdate="!orig_dir=$(pwd); f() { if cd $(pwd).bak; then if [ -d $orig_dir/.git ]; then rm * -r && cp -r -f $orig_dir/* . && git acp 'backup_update'; else echo 'No .git directory found'; fi; else echo '(!) Backup directory not found'; fi }; f"
# forking for backups
bkupfork = "!fork_repo=$bkup; mkdir ../$(basename `git rev-parse --show-toplevel`).bak && gh repo fork $(gh repo view | awk '/name:/ {print $2}') --clone=false --org=$(git config --get user.name)-org && gh repo rename --repo $(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`) $(basename `git rev-parse --show-toplevel`).bak && gh repo edit $(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`) --description 'Backup fork.' && git clone https://github.com/$(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`).bak ../$(basename `git rev-parse --show-toplevel`).bak && echo '> See backup repository here: '\"$(tput bold)\"'https://github.com/'\"$(gh repo view --json owner | jq -r '.owner.login')\"'-org/'\"$(gh repo view --json name | jq -r '.name')$(tput srg0)\";"
bkupdatefork = "!gh repo sync $(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`).bak && rm -r -f ../$(basename `git rev-parse --show-toplevel`).bak && mkdir ../$(basename `git rev-parse --show-toplevel`).bak && git clone https://github.com/$(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`).bak ../$(basename `git rev-parse --show-toplevel`).bak"
bkupforkrm = "!gh repo delete $(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`) --confirm && rm -rf ../$(basename `git rev-parse --show-toplevel`).bak"
bkupforkreset= "!gh repo sync $(git config --get user.name)-org/$(basename `git rev-parse --show-toplevel`).bak && cd ../$(basename `git rev-parse --show-toplevel`).bak; git pull && git fetch origin; git checkout main; git reset --hard origin/main"
# clones all forked repos on your github account, then deletes them
clonermforks = "!f() { \
urls=$(gh repo list --limit 200 --fork --json url | jq -r '.[].url'); \
for url in $urls; \
do \
repo_name=$(gh repo view "$url" --json parent --jq .parent.name); \
repo_owner=$(gh repo view "$url" --json parent --jq .parent.owner.login); \
fork_url=https://github.com/$repo_owner/$repo_name; \
git clone "$fork_url"; \
gh repo delete "$url" --confirm; \
done; \
}; f"
# restore local repo from backup repo
bkuprestore="!orig_dir=$(pwd) && if [ -d $orig_dir/.git ]; then bkup_url=$(gh repo view --json url | jq -r '.url').bak && rm * -rf && git clone $bkup_url && cp $orig_dir/$(basename `git rev-parse --show-toplevel`).bak/* $orig_dir -rf && rm -rf $orig_dir/$(basename `git rev-parse --show-toplevel`).bak; else echo \"$(tput bold)\"'(!) No git dir here!'; fi;"
# restore local repo from backup fork repo
bkuprestorefromfork="!orig_dir=$(pwd); org_owner_repo=$(gh repo view --json owner | jq -r '.owner.login')-org/$(gh repo view --json name | jq -r '.name') && if [ -d $orig_dir/.git ]; then if [ -n $(gh api repos/$org_owner_repo | jq -r '.id') ]; then bkup_url=$(gh api repos/$org_owner_repo | jq -r '.svn_url') && rm * -rf && git clone $bkup_url && cp $orig_dir/$(basename `git rev-parse --show-toplevel`).bak/* $orig_dir -rf && rm -rf $orig_dir/$(basename `git rev-parse --show-toplevel`).bak; else echo \"$(tput bold)\"'(!) No git dir here!'; fi; fi;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment