Skip to content

Instantly share code, notes, and snippets.

@weaming weaming/git-handy.sh
Last active May 10, 2019

Embed
What would you like to do?
# vim: filetype=sh
# git
alias gconf='vi .git/config'
alias gamend='git commit --amend'
alias gamendy='git commit --amend --no-edit'
alias gback='git reset --hard HEAD~'
alias gl='git lg'
function greset() {
git reset --hard "${1:-HEAD}"
}
git-setup-global-config() {
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset %Cgreen(%cr)%Creset -%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global push.followTags true
git config --global push.default current
}
git-setup-global-user-name-and-email() {
echo 'The old global config:'
git config --global user.name
git config --global user.email
local name=${1:?'missing user.name'}
local email=${2:?'missing user.email'}
git config --global user.name "$name"
git config --global user.email "$email"
}
git-remove-big-files() {
# https://gist.github.com/shennyg/1047737
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $@" HEAD
echo ' Maybe you want to execute:'
echo ''
echo ' rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune'
echo ' git push --all --prune --force'
echo ''
echo ' Once you pushed, all your teammates need to clone repository again'
}
git-current-branch() {
local prefix=${1-''}
echo -n "$prefix$(git rev-parse --abbrev-ref HEAD)"
}
git-reset-origin-same-branch() {
git reset --hard origin/$(git-current-branch)
}
alias gcl='git clone'
alias gpl='git pull && git submodule update --init'
alias gplr='git pull --rebase'
alias gplp='git pull --prune'
alias gplpr='git pull --prune --rebase'
alias gcl='git clone'
alias gps='git push'
alias glg='git log'
alias gs='git status'
alias gd='git diff'
alias gdiff='git diff --no-index'
alias gdc='git diff --cached'
alias gdl='git diff HEAD~'
alias ga='git add -A'
alias gm='git commit -m'
alias gma='git commit -am'
alias gmerge='git merge --no-ff'
alias gb='git branch'
alias gbc='git branch --contains'
alias gc='git checkout'
alias gra='git remote add'
alias grr='git remote rm'
alias grv='git remote -v'
alias grl='git remote get-url $(git remote)'
alias grlc='grl | pbcopy'
alias gta='git tag -a -m'
alias gf='git reflog'
alias gcp='git cherry-pick'
alias gsu='git submodule update'
alias grcontinue='git rebase --continue'
alias grabort='git rebase --abort'
git-submodule-update-all() {
git submodule update --init
if [[ "$1" == "clean" ]]; then
echo "git clean -fd ($PWD)"
git clean -fd
fi
for x in $(git submodule | awk '{print $2}'); do
local name="$x"
if [ -d "$name" ]; then
echo $(green "-> $x")
pushd "$name" 1>/dev/null 2>&1
git-submodule-update-all "$1"
popd 1>/dev/null 2>&1
echo $(red "<- $name")
fi
done
}
git-submodule-branch() {
local submodule=${1:-'.'}
local depth=${2:-'0'}
echo "$(str_repeat ' ' $depth) -> $submodule $(green $(git-current-branch))"
local x
for x in $(git submodule | grep -v -E './$' | awk '{print $2}'); do
local name="$x"
if [ -d "$name" ]; then
pushd "$name" &>/dev/null
git-submodule-branch "$name" $((depth+1))
popd &>/dev/null
fi
done
}
git-rebase-remote() {
local branch=${1:?'missing remote branchname, e.g. origin/feature/awesome'}
git fetch && git rebase "$branch"
}
git-pull-rebase-and-rebase-on-local() {
local branch=${1:?'missing local branchname, maybe develop or master'}
git pull --rebase && git rebase "$branch"
}
git-fetch-and-reset-to() {
local branch=${1:?'missing remote branch. e.g. origin/feature/awesome'}
git fetch && git reset --hard "$branch"
}
git-rebase-and-merge() {
local branch=${1:?'missing local branchname, maybe develop or master'}
git rebase "$branch" && \
git checkout "$branch" && \
git merge --no-ff "$(git-current-branch)"
}
gitignore-get()
{
local url="https://raw.githubusercontent.com/github/gitignore/master/$1.gitignore"
type wget &>/dev/null && wget $url -O .gitignore || curl -sSL $url > .gitignore
}
git-proxy()
{
if [ -z "$1" ]; then
git config --get http.proxy
git config --get https.proxy
else
git config --global http.proxy "$1"
git config --global https.proxy "$1"
fi
}
git-noproxy()
{
git config --global --unset http.proxy
git config --global --unset https.proxy
}
git-force-sync-origin-same-branch()
{
git fetch && git-reset-origin-same-branch
}
git-stat-username()
{
git log --author="$1" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
}
git-stat-all-by-lines()
{
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
}
git-stat-top-by-commits()
{
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5
}
git-commit-with-now() {
git commit -m "$(date +'%Y-%m-%d %X')"
}
git-rewrite-committer() {
git filter-branch -f --env-filter '
OLD_EMAIL="jiadeng@16financial.com"
CORRECT_NAME="weaming"
CORRECT_EMAIL="garden.yuen@gmail.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
}
git-setup-project-committer() {
local script_path=~/.dotfiles/zsh/configure_git_committer.py
local name email rc1 rc2
name=$($script_path workdir name)
rc1=$?
email=$($script_path workdir email)
rc2=$?
if [ "$rc1" -eq 0 ] && [ "$rc2" -eq 0 ]; then
if [[ -d '.git' ]] && [[ "$name" != "$(git config user.name)" ]]; then
echo $(green "Setup git user & email:")
git config user.name $name
echo "$(git config user.name)"
git config user.email $email
echo "$(git config user.email)"
fi
fi
}
git-write-committers-config() {
local script_path=~/.dotfiles/zsh/configure_git_committer.py
python3 $script_path write_example
}
git-ignore-file-change() {
git update-index --assume-unchanged $1
}
git-rename-local-and-remote-branch() {
local old=${1?'missing old branch name'}
local new=${2?'missing new branch name'}
git branch -m "$old" "$new"
git push origin :"$old" "$new"
}
function git-sort-and-count-on-committer() {
xargs -I{} git log --diff-filter=A -- {} | grep Author | sort | uniq -c
}
function django-count-migrations-on-committer() {
ls **/migrations/*.py | git-sort-and-count-on-committer
}
git-log-file() {
local file=${1:?'missing file'}
git log -p "$file"
}
git-copy-url() {
git remote -v | col 2 | index 1 | pbcopy
}
git-annotate-tag() {
local tag=${1?'missing tag'}
local commment=${2?'missing comment'}
if [[ "$GIT_PGP_SIGN" == "" ]]; then
git tag -a "$tag" -m "$commment"
else
git tag -s "$tag" -m "$commment" && \
echo "added tag $tag with gpg key"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.