Git
Git Version Control
A distributed version control system. Tracks source code change history with branches, merges, and commits. Used together with GitHub and GitLab.
Git Version Control
A distributed version control system. Tracks source code change history with branches, merges, and commits. Used together with GitHub and GitLab.
Git is a distributed version control system (DVCS) created by Linus Torvalds in 2005 for developing the Linux kernel. It tracks the change history of files, allows multiple developers to work simultaneously, and is currently the most widely used source code management tool in the world.
# ์ ์ฅ์ ์ด๊ธฐํ ๋๋ ๋ณต์
git init my-project
git clone https://github.com/user/repo.git
# ์ํ ํ์ธ
git status
git log --oneline --graph
# ํ์ผ ์คํ
์ด์ง ๋ฐ ์ปค๋ฐ
git add . # ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ ์คํ
์ด์ง
git add src/app.js # ํน์ ํ์ผ๋ง ์คํ
์ด์ง
git commit -m "feat: ๋ก๊ทธ์ธ ๊ธฐ๋ฅ ์ถ๊ฐ"
# ์๊ฒฉ ์ ์ฅ์ ์ฐ๋
git remote add origin https://github.com/user/repo.git
git push -u origin main
git pull origin main
# ๋ณ๊ฒฝ์ฌํญ ํ์ธ
git diff # Working vs Staging
git diff --staged # Staging vs Last Commit
git diff main..feature-branch # ๋ธ๋์น ๊ฐ ๋น๊ต
# ๋ธ๋์น ์์ฑ ๋ฐ ์ ํ
git branch feature/login # ๋ธ๋์น ์์ฑ
git checkout feature/login # ๋ธ๋์น ์ ํ
git checkout -b feature/login # ์์ฑ๊ณผ ์ ํ์ ๋์์
git switch -c feature/login # Git 2.23+ ์๋ก์ด ๋ช
๋ น์ด
# ๋ธ๋์น ๋ชฉ๋ก
git branch # ๋ก์ปฌ ๋ธ๋์น
git branch -r # ์๊ฒฉ ๋ธ๋์น
git branch -a # ๋ชจ๋ ๋ธ๋์น
# ๋ธ๋์น ๋ณํฉ
git checkout main
git merge feature/login # feature/login์ main์ ๋ณํฉ
git merge --no-ff feature/login # fast-forward ๋ฐฉ์ง
# ๋ธ๋์น ์ญ์
git branch -d feature/login # ๋ณํฉ๋ ๋ธ๋์น ์ญ์
git branch -D feature/login # ๊ฐ์ ์ญ์
git push origin --delete feature/login # ์๊ฒฉ ๋ธ๋์น ์ญ์
# ์คํ
์ด์ง ์ทจ์
git restore --staged file.js # Git 2.23+
git reset HEAD file.js # ์ด์ ๋ฐฉ์
# ์์
๋ณ๊ฒฝ์ฌํญ ๋ฒ๋ฆฌ๊ธฐ
git restore file.js # Git 2.23+
git checkout -- file.js # ์ด์ ๋ฐฉ์
# ๋ง์ง๋ง ์ปค๋ฐ ์์
git commit --amend -m "์ ๋ฉ์์ง" # ๋ฉ์์ง ์์
git commit --amend --no-edit # ํ์ผ๋ง ์ถ๊ฐ
# ์ปค๋ฐ ๋๋๋ฆฌ๊ธฐ
git revert HEAD # ์ ์ปค๋ฐ์ผ๋ก ๋๋๋ฆผ (์์ )
git reset --soft HEAD~1 # ์ปค๋ฐ ์ทจ์, ์คํ
์ด์ง ์ ์ง
git reset --mixed HEAD~1 # ์ปค๋ฐ ์ทจ์, ์คํ
์ด์ง๋ ์ทจ์
git reset --hard HEAD~1 # ๋ชจ๋ ๋ณ๊ฒฝ์ฌํญ ์ญ์ (์ฃผ์!)
# Rebase - ์ปค๋ฐ ํ์คํ ๋ฆฌ ์ ๋ฆฌ
git checkout feature
git rebase main # feature๋ฅผ main ์๋ก ์ด๋
# Interactive Rebase - ์ปค๋ฐ ํธ์ง
git rebase -i HEAD~3 # ์ต๊ทผ 3๊ฐ ์ปค๋ฐ ํธ์ง
# pick, squash, reword, edit, drop ์ต์
์ฌ์ฉ
# Cherry-pick - ํน์ ์ปค๋ฐ๋ง ๊ฐ์ ธ์ค๊ธฐ
git cherry-pick abc1234
# Stash - ์์ ์ ์ฅ
git stash # ๋ณ๊ฒฝ์ฌํญ ์์ ์ ์ฅ
git stash pop # ๋ณต์
git stash list # ๋ชฉ๋ก
git stash drop # ์ญ์
# ํน์ ํ์ผ ํ์คํ ๋ฆฌ
git log --follow -p -- src/app.js
# ๋๊ฐ ์์ ํ๋์ง ํ์ธ
git blame src/app.js
# .gitignore
# Node.js
node_modules/
npm-debug.log
.env
.env.local
# Build
dist/
build/
*.min.js
# IDE
.idea/
.vscode/
*.swp
# OS
.DS_Store
Thumbs.db
# ์์ธ
!.gitkeep
!important-file.env.example
Using `git push --force` on shared branches (main, develop) can cause other team members' work to be lost. The `--force-with-lease` option is safer.
If you commit passwords, API keys, and the like, they are permanently recorded in the history. Even if you delete them, they can be recovered, so replace the keys immediately if they are exposed.
Git is not well suited to large binary files. Use Git LFS (Large File Storage) for videos and build artifacts.
Using a consistent commit message format (Conventional Commits) makes automated changelog generation and version management easier.