티스토리 뷰

Git

[Git] 편리한 git alias 사용하기

니용 2022. 9. 24. 08:28
반응형

이번 글에서는 이전에 사용하였던 git 에서 alias 라는 별칭을 두어 새롭게 관리하는 방법을 알아보고자 합니다. 가장 먼저 글을 작성하는데 많은 도움이 되었던 글 링크를 먼저 남기고 글을 시작할까 합니다.

글 링크 : https://johngrib.github.io/wiki/git-alias/

 

편리한 git alias 설정하기

나만의 git alias를 만들어 보자

johngrib.github.io

 

Git Alias 정의

일반적으로 우리는 commit, checkout 명령어를 쓸 떄 아래와 같이 사용합니다. 

  • git commit -m {message}
  • git checkout {branch} 

이렇게 사용하는 방법을 alias를 사용하면 다른 방법으로 사용 가능합니다. 

  • git ci -m {message}
  • git co {branch}

즉 타자치는 양을 줄여 더 편리하게 git 관리가 가능하다는 장점이 있습니다.

 

 

최종으로 완성된 ~/.gitconfig 파일 내의 alias 소스코드입니다.

[alias]
    alias-basic = "!#----------------------------------------------------------;\n\
        git alias | head -7"
    ci = commit
    co = checkout
    sw = switch
    re = restore
    s = status -s
    assume   = update-index --assume-unchanged
    assumed  = "!git ls-files -v | grep ^h | cut -c 3-"
    unassume = update-index --no-assume-unchanged

    alias-log = "!#----------------------------------------------------------;\n\
        git alias | egrep 'log|commit-'"
    commit-select = "!# Select a commit hash from log graph.;\n\
        git l \
        | fzf -m --ansi --layout=reverse --preview=\"echo {} | sed 's/^[*| ]*//g' | cut -d' ' -f1 | xargs git show --color=always \" \
        | sed 's/^[*| ]*//g' | cut -d' ' -f1"
    c-s = "!git commit-select"
    commit-copy = "!# Select & copy a commit hash from log graph.;\n\
        git commit-select | tr -d '\n' | pbcopy && echo Copied: `pbpaste`;"
    c-c = "!git commit-copy"
    l = "log \
        --color --graph --decorate \
        --date=format:'%Y-%m-%d' \
        --abbrev-commit \
        --pretty=format:'%C(red)%h%C(auto)%d %s %C(green)(%cr)%C(bold blue) %an'"
    ld = "log \
        --color --graph --decorate \
        --date=format:'%Y-%m-%d %H:%M:%S' \
        --abbrev-commit \
        --pretty=format:'%C(red)%h%C(auto)%d %s %C(green)(%ad)%C(bold blue) %an'"
    ll = "log \
        --color --graph --decorate \
        --date=format:'%Y-%m-%d' \
        --abbrev-commit \
        --pretty=format:'%C(red)%H%C(auto)%d %s %C(green)(%cr)%C(bold blue) %an'"
    lld = "log \
        --color --graph --decorate \
        --date=format:'%Y-%m-%d %H:%M:%S' \
        --abbrev-commit \
        --pretty=format:'%C(red)%H%C(auto)%d %s %C(green)(%ad)%C(bold blue) %an'"
    lh = "!# Show log graph | head \n\
        f() { \
            if [ $# = 0 ]; then \
                git l | head -25; \
            else \
                git l | head -$1; \
            fi; \
            echo ''; \
        }; f"
    ranking = "!# List commit counts of contributors;\n\
        git shortlog -sn"

    alias-branch = "!#----------------------------------------------------------;\n\
        git alias | egrep '[bB]ranch'"
    b0 = "!# Print current branch.;\n\
        git branch | awk '/^\\*/{print $2}'"
    back = "!# Back up current branch.;\n\
        echo created new branch: backup-`git b0`;\
        git branch backup-`git b0`"
    bb = "!# Branch tools. Type 'git bb help'.;\n\
        f() { \n\
            if [ $# = 0 ]; then \
                git branch-select | xargs git checkout; \
            elif [ $1 = 'help' ]; then \
                echo 'git bb           : Select and checkout branch'; \
                echo 'git bb c, clean  : Clean all merged branches'; \
                echo 'git bb d, D      : Delete seleted branches(D: force)'; \
                echo 'git bb l, list   : List branches excluding the current branch'; \
                echo 'git bb m, merged : List merged branches excluding the current and master branches'; \
            elif [ $1 = 'd' -o $1 = 'D' ]; then \
                git branch -$1 $(git bb list | fzf -m | awk '{print $2}'); \
            elif [ $1 = 'clean' -o $1 = 'c' ]; then \
                git branch-clean; \
            elif [ $1 = 'list' -o $1 = 'l' ]; then \
                git branch-list; \
            elif [ $1 = 'merged' -o $1 = 'm' ]; then \
                git branch-merged; \
            elif [ $1 = 'select' -o $1 = 's' ]; then \
                git branch-select; \
            else \
                git bb help; \
            fi; \
        }; f"
    branch-clean = "!# Search and delete merged branches.;\n\
        curr_hash=`git show -s | head -1 | cut -d ' ' -f2`; \
        for branch in `find .git/refs -type f | fzf --ansi -m --preview=\"cat {} | xargs git l\"` ; do \
            hash=`cat $branch`; \
            if [ $hash = $curr_hash ]; then \
                continue; \
            fi; \
            git ll | egrep $hash -C 1; \
            read -p \"Delete $branch? [y|n] \" -r; \
            REPLY=${REPLY:-"n"}; \
            if [ $REPLY = \"y\" ]; then \
                rm $branch; \
                echo \"\\033[32m$branch \\033[0mhas been\\033[31m deleted\\033[0m.\n\"; \
            fi; \
        done"
    b-c = "!git branch-clean"
    branch-list = "!# List the branches.;\n\
        git for-each-ref --sort=-committerdate refs/heads --format='%(HEAD) %(refname:strip=2) | %(committerdate:relative) | %(authorname)' \
        | column -ts'|' \
        | sed 's/^ /./'"
    b-l = "!git branch-list"
    branch-list-all = "!# List all branches.;\n\
        for branch in `git branch -r --merged | grep -v HEAD`; do echo `git show --format=\"%ci ; %cr ; %an ;\" $branch | head -n 1` $branch; done | sort -r | column -ts';'"
    b-la = "!git branch-list-all"
    branch-select = "!# Select a branch.;\n\
        f() { \
            _height=$(stty size | awk '{print $1}');\
            git branch-list | fzf --preview \"git l {2} | head -n $_height\" | awk '{print $2}'; \
        }; f"
    b-s = "!git branch-select"
    ch = "!# Branch tools. Same with bb.;\n\
        git bb $1"
    sync = "!# Sync with a branch with the same name in the remote repo.;\n\
        remote=$( \
            { \
                git remote -v | egrep '^(origin|upstream)\\s'; \
                git remote -v | egrep -v '^(origin|upstream)\\s' | sort; \
            } \
            | awk '{print $1, $2}' | uniq \
            | column -t \
            | fzf | awk '{print $1}' \
        ); \
        if ! [ -z $remote ]; then \
            git fetch $remote && git reset --hard $remote/`git b0`; \
        fi"
    update = "!# Synchronize the contents of local files with the repository;\n\
        remote=$( \
            { \
                git remote -v | egrep '^(origin|upstream)\\s'; \
                git remote -v | egrep -v '^(origin|upstream)\\s' | sort; \
            } \
            | awk '{print $1, $2}' | uniq \
            | column -t \
            | fzf | awk '{print $1}' \
        ); \
        git stash; git pull --rebase $remote `git b0`; git stash pop;"
    alias-stage = "!#----------------------------------------------------------;\n\
        git alias | egrep '(add|diff|stage)' -i"
    a = "!# Select files and Add them.;\n\
        git diff-select | xargs git add"
    a-c = "!# Add, Commit.;\n\
        git a; git commit"
    diff-info = "!# Print diff report.;\n\
        fileA=/tmp/git-s-$(uuidgen); \
        fileB=/tmp/git-diff-$(uuidgen); \
        git s | awk '{print $2,$1}' > $fileA; \
        git diff --numstat | awk '{print $3,$1,$2}' > $fileB; \
        join -t' ' -a 1 $fileA $fileB | awk '{print $2, \"(+\"$3 \",-\"$4\")\", $1}' | sed 's/(+,-)/./; s/^\\([^?]\\) *\\./\\1 STAGED/' | column -t -s' ' ; \
        rm -f $fileA $fileB; \
    "
    diff-select = "!# Select changed files to add them.;\n\
        f() { \
            git diff-info \
            | egrep -v '[^?] *STAGED ' \
            | fzf -m --header \"$(git diff --shortstat)\" --preview \
                \"if [[ {1} == '??' ]]; then bat {3}; else git diff --color=always {3}; fi\" \
            | awk '{print $3}'; \
        }; f"
    diff-unselect = "!# Select staged files to unstage them.;\n\
        f() { \
            git diff-info \
            | egrep '[^?] *STAGED ' \
            | fzf -m --header \"$(git diff --shortstat)\" --preview \
                \"if [[ {1} == '??' ]]; then pygmentize {3}; else git diff --color=always --cached {3}; fi\" \
            | awk '{print $3}'; \
        }; f"
    unstage = "!# Select staged files and Unstage them.;\n\
        git diff-unselect | xargs git reset HEAD"
    alias-stash = "!#----------------------------------------------------------;\n\
        git alias | grep stash"
    stash-apply = "!# Select a stash item and Apply it.;\n\
        git stash-op apply"
    s-a = "!git stash-apply"
    stash-drop= "!# Select the stash items and Drop them.;\n\
        for sid in $(git stash-select -m) ; do \
            git stash drop $sid; \
        done;"
    s-d = "!git stash-drop"
    stash-list = "!# List stash items.;\n\
        git stash list --pretty=format:\"%C(red)%gd%C(reset) %C(green)(%cr) %C(reset)%s\""
    s-l = "!git stash-list"
    stash-pop= "!# Select a stash item and Pop it.;\n\
        git stash-op pop"
    s-p = "!git stash-pop"
    stash-save = "!# Save changes into the stash stack.;\n \
        git diff-info; \
        read -p \"save message: \" msg; \
        git stash save \"$msg\""
    s-s = "!git stash-save"
    stash-select = "!# Select stash item(s).;\n\
        f() { \
            git stash-list \
            | fzf --ansi $1 --preview \"git stash show -p {1} --color=always\" \
            | cut -d' ' -f1; \
        }; f"
    stash-op = "!#A private stash tool.;\nf() { git stash-select | xargs git stash $1; }; f"
    tag-refresh = "!# Re reference tag.;\n \
        f() { \
            _height=$(stty size | awk '{print $1}');\
            tag_name=`git tag | fzf --preview=\"git l {1} | head -n $_height\" `; \
            echo $tag_name; \
            git tag -d $tag_name; \
            git tag $tag_name; \
        }; f"
    alias-alias = "!#----------------------------------------------------------;\n\
        git alias | grep alias"
    alias = "!# Prints all aliases.;\n\
        git config --list | egrep '^alias.+' | sed -e 's/^alias\\.//' | sed -e 's/^[^=]*=/\\'$'\\033[31m&\\033[(B\\033[m/' | column -t -s'=' | sed 's/!#* *//; s/;$//' | cut -c1-85"
    alias-doctor = "!# Check the dependency tools.;\n\
    f() { \
        if [ $(which pygmentize | wc -l) -lt 1 ]; then \
            echo 'Not found : Pygments(pygmentize)'; \
            echo '    see     : http://pygments.org/'; \
            echo '    install : pip3 install Pygments'; \
            echo '';\
        fi; \
        if [ $(which fzf | wc -l) -lt 1 ]; then \
            echo 'Not found : fzf'; \
            echo '    see          : https://github.com/junegunn/fzf#installation'; \
            echo '    install(Mac) : brew install fzf'; \
            echo '    install(git) : git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf'; \
            echo '                   ~/.fzf/install'; \
            echo '';\
        fi; \
    }; f"
    knot = "!# Merge two commits with 1 depth.;\n\
    f() { \
        if [ $(git status --short | egrep -v '^\\?' | wc -l) -gt 0 ]; then \
            echo '변경사항을 먼저 정리해 주세요.';\
            return; \
        fi; \
        _branch=`git b0`; \
        _current=backup-`uuidgen`; \
        _knot1=`uuidgen`; \
        _knot2=`uuidgen`; \
        git tag $_current; \
        git switch -c $_knot2 $2; \
        git switch -c $_knot1 $1; \
        git merge $2 --no-ff; \
        git switch $_branch; \
        git rebase -i -r $_knot1; \
        git branch -D $_knot1; \
        git branch -D $_knot2; \
        read -p \"Delete backup tag $_current ? [y|n] \" -r; \
        REPLY=${REPLY:-"n"}; \
        if [ $REPLY = \"y\" ]; then \
            git tag -d $_current; \
        fi; \
    }; f"
[core]
    excludesfile = /Users/johngrib/.gitignore_global
    editor = /opt/homebrew/bin/nvim
    autocrlf = input
    precomposeunicode = true
    quotepath = false
    ; pager = bat
[merge]
    ; conflictstyle = diff3
    trustExitCode = true
[diff]
    ; colorMoved = default
    tool = vimdiff
    wsErrorHighlight = all
[difftool "vimdiff"]
    path = /opt/homebrew/bin/nvim
[mergetool "vimdiff"]
    path = /opt/homebrew/bin/nvim
    trustExitCode = true

 

반응형
댓글
공지사항