30일 만에 마스터하는 Git 버전 관리

Day 30: 실무에서 유용한 Git 작업 팁 공유

29일간의 Git 학습을 마치고, 이 마지막 글에서는 일상적인 Git 작업을 더 효율적이고 즐겁게 만들어 주는 실용적인 팁과 요령을 공유하겠습니다.

구성 팁

전역 구성

# Set default editor
git config --global core.editor "code --wait"

# Enable color UI
git config --global color.ui auto

# Set default branch name
git config --global init.defaultBranch main

# Auto-correct typos
git config --global help.autocorrect 1

# Cache credentials
git config --global credential.helper cache --timeout=3600

유용한 Alias

# Short status
git config --global alias.st status

# Short log
git config --global alias.ll "log --oneline --graph --all --decorate"

# Last commit
git config --global alias.last "log -1 HEAD"

# Undo last commit (keep changes)
git config --global alias.undo "reset HEAD~1 --mixed"

# Amend without editing message
git config --global alias.amend "commit --amend --no-edit"

# List branches sorted by last modified
git config --global alias.br "branch --sort=-committerdate"

# Show changed files in last commit
git config --global alias.dl "diff --name-only HEAD~1"

# Search commit messages
git config --global alias.search "log --all --grep"

# Nuke branch (force delete)
git config --global alias.nuke "branch -D"

생산성 팁

빠른 탐색

# Jump to repository root
alias gr='cd $(git rev-parse --show-toplevel)'

# Open repo in browser
alias browse='git remote get-url origin | xargs open'

검색

# Find file in history
git log --all --full-history -- "**/filename.*"

# Search in all commits
git log -S "search term" --source --all

# Find who changed a line
git blame filename

# Prettier blame
git blame -w -C -C -L 10,20 filename

정리

# Delete merged branches
git branch --merged main | grep -v "^*\|main" | xargs git branch -d

# Delete all local branches except main
git branch | grep -v "main" | xargs git branch -D

# Remove untracked files (dry run first!)
git clean -n
git clean -fd

# Prune remote branches
git fetch --prune

시간 절약 명령어

빠른 커밋

# Add and commit in one step
git commit -am "Quick fix"

# Add, commit, and push
alias gcp='git commit -am "$1" && git push'

브랜치 관리

# Create and checkout branch in one step
git checkout -b feature/new-feature

# Switch to previous branch
git checkout -

# Show branches with last commit
git branch -v

# Show remote tracking branches
git branch -vv

Stash 요령

# Stash with message
git stash push -m "Work in progress on feature X"

# List stashes with details
git stash list --pretty=format:"%C(yellow)%gd%C(reset) - %C(bold)%s%C(reset)"

# Apply and drop in one step
git stash pop

# Apply specific stash
git stash apply stash@{2}

# Stash including untracked files
git stash -u

고급 기법

여러 커밋 Cherry-Pick하기

# Cherry-pick a range of commits
git cherry-pick A^..B

# Cherry-pick from another branch
git cherry-pick feature-branch~3..feature-branch

대화형 스테이징

# Stage parts of a file
git add -p filename

# Interactive add
git add -i

Diff 팁

# Word diff instead of line diff
git diff --word-diff

# Ignore whitespace changes
git diff -w

# Show diff of staged changes
git diff --staged

# Diff between branches
git diff main..feature-branch

# Diff specific file between commits
git diff commit1 commit2 -- filename

Log 서식

# Pretty log
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)(%cr)%C(reset) %s %C(blue)<%an>%C(reset)"

# Log with graph
git log --graph --abbrev-commit --decorate --all

# Show files changed per commit
git log --stat

# Show patches
git log -p

문제 해결

실수 되돌리기

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo all uncommitted changes
git checkout .

# Unstage file
git reset HEAD filename

# Restore file from HEAD
git checkout HEAD -- filename

복구

# Find lost commits
git reflog

# Recover deleted branch
git branch branch-name <commit-hash>

# Find dangling commits
git fsck --lost-found

디버깅

# See who introduced a bug (binary search)
git bisect start
git bisect bad  # Current version is bad
git bisect good <commit>  # Known good commit
# Git checks out middle commit
# Test and mark as good or bad
git bisect good/bad
# Repeat until found
git bisect reset

# Blame with line range
git blame -L 10,20 filename

# Show commit that introduced line
git log -S "function name" --source --all

워크플로 최적화

Pre-Commit 훅

.git/hooks/pre-commit을 만듭니다.

#!/bin/bash

# Run tests before commit
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed, commit aborted"
    exit 1
fi

커밋 메시지 템플릿

~/.git-commit-template을 만듭니다.

# <type>: <subject>
#
# <body>
#
# <footer>
#
# Types: feat, fix, docs, style, refactor, test, chore

기본값으로 설정합니다.

git config --global commit.template ~/.git-commit-template

통합 팁

VS Code와 Git

# Open current repo in VS Code
code .

# Open merge tool
git mergetool --tool=vscode

Vim과 Git

# Set Vim as default editor
git config --global core.editor vim

# Vim as diff tool
git config --global diff.tool vimdiff
git config --global difftool.prompt false

성능 팁

대규모 리포지토리

# Shallow clone (faster)
git clone --depth 1 https://github.com/user/repo.git

# Partial clone (Git 2.22+)
git clone --filter=blob:none https://github.com/user/repo.git

# Sparse checkout (specific folders)
git sparse-checkout init
git sparse-checkout set folder1 folder2

더 빠른 Status

# Use untracked cache
git config core.untrackedCache true

# Use file system monitor
git config core.fsmonitor true

보안 팁

GPG 서명

# Generate GPG key
gpg --gen-key

# List keys
gpg --list-secret-keys --keyid-format LONG

# Configure Git
git config --global user.signingkey <key-id>
git config --global commit.gpgsign true

# Sign commits
git commit -S -m "Signed commit"

자격 증명

# Never store passwords in config
# Use credential helpers instead

# SSH for authentication (recommended)
ssh-keygen -t ed25519 -C "email@example.com"

# Use HTTPS with token
# Generate personal access token on GitHub
# Use it as password

마지막 지혜

모범 사례 점검표

  • 자주 커밋하고 정기적으로 push하세요
  • 의미 있는 커밋 메시지를 작성하세요
  • 커밋을 원자적이고 집중적으로 유지하세요
  • 모든 기능/수정에 브랜치를 만드세요
  • PR 전에 본인의 코드를 리뷰하세요
  • push하기 전에 pull하세요
  • 공유 브랜치에는 절대로 강제 push하지 마세요
  • .gitignore를 올바르게 사용하세요
  • 릴리스에 태그를 붙이세요
  • 워크플로를 문서화하세요

문제가 생겼을 때

  1. 당황하지 마세요: Git이 데이터를 잃는 일은 거의 없습니다
  2. reflog를 확인하세요: 여러분의 안전망입니다
  3. 백업을 만드세요: 실험하기 전에 브랜치를 만드세요
  4. 도움을 요청하세요: Git 커뮤니티는 도움을 잘 줍니다
  5. 실수에서 배우세요: 모든 오류가 무언가를 가르쳐 줍니다

축하합니다!

30일간의 Git 마스터 과정을 완료하셨습니다! 다음을 배우셨습니다.

  • Git 기초와 아키텍처
  • 브랜치와 병합 전략
  • 이력 조작(reset, rebase, amend)
  • 원격 워크플로와 협업
  • GitHub 워크플로와 모범 사례
  • 고급 팁과 문제 해결

계속 학습하세요

  • 정기적으로 연습하세요
  • 오픈 소스에 기여하세요
  • 다른 사람의 Git 이력에서 배우세요
  • 고급 기능을 탐색하세요
  • Git 릴리스 소식을 계속 확인하세요

마무리하며

Git은 사용할수록 더 가치 있어지는 강력한 도구입니다. 모든 것을 외우려고 하지 마세요. 이 가이드를 북마크해 두고 필요할 때 다시 참고하세요. 가장 중요한 것은 계속 연습하고 (안전한 환경에서) 실험하는 것을 두려워하지 않는 것입니다.

즐거운 코딩 되세요. 여러분의 병합이 항상 충돌 없이 이루어지기를 바랍니다!

참고 링크



감사합니다!

30일간의 Git 여정을 함께해 주셔서 감사합니다. 계속 연습하고, 계속 배우세요. 그리고 모든 Git 전문가도 한때는 초보자였다는 것을 기억하세요!