Article

Git Alias로 개발 생산성 2배 높이기

Git 명령어의 생산성 문제

Git은 분명 강력한 도구이지만, 명령어가 길고 복잡합니다. git checkout -b feature/user-auth origin/develop처럼 자주 사용하는 명령어조차 타이핑이 길고 오류가 나기 쉽습니다. 이런 반복적인 명령어를 alias(단축어)로 등록하면 타이핑 횟수를 줄이고 오류를 방지할 수 있습니다.

Alias는 단순한 편의 기능이 아니라, 개발자의 시간과 집중력을 아끼는 중요한 도구입니다. 하루에 50번 사용하는 명령어라면, alias를 사용했을 때 절약되는 시간은 연간 몇 시간에 달합니다.

참고: 종립님의 깃 alias를 기반으로 개인 맞춤형으로 수정했다.

전체 코드: gist.github.com/yousung/515eeec8268544056c1a5664885e754e

기본 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]
    blist = "!git branch | grep -v '^\\*'"
    blist-merged = "!git branch --merged | grep -v '^\\*\\|\\<master$'"
    bclean = "!# current branch delete.; \n\
        f() { \
            branch=`git b0`; \
            git checkout master; \
            read -p \"Delete local branch $branch? [y|n] \" -r; \
            REPLY=${REPLY:-\"n\"}; \
            if [ $REPLY = \"y\" ]; then \
                git branch -D $branch; \
            fi; \
            read -p \"Delete origin branch $branch? [y|n] \" -r; \
            REPLY=${REPLY:-\"n\"}; \
            if [ $REPLY = \"y\" ]; then \
                git push origin --delete $branch; \
            fi; \
        }; f"
    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`"
    done = "!git branch --merged | grep -v '^\\*\\|\\<master$'"
    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 selected 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'; \
            fi; \
        }; f"
    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/^ /./'"
    bl = "!git branch-list"
    b-s = "!git branch-select"
    sync = "!# Sync with a branch with the same name in the remote repo.;\n\
        git pull --rebase origin `git b0`;"

로그 조회

[alias]
    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'"
    ll = "log \
        --color --graph --decorate \
        --date=format:'%Y-%m-%d' \
        --abbrev-commit \
        --all \
        --pretty=format:'%C(red)%h%C(auto)%d %s %C(green)(%cr)%C(bold blue) %an'"
    ranking = "!# List commit counts of contributors;\n\
        git shortlog -sn"
    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 | pbcopy && echo Copied : `pbpaste`;"
    c-c = "!git commit-copy"

스테이징 및 Diff

[alias]
    a = "!# Select files and Add them.;\n\
        git diff-select | xargs git add"
    a-c = "!# Add, Commit.;\n\
        git a; git commit"
    diff-select = "!# Select changed files to add them.;\n\
        f() { \
            git diff-info \
            | egrep -v '[^?] *STAGED ' \
            | fzf -m --preview \
                \"if [[ {1} == '??' ]]; then bat {3}; else git diff --color=always {3}; fi\" \
            | awk '{print $3}'; \
        }; f"
    unstage = "!# Select staged files and Unstage them.;\n\
        git diff-unselect | xargs git reset HEAD"

Stash 관리

[alias]
    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"

사용 팁

  • fzf 설치: brew install fzf (macOS)
  • Pygments 설치: pip3 install Pygments
  • git alias-doctor로 의존성 확인 가능

이 alias들을 .gitconfig에 추가하면 일상적인 깃 작업이 훨씬 빨라진다.

댓글