Git 치트시트
일상 개발에서 자주 사용하는 Git 명령어 모음. 검색, 필터, 원클릭 복사를 지원합니다.
설정 & 초기화
7 itemsgit config --global user.name
커밋에 표시될 사용자 이름을 설정합니다.
git config --global user.name "John Doe"
git config --global user.email
커밋에 표시될 이메일 주소를 설정합니다.
git config --global user.email "john@example.com"
git init
현재 디렉토리에 새 Git 저장소를 생성합니다.
git init # or initialize with a specific branch name git init -b main
git clone
원격 저장소를 로컬에 복제합니다.
# Clone via HTTPS git clone https://github.com/user/repo.git # Clone via SSH git clone git@github.com:user/repo.git # Clone into a specific folder git clone https://github.com/user/repo.git my-folder
git remote add
원격 저장소를 추가합니다.
git remote add origin https://github.com/user/repo.git git remote add upstream https://github.com/original/repo.git
git remote -v
등록된 원격 저장소 목록과 URL을 확인합니다.
git remote -v # origin https://github.com/user/repo.git (fetch) # origin https://github.com/user/repo.git (push)
git config --list
현재 Git 설정을 모두 출력합니다.
# Show all config git config --list # Show specific value git config user.name
브랜치 관리
7 itemsgit branch
로컬 브랜치 목록을 표시합니다.
# List local branches git branch # List with last commit info git branch -v
git branch -a
로컬 및 원격 브랜치를 모두 표시합니다.
git branch -a # * main # feature/auth # remotes/origin/main # remotes/origin/develop
git checkout -b
새 브랜치를 생성하고 전환합니다.
# Create and switch to a new branch git checkout -b feature/login # Create branch from a specific commit git checkout -b hotfix/bug-123 abc1234
git switch
브랜치를 전환합니다 (Git 2.23+).
# Switch to existing branch git switch main # Create and switch git switch -c feature/new-feature
git merge
다른 브랜치를 현재 브랜치에 병합합니다.
# Merge feature branch into main git switch main git merge feature/login # Merge with no fast-forward (always create merge commit) git merge --no-ff feature/login
git branch -d / -D
브랜치를 삭제합니다.
# Safe delete (only if merged) git branch -d feature/login # Force delete (even if not merged) git branch -D experimental # Delete remote branch git push origin --delete feature/login
git branch -m
브랜치 이름을 변경합니다.
# Rename current branch git branch -m new-name # Rename a specific branch git branch -m old-name new-name
스테이징 & 커밋
7 itemsgit add
파일을 스테이징 영역에 추가합니다.
# Stage a specific file git add src/app.ts # Stage all changes git add . # Stage all .tsx files git add "*.tsx"
git add -p
변경 사항을 하나씩 확인하며 선택적으로 스테이징합니다.
git add -p # y = stage this hunk # n = skip this hunk # s = split into smaller hunks # q = quit
git commit -m
스테이징된 변경 사항을 커밋합니다.
# Simple commit git commit -m "feat: add user authentication" # Multi-line commit message git commit -m "fix: resolve login timeout" -m "Increased session TTL from 15m to 1h"
git commit --amend
마지막 커밋을 수정합니다 (메시지 또는 파일 추가).
# Change last commit message git commit --amend -m "fix: correct typo in auth module" # Add forgotten file to last commit git add forgotten-file.ts git commit --amend --no-edit
git status
워킹 디렉토리의 상태를 확인합니다.
# Full status git status # Short format git status -s # M src/app.ts (modified, staged) # ? new-file.ts (untracked)
git diff
아직 스테이징되지 않은 변경 사항을 확인합니다.
# Unstaged changes git diff # Changes between two branches git diff main...feature/auth # Specific file only git diff src/app.ts
git diff --staged
스테이징된 변경 사항을 확인합니다 (커밋될 내용).
git diff --staged # or equivalent git diff --cached
히스토리 & 로그
6 itemsgit log --oneline
각 커밋을 한 줄로 간략하게 표시합니다.
git log --oneline # abc1234 feat: add login page # def5678 fix: resolve CORS issue # ghi9012 chore: update dependencies
git log --graph
브랜치 히스토리를 그래프로 시각화합니다.
git log --oneline --graph --all --decorate # * abc1234 (HEAD -> main) Merge feature/auth # |\ # | * def5678 feat: add OAuth # |/ # * ghi9012 initial commit
git log -p
각 커밋의 diff(변경 내용)를 함께 표시합니다.
# Show last 3 commits with diffs git log -p -3 # Show changes for a specific file git log -p -- src/auth.ts
git blame
파일의 각 줄을 누가 마지막으로 수정했는지 표시합니다.
git blame src/app.ts # abc1234 (John 2024-01-15) import express # def5678 (Jane 2024-02-20) const app = express()
git show
특정 커밋의 상세 정보와 변경 내용을 표시합니다.
# Show latest commit git show # Show specific commit git show abc1234 # Show a file at a specific commit git show abc1234:src/app.ts
git reflog
HEAD의 이동 기록을 표시합니다 (실수 복구에 필수).
git reflog
# abc1234 HEAD@{0}: commit: feat: add login
# def5678 HEAD@{1}: checkout: moving from feature to main
# ghi9012 HEAD@{2}: commit: fix: resolve bug되돌리기 & 리셋
6 itemsgit reset HEAD~1
마지막 커밋을 취소하고 변경 사항은 유지합니다 (mixed).
# Undo last commit, keep changes (default: --mixed) git reset HEAD~1 # Undo last commit, keep changes staged git reset --soft HEAD~1
git reset --hard
커밋, 스테이징, 워킹 디렉토리를 모두 되돌립니다.
# Discard everything back to last commit git reset --hard HEAD # Reset to a specific commit (DANGEROUS) git reset --hard abc1234
git revert
특정 커밋을 취소하는 새로운 커밋을 생성합니다.
# Revert a specific commit git revert abc1234 # Revert without auto-commit git revert --no-commit abc1234
git checkout -- file
특정 파일의 변경을 마지막 커밋 상태로 되돌립니다.
# Discard changes in a single file git checkout -- src/app.ts # Restore from a specific commit git checkout abc1234 -- src/app.ts
git restore
파일을 복원합니다 (Git 2.23+에서 checkout 대체).
# Discard unstaged changes git restore src/app.ts # Unstage a staged file git restore --staged src/app.ts # Restore from specific commit git restore --source=abc1234 src/app.ts
git clean -fd
추적되지 않는 파일과 디렉토리를 삭제합니다.
# Preview what will be deleted git clean -nd # Delete untracked files and directories git clean -fd # Also remove ignored files git clean -fdx
스태시 (임시 저장)
6 itemsgit stash
현재 변경 사항을 임시 저장하고 워킹 디렉토리를 깨끗하게 만듭니다.
# Stash with default message git stash # Stash with a descriptive name git stash push -m "WIP: login form validation"
git stash pop
가장 최근 스태시를 복원하고 스태시 목록에서 제거합니다.
git stash pop
# Pop a specific stash
git stash pop stash@{2}git stash list
저장된 스태시 목록을 표시합니다.
git stash list
# stash@{0}: WIP: login form validation
# stash@{1}: On main: quick fix attemptgit stash drop
특정 스태시를 삭제합니다.
# Drop the latest stash
git stash drop
# Drop a specific stash
git stash drop stash@{1}
# Clear all stashes
git stash cleargit stash apply
스태시를 복원하되 목록에서 제거하지 않습니다.
git stash apply
# Apply a specific stash
git stash apply stash@{1}git stash branch
스태시를 기반으로 새 브랜치를 생성합니다.
git stash branch feature/from-stash
# From a specific stash
git stash branch feature/recovery stash@{2}원격 & 동기화
6 itemsgit fetch
원격 변경 사항을 다운로드하되 병합하지 않습니다.
# Fetch from default remote git fetch # Fetch from specific remote git fetch upstream # Fetch and prune deleted branches git fetch --prune
git pull
원격 변경 사항을 다운로드하고 현재 브랜치에 병합합니다.
# Pull from tracking branch git pull # Pull from specific remote/branch git pull origin main
git push
로컬 커밋을 원격 저장소에 업로드합니다.
# Push to tracking branch git push # Push specific branch git push origin feature/login
git push -u
업스트림 추적을 설정하며 push합니다.
# First push of a new branch git push -u origin feature/login # After this, just "git push" works
git pull --rebase
원격 변경 사항 위에 로컬 커밋을 재배치합니다.
git pull --rebase origin main # Set as default behavior git config --global pull.rebase true
git remote prune
원격에서 삭제된 브랜치의 로컬 추적 참조를 정리합니다.
# Prune stale remote references git remote prune origin # Or during fetch git fetch --prune
고급 명령
6 itemsgit rebase
커밋을 다른 베이스 위로 재배치합니다.
# Rebase current branch onto main git rebase main # Continue after resolving conflicts git rebase --continue # Abort rebase in progress git rebase --abort
git cherry-pick
특정 커밋을 현재 브랜치에 복사합니다.
# Pick a single commit git cherry-pick abc1234 # Pick multiple commits git cherry-pick abc1234 def5678 # Pick without committing git cherry-pick --no-commit abc1234
git bisect
이진 검색으로 버그를 도입한 커밋을 찾습니다.
# Start bisecting git bisect start git bisect bad # current version is broken git bisect good v1.0.0 # this version was working # Git checks out a middle commit. Test and mark: git bisect good # or git bisect bad # When done git bisect reset
git tag
특정 커밋에 태그를 생성합니다 (릴리스 버전 등).
# Lightweight tag git tag v1.0.0 # Annotated tag with message git tag -a v1.0.0 -m "Release version 1.0.0" # Push tags to remote git push origin --tags # Delete a tag git tag -d v1.0.0
git archive
저장소의 특정 시점을 아카이브 파일로 내보냅니다.
# Create a zip archive of HEAD git archive --format=zip HEAD > project.zip # Archive a specific branch git archive --format=tar.gz v1.0.0 > release-1.0.0.tar.gz
git worktree
하나의 저장소에서 여러 작업 트리를 동시에 사용합니다.
# Add a new worktree for a branch git worktree add ../hotfix hotfix/urgent-bug # List all worktrees git worktree list # Remove a worktree git worktree remove ../hotfix
Git 치트시트 사용 가이드
Git은 전 세계에서 가장 널리 사용되는 분산 버전 관리 시스템입니다. 이 치트시트는 일상 개발에서 자주 사용하는 핵심 Git 명령어를 카테고리별로 정리했습니다. 초보자부터 숙련된 개발자까지 빠르게 참조할 수 있습니다.
Git 워크플로 기초
Git의 기본 워크플로는 변경 → 스테이징(add) → 커밋(commit) → 푸시(push)입니다. 브랜치를 사용하면 기능 개발, 버그 수정, 실험을 격리된 환경에서 진행할 수 있고, 완료 후 메인 브랜치에 병합합니다.
좋은 커밋 메시지 작성법
커밋 메시지는 "무엇을"보다 "왜"를 설명하세요. Conventional Commits 형식(feat:, fix:, chore: 등)을 사용하면 변경 로그를 자동으로 생성할 수 있습니다. 첫 줄은 50자 이내로 요약하고, 필요하면 빈 줄 뒤에 상세 설명을 추가합니다.
실수 복구 전략
git reflog는 HEAD의 모든 이동 기록을 보관합니다. reset --hard로 잃어버린 커밋도 reflog에서 찾아 복구할 수 있습니다. 위험한 작업 전에는 항상 git stash 또는 임시 브랜치를 만들어두면 안전합니다.