๐Ÿ”ง DevOps

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.

Detailed Explanation

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.

Key concepts

  • Repository: A space that holds a project's files and change history
  • Commit: A snapshot of file changes. Identified by a unique SHA-1 hash
  • Branch: An independent line of development. Isolates feature development or bug fixes
  • Merge: The operation of combining diverged branches back together
  • Remote: A remote repository (GitHub, GitLab, etc.)
  • Clone: Creates a full copy of a remote repository

The Three Areas of Git

  • Working Directory: The workspace where the actual files are located
  • Staging Area (Index): The area where changes wait to be committed
  • Repository (.git): The repository of committed snapshots

Code Example

Basic Git Workflow

# ์ €์žฅ์†Œ ์ดˆ๊ธฐํ™” ๋˜๋Š” ๋ณต์ œ
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       # ๋ธŒ๋žœ์น˜ ๊ฐ„ ๋น„๊ต

Working with Branches

# ๋ธŒ๋žœ์น˜ ์ƒ์„ฑ ๋ฐ ์ „ํ™˜
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  # ์›๊ฒฉ ๋ธŒ๋žœ์น˜ ์‚ญ์ œ

Undoing and Amending Changes

# ์Šคํ…Œ์ด์ง• ์ทจ์†Œ
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 and Advanced Features

# 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 Example

# .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

Practical Conversation Example

Junior Developer
"I pushed directly to the main branch and missed the code review. What do I do about a commit I've already made?"
Senior developer
"If someone else has already pulled, a force push is risky. Create a new commit with `git revert HEAD` to roll back, then redo the work on a feature branch later and open a PR."
Junior Developer
"What's the difference between revert and reset?"
Senior developer
"revert adds a new commit that applies the opposite changes of a previous commit. The history is preserved, so it's safe for collaboration. reset modifies the history directly, so if you use it on an already shared branch, it can get out of sync with other people's repositories."

Cautions

No Force Push

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.

Do Not Commit Sensitive Information

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.

Beware of Large Files

Git is not well suited to large binary files. Use Git LFS (Large File Storage) for videos and build artifacts.

Commit Message Conventions

Using a consistent commit message format (Conventional Commits) makes automated changelog generation and version management easier.

Learn More