Last active
April 25, 2022 14:28
-
-
Save rbeer/5cb9f09ee7b344aa1bc3be823ddabf07 to your computer and use it in GitHub Desktop.
Handy aliases for your everyday git workflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Install | |
# 1. Download this file to ~/.git_aliases | |
# 2. Add `source ~/.git_aliases` to your ~/.bash_aliases or ~/.bashrc file | |
### | |
# git | |
# These aliases follow a simple scheme of 'git' and then | |
# the first letters of the command and its options. | |
# There are a few exceptions, like `gitaa` which means | |
# git add (all), i.e. `git add .` | |
# Those edge cases are commented. | |
## source git's shell helpers | |
. /usr/share/bash-completion/completions/git | |
# This creates a bare repositor in REPO_PATH (default: /opt/git/*) | |
# REMOTE_PATH is set to expect an ssh host/repo remote named git.local | |
# Owner is the user `git`; set up your ssh accordingly! | |
_create_bare_git_remote() { | |
local REPO_PATH=/opt/git/$1.git | |
local REMOTE_PATH=git.local:$REPO_PATH | |
git init --bare $REPO_PATH | |
sudo chown -R git:git $REPO_PATH | |
sudo setfacl -R -m g:git:rwX $REPO_PATH | |
find $REPO_PATH -type d | xargs sudo setfacl -R -m d:g:git:rwX | |
git remote add local $REMOTE_PATH | |
git remote -v | |
} | |
alias gitcbr=_create_bare_git_remote | |
# Show changes made at a specific commit | |
_git_diff_at() { | |
git diff "$1~" "$1" | |
} | |
alias gitd="git diff" | |
# git diff (at commit) | |
alias gitdat=_git_diff_at | |
# git diff (last commit) | |
alias gitdl="gitd HEAD~ HEAD" | |
alias gitdno="git diff --name-only" | |
alias gitds="git diff --staged" | |
alias gita="git add" | |
# git add (all) | |
alias gitaa="gita ." | |
alias gitap="gita -p" | |
alias gitc="git commit" | |
alias gitcm="git commit -m" | |
alias gitca="git commit --amend" | |
alias gitcane="git commit --amend --no-edit" | |
alias gitcmnv="git commit -m --no-verify" | |
# default git log commit shows the last 7 commits, only | |
alias gitl="gitla -7" | |
# git log (all) - using oneline, abbreviated hashes by default | |
alias gitla="git log --pretty=oneline --abbrev-commit" | |
# `gitln 10` for the last 10 commits | |
alias gitln="gitl -n" | |
# git log (long) - in case you need to see the commit message, not only the title | |
alias gitll="git log --abbrev-commit" | |
# git log (update) - this shows all commits since last version's tag | |
alias gitlu="gitl $(git tag | grep -P "v\d{1,}\..*" | tail -1)^..HEAD" | |
alias gitrl="git reflog" | |
gits() { | |
script -q /dev/null -c "git status --short $1" | sort -r | |
} | |
# git (check)(out) - gitc was taken by `commit` :P | |
alias gitco="git checkout" | |
alias gitcob="gitco -b" | |
alias gitba="git branch -a" | |
alias gitbd="git branch -d" | |
alias gitbD="git branch -D" | |
alias gitbm="git branch -m" | |
alias gitbr="git branch -r" | |
alias gitspm="git stash push -m" | |
alias gitski="git stash --keep-index" | |
alias gitsl="git stash list" | |
alias gitrbi="git rebase -i" | |
alias gitrbc="git rebase --continue" | |
alias gitrv="git remote -v" | |
alias gitrs="git reset" | |
alias gittl="git tag -l" | |
# Not really envouge anymore, but it's nice to have | |
# it for projects that use gitflow | |
alias gitffs="git flow feature start --showcommands $1" | |
alias git3f="git flow feature finish --showcommands $1" | |
alias gitfl="git flow log" | |
# This allows to commit a file that is usually ignored | |
# Rarely ever needed | |
git_commit_untracked(){ | |
echo "Adding files back to index" | |
git update-index --no-skip-worktree $@ | |
gita -f $@ | |
gitc | |
echo "Ignoring files, after commit" | |
git update-index --skip-worktree $@ | |
echo "Ignored files:" | |
git ls-files --ignored --exclude-standard | |
} | |
alias gitcu=git_commit_untracked | |
alias gitsp="git stash pop" | |
# Enable aliases to autocomplete within their context | |
__git_complete gitb _git_branch | |
__git_complete gitbm _git_branch | |
__git_complete gitbd _git_branch | |
__git_complete gitbD _git_branch | |
__git_complete gitco _git_checkout | |
__git_complete gitd _git_diff | |
__git_complete gitdat _git_branch | |
__git_complete gitdno _git_branch | |
__git_complete gitll _git_log | |
__git_complete gitla _git_log | |
__git_complete gitl _git_log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment