포스트

[GitHub 100일 챌린지] Day 40 - Git Alias 단축키 설정

[GitHub 100일 챌린지] Day 40 - Git Alias 단축키 설정

100일 챌린지 Day 40 - 자주 쓰는 명령어를 단축키로 만듭니다

배울 내용

  1. Alias 기본 설정
  2. 유용한 Alias 모음
  3. 고급 Alias 활용

Topic1. Alias 기본 설정

Git Alias = 명령어 단축키

자주 사용하는 긴 명령어를 짧은 별칭으로 만들어 생산성을 높입니다.

기본 문법

1
git config --global alias.<별칭> '<명령어>'

예시:

1
2
3
4
5
6
# git status를 git st로
git config --global alias.st 'status'

# 사용
git st
# = git status

Alias 설정 방법

방법 1: 명령어로 설정:

1
2
3
git config --global alias.co 'checkout'
git config --global alias.br 'branch'
git config --global alias.ci 'commit'

방법 2: 설정 파일 직접 편집:

1
2
3
4
5
# ~/.gitconfig 열기
vim ~/.gitconfig

# 또는
git config --global -e

~/.gitconfig 파일:

1
2
3
4
5
[alias]
  co = checkout
  br = branch
  ci = commit
  st = status

Alias 조회

1
2
3
4
5
6
7
# 모든 alias 보기
git config --get-regexp alias

# 출력:
# alias.co checkout
# alias.br branch
# alias.ci commit

Alias 삭제

1
2
3
4
# 특정 alias 삭제
git config --global --unset alias.co

# 설정 파일에서도 제거됨

Global vs Local Alias

Global Alias (모든 프로젝트):

1
2
git config --global alias.st 'status'
# ~/.gitconfig에 저장

Local Alias (현재 프로젝트만):

1
2
git config --local alias.st 'status -s'
# .git/config에 저장

우선순위:

1
Local (프로젝트) > Global (사용자) > System (시스템)

옵션 포함 Alias

1
2
3
4
5
6
# 옵션 포함
git config --global alias.s 'status -s'
git config --global alias.lg 'log --oneline --graph'

# 여러 옵션
git config --global alias.ll 'log --oneline --graph --all --decorate'

해보기: 기본 Alias 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. 자주 쓰는 명령어 단축
git config --global alias.co 'checkout'
git config --global alias.br 'branch'
git config --global alias.ci 'commit'
git config --global alias.st 'status'

# 2. 옵션 포함 단축
git config --global alias.s 'status -s'
git config --global alias.sb 'status -sb'
git config --global alias.cm 'commit -m'

# 3. 사용해보기
git s           # git status -s
git sb          # git status -sb
git cm "test"   # git commit -m "test"

# 4. 설정 확인
git config --get-regexp alias

# 5. 하나 삭제
git config --global --unset alias.ci

결과: 자주 쓰는 명령어를 빠르게 실행할 수 있습니다


Topic2. 유용한 Alias 모음

실전 Alias = 개발 효율 2배

현업에서 많이 사용하는 유용한 Alias들을 소개합니다.

카테고리 1: 기본 명령어 단축

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 기본 동작
git config --global alias.co 'checkout'
git config --global alias.br 'branch'
git config --global alias.ci 'commit'
git config --global alias.st 'status'

# 자주 쓰는 조합
git config --global alias.s 'status -s'
git config --global alias.sb 'status -sb'
git config --global alias.aa 'add --all'
git config --global alias.cm 'commit -m'
git config --global alias.ca 'commit --amend'
git config --global alias.ps 'push'
git config --global alias.pl 'pull'
git config --global alias.f 'fetch'

카테고리 2: Log 관련

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 간결한 log
git config --global alias.l 'log --oneline'
git config --global alias.ll 'log --oneline -10'

# 그래프 log
git config --global alias.lg 'log --oneline --graph --all --decorate'

# 상세 log
git config --global alias.lga 'log --graph --all --format="%C(yellow)%h%C(reset) - %s %C(green)(%cr)%C(reset) %C(blue)<%an>%C(reset)%C(auto)%d%C(reset)"'

# 통계 log
git config --global alias.ls 'log --stat'

# 파일별 log
git config --global alias.lf 'log --follow -p --'

카테고리 3: Diff 관련

1
2
3
4
5
6
7
8
9
10
11
# 기본 diff 단축
git config --global alias.d 'diff'
git config --global alias.ds 'diff --staged'
git config --global alias.dc 'diff --cached'

# 단어 단위 diff
git config --global alias.dw 'diff --word-diff'
git config --global alias.dt 'diff --stat'

# HEAD와 비교
git config --global alias.dh 'diff HEAD'

카테고리 4: Show 관련

1
2
3
4
5
6
# 최근 커밋
git config --global alias.last 'show -s'
git config --global alias.lastf 'show --stat'

# 파일 목록
git config --global alias.showf 'show --name-status'

카테고리 5: Branch 관련

1
2
3
4
5
6
7
8
9
# 브랜치 목록
git config --global alias.b 'branch'
git config --global alias.ba 'branch -a'
git config --global alias.bd 'branch -d'
git config --global alias.bD 'branch -D'

# 브랜치 정보
git config --global alias.bv 'branch -v'
git config --global alias.bvv 'branch -vv'

카테고리 6: Remote 관련

1
2
3
4
5
6
7
8
9
10
# Remote 조회
git config --global alias.r 'remote'
git config --global alias.rv 'remote -v'

# Remote 브랜치
git config --global alias.rb 'branch -r'

# Fetch와 Pull
git config --global alias.fp 'fetch -p'
git config --global alias.pom 'pull origin main'

카테고리 7: 작업 취소

1
2
3
4
5
6
7
8
# 변경사항 되돌리기
git config --global alias.unstage 'reset HEAD --'
git config --global alias.uncommit 'reset --soft HEAD^'
git config --global alias.discard 'checkout --'

# 마지막 커밋 수정
git config --global alias.amend 'commit --amend --no-edit'
git config --global alias.amendc 'commit --amend'

카테고리 8: Stash 관련

1
2
3
4
5
6
# Stash 단축
git config --global alias.ss 'stash save'
git config --global alias.sl 'stash list'
git config --global alias.sp 'stash pop'
git config --global alias.sd 'stash drop'
git config --global alias.sa 'stash apply'

카테고리 9: 정리 및 유지보수

1
2
3
4
5
6
7
8
# 로컬 브랜치 정리
git config --global alias.cleanup 'branch --merged | grep -v "\*" | xargs -n 1 git branch -d'

# 원격 추적 브랜치 정리
git config --global alias.prune 'fetch --prune'

# 가비지 컬렉션
git config --global alias.gc-now 'gc --aggressive --prune=now'

카테고리 10: 검색 및 찾기

1
2
3
4
5
6
7
8
# 커밋 메시지 검색
git config --global alias.find 'log --all --grep'

# 작성자 검색
git config --global alias.who 'shortlog -sn --'

# 파일 검색
git config --global alias.grep 'grep -Ii'

해보기: 유용한 Alias 일괄 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 스크립트로 한 번에 설정
cat > ~/.git-aliases.sh << 'EOF'
#!/bin/bash

# 기본
git config --global alias.co 'checkout'
git config --global alias.br 'branch'
git config --global alias.ci 'commit'
git config --global alias.st 'status'
git config --global alias.s 'status -s'
git config --global alias.sb 'status -sb'

# Log
git config --global alias.lg 'log --oneline --graph --all --decorate'
git config --global alias.ll 'log --oneline -10'

# Diff
git config --global alias.d 'diff'
git config --global alias.ds 'diff --staged'
git config --global alias.dw 'diff --word-diff'

# Show
git config --global alias.last 'show -s'
git config --global alias.lastf 'show --stat'

# 작업 취소
git config --global alias.unstage 'reset HEAD --'
git config --global alias.uncommit 'reset --soft HEAD^'
git config --global alias.amend 'commit --amend --no-edit'

echo "✅ Git aliases configured!"
EOF

# 실행
chmod +x ~/.git-aliases.sh
~/.git-aliases.sh

# 확인
git config --get-regexp alias

결과: 생산성을 높이는 Alias 세트가 준비됐습니다


Topic3. 고급 Alias 활용

고급 Alias = 쉘 스크립트 수준 자동화

복잡한 작업을 자동화하는 강력한 Alias를 만듭니다.

쉘 명령어 실행 (!로 시작)

기본 쉘 명령어:

1
2
3
4
5
6
# 현재 브랜치 이름만
git config --global alias.current '!git rev-parse --abbrev-ref HEAD'

# 사용
git current
# main

느낌표(!) 의미:

1
! = Git 명령어가 아닌 쉘 명령어 실행

함수 형태 Alias

기본 함수 구조:

1
git config --global alias.name '!f() { 명령어; }; f'

예시 1: 브랜치 생성 + 체크아웃:

1
2
3
4
5
git config --global alias.cob '!f() { git checkout -b "$1"; }; f'

# 사용
git cob feature-login
# = git checkout -b feature-login

예시 2: 커밋 + Push:

1
2
3
4
5
git config --global alias.cap '!f() { git add -A && git commit -m "$1" && git push; }; f'

# 사용
git cap "Add new feature"
# = git add -A && git commit -m "Add new feature" && git push

예시 3: 특정 파일 히스토리:

1
2
3
4
git config --global alias.fh '!f() { git log --follow -p -- "$1"; }; f'

# 사용
git fh src/auth.js

복잡한 워크플로우 자동화

예시 1: PR 준비:

1
2
3
4
5
6
7
8
9
10
11
git config --global alias.pr '!f() {
  current_branch=$(git current);
  git push -u origin "$current_branch";
  echo "Branch pushed: $current_branch";
  echo "Create PR: https://github.com/$(git remote get-url origin | sed "s/.*github.com[:/]\(.*\).git/\1/")/compare/$current_branch";
}; f'

# 사용
git pr
# Branch pushed: feature-login
# Create PR: https://github.com/user/repo/compare/feature-login

예시 2: 리베이스 워크플로우:

1
2
3
4
5
6
7
8
9
10
11
git config --global alias.sync '!f() {
  branch=$(git current);
  git checkout main;
  git pull;
  git checkout "$branch";
  git rebase main;
  echo "✅ $branch synced with main";
}; f'

# 사용
git sync

예시 3: 커밋 통계:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
git config --global alias.stats '!f() {
  echo "=== Commit Stats ===";
  echo "Total commits: $(git rev-list --count HEAD)";
  echo "Contributors: $(git shortlog -sn | wc -l)";
  echo "";
  echo "Top contributors:";
  git shortlog -sn --no-merges | head -5;
}; f'

# 사용
git stats
# === Commit Stats ===
# Total commits: 247
# Contributors: 5
#
# Top contributors:
#   120  Your Name
#    58  Team Member A
#    45  Team Member B

조건문과 반복문 사용

조건문 예시:

1
2
3
4
5
6
7
8
9
10
11
12
git config --global alias.safe-push '!f() {
  if [ "$(git current)" = "main" ]; then
    echo "❌ Cannot push to main directly!";
    return 1;
  else
    git push "$@";
  fi
}; f'

# 사용
git safe-push
# main 브랜치면 에러, 다른 브랜치면 push

반복문 예시:

1
2
3
4
5
6
7
8
9
10
git config --global alias.recent '!f() {
  count=${1:-5};
  for i in $(seq 0 $((count-1))); do
    git show -s --format="%C(yellow)%h%C(reset) - %s %C(green)(%cr)%C(reset)" HEAD~$i;
  done
}; f'

# 사용
git recent     # 최근 5개
git recent 10  # 최근 10개

외부 도구 통합

jq 활용 (JSON 파싱):

1
2
3
4
5
git config --global alias.pkg-version '!cat package.json | jq -r .version'

# 사용
git pkg-version
# 1.2.3

fzf 활용 (대화형 선택):

1
2
3
4
5
git config --global alias.coi '!git branch | fzf | xargs git checkout'

# 사용
git coi
# 브랜치 목록이 fzf로 표시되고 선택하면 checkout

디버깅 및 정보 Alias

종합 정보:

1
2
3
4
5
6
7
8
9
10
11
git config --global alias.info '!f() {
  echo "=== Git Repository Info ===";
  echo "Current Branch: $(git current)";
  echo "Remote: $(git remote get-url origin)";
  echo "Last Commit: $(git show -s --format="%h - %s (%cr)")";
  echo "Uncommitted Changes: $(git status -s | wc -l)";
  echo "Untracked Files: $(git ls-files --others --exclude-standard | wc -l)";
}; f'

# 사용
git info

변경사항 요약:

1
2
3
4
5
6
7
8
9
git config --global alias.summary '!f() {
  echo "=== Changes Summary ===";
  echo "Files changed: $(git diff --name-only | wc -l)";
  echo "Lines added: $(git diff --shortstat | grep -oP "\d+ insertion")";
  echo "Lines deleted: $(git diff --shortstat | grep -oP "\d+ deletion")";
  echo "";
  echo "Modified files:";
  git diff --name-status;
}; f'

Alias 관리 Alias

모든 Alias 보기:

1
2
3
4
5
6
7
git config --global alias.aliases '!git config --get-regexp alias | sed "s/alias\.\(\S*\) /\1 = /"'

# 사용
git aliases
# co = checkout
# br = branch
# ...

Alias 검색:

1
2
3
4
5
6
git config --global alias.alias-find '!f() { git config --get-regexp alias | grep -i "$1"; }; f'

# 사용
git alias-find log
# alias.lg log --oneline --graph
# alias.ll log --oneline -10

해보기: 고급 Alias 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 1. 현재 브랜치 이름
git config --global alias.current '!git rev-parse --abbrev-ref HEAD'
git current

# 2. 브랜치 생성 + 체크아웃
git config --global alias.cob '!f() { git checkout -b "$1"; }; f'
git cob test-branch

# 3. 안전한 Push
git config --global alias.safe-push '!f() {
  if [ "$(git current)" = "main" ]; then
    echo "❌ Cannot push to main!";
  else
    git push "$@";
  fi
}; f'

# 4. 종합 정보
git config --global alias.info '!f() {
  echo "Branch: $(git current)";
  echo "Last Commit: $(git show -s --format="%h - %s")";
  echo "Status: $(git status -s | wc -l) changes";
}; f'
git info

# 5. 모든 Alias 보기
git config --global alias.aliases '!git config --get-regexp alias | sed "s/alias\.\(\S*\) /\1 = /"'
git aliases

결과: 강력한 자동화 도구를 만들 수 있습니다


정리

완료 체크:

  • Alias 기본 설정 방법을 안다
  • 유용한 Alias들을 활용할 수 있다
  • 고급 Alias로 워크플로우를 자동화할 수 있다

핵심 명령어:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Alias 설정
git config --global alias.<name> '<command>'
git config --global -e              # 설정 파일 편집

# Alias 관리
git config --get-regexp alias       # 모든 alias 보기
git config --global --unset alias.<name>  # alias 삭제

# 쉘 명령어 실행
git config --global alias.name '!command'

# 함수 형태
git config --global alias.name '!f() { commands; }; f'

추천 기본 Alias:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 필수
git config --global alias.s 'status -s'
git config --global alias.sb 'status -sb'
git config --global alias.lg 'log --oneline --graph --all --decorate'
git config --global alias.d 'diff'
git config --global alias.ds 'diff --staged'

# 편의
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'show -s'
git config --global alias.amend 'commit --amend --no-edit'

# 고급
git config --global alias.current '!git rev-parse --abbrev-ref HEAD'
git config --global alias.aliases '!git config --get-regexp alias'

Alias 파일 백업:

1
2
3
4
5
6
7
8
9
# 백업
git config --global --list | grep alias > ~/.git-aliases-backup

# 복원
while read line; do
  key=$(echo "$line" | cut -d'=' -f1)
  value=$(echo "$line" | cut -d'=' -f2-)
  git config --global "$key" "$value"
done < ~/.git-aliases-backup

주의사항:

1
2
3
4
5
✅ 일관된 명명 규칙 사용
✅ 기존 Git 명령어와 충돌 피하기
✅ 팀과 공유할 Alias는 문서화
❌ 너무 짧은 이름 (가독성 저하)
❌ 복잡한 로직 (유지보수 어려움)

Week 8 완료! 🎉

Phase 4 (Day 31-40) 완료! Remote 작업과 Git 조회 명령어를 마스터했습니다.

다음: Phase 5 시작 - Day 41: 브랜치 개념


이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.