Git 초보자를 위한 기본 및 핵심 명령
Source: Dev.to
위에 제공된 소스 링크 외에 번역할 텍스트를 알려주시면 한국어로 번역해 드리겠습니다.
Source: …
Git이란?
Git은 오픈 소스 분산 버전 관리 시스템(DVCS)입니다. 2005년 리눅스 창시자 린우스 토발즈가 만든 이 시스템은 대규모 프로젝트를 고속·고효율로 처리하도록 설계되었습니다.
간단히 말해, Git은 프로젝트의 “시간 기계”와 같습니다. 개발자들은 Git을 사용하여:
- 변경 사항 추적 – 언제, 누가, 무엇을 변경했는지 정확히 확인합니다.
- 협업 – 여러 사람이 동시에 같은 코드베이스에서 작업할 수 있습니다.
- 히스토리 관리 – 문제가 발생했을 때 이전 버전으로 되돌릴 수 있습니다.
혼자 작업하든 큰 팀과 함께 작업하든, Git은 프로젝트 히스토리를 안전하고 체계적으로 관리해 줍니다.
핵심 개념
- 작업 디렉터리 – 파일을 실제로 편집하는 로컬 폴더입니다.
- 스테이징 영역(Index) – “대기실”. 파일이 저장될 준비가 되면 여기로 이동합니다.
- 로컬 저장소 – 프로젝트 히스토리를 저장하는 머신 내 숨김
.git폴더입니다. - 원격 저장소 – 온라인에 호스팅된 프로젝트 사본(예: GitHub)으로, 공유와 백업을 담당합니다.
- 커밋 – 프로젝트의 “스냅샷”. 각 커밋은 고유 ID와 변경 내용을 설명하는 메시지를 가집니다.
- 브랜치 – 병렬 작업 공간. 메인 코드를 깨뜨리지 않고 기능을 개발할 수 있게 해줍니다.
- 머지 – 한 브랜치의 변경 사항을 다른 브랜치에 합치는 작업(예: “feature” 브랜치를 “main”에 병합).
- HEAD – 현재 프로젝트 히스토리에서 위치를 가리키는 포인터. “현재 위치” 표시판과 같습니다.
Source:
필수 Git 명령
설정 및 저장소 설정
# Set your name for all your commits
git config --global user.name "Your Name"
# Set your email address
git config --global user.email "yourname@example.com"
# Turn a local folder into a Git repository
git init
# Download an existing project from a URL (like GitHub)
git clone [url]
변경 작업
# Show modified files in your working directory and staged files for your next commit
git status
# Add a file to the staging area for the next commit
git add [file]
# Add all untracked files to the staging area
git add .
# Remove a file from the staging area while retaining changes in the working area
git reset [file]
# Show the difference of what is changed but not staged
git diff
# Show the difference of what is staged but not yet committed
git diff --staged
# Commit your staged content as a new snapshot
git commit -m "[descripted message]"
# Shortcut: stage and commit tracked files
git commit -am "[descripted message]"
브랜치 관리
# List all local branches
git branch
# Create a new branch
git branch [branch-name]
# Switch to a specific branch
git checkout [branch-name]
# Combine another branch's history into your current one
git merge [branch-name]
히스토리 보기
# Show the commit history of the current branch
git log
# Show the commits on branchA that are not on branchB
git log branchB..branchA
# Show the commits that change a file, even across renaming
git log --follow [file]
# Show the difference of what is in branchA that is not in branchB
git diff branchB...branchA
# Show any object in Git in human‑readable format
git show [SHA]
원격 저장소 작업
# Add a remote repository (alias)
git remote add [alias] [url]
# Fetch all branches from the remote
git fetch [alias]
# Merge the remote branch into your current branch
git merge [alias]/[branch]
# Push local commits to the remote repository
git push [alias] [branch]
# Fetch and merge any commit from the tracking remote branch
git pull
Source: …
실용 예제
-
새 프로젝트 폴더 만들기 (작업 디렉터리)
mkdir my-first-git-project cd my-first-git-project git init # Initialize Git (run once per project) -
파일 만들기
echo "Hello Git!" > hello.txt -
상태 확인
git statushello.txt가 “추적되지 않음”으로 표시됩니다. -
스테이징 및 커밋
git add hello.txt git commit -m "Initial commit: Create hello.txt" -
추가 변경 사항 만들기
echo "Learning Git is fun!" >> hello.txt git diff # See exactly what changed before saving -
업데이트를 스테이징하고 커밋
git add . git commit -m "Update hello.txt with a new sentence" -
프로젝트 히스토리 보기
git log --oneline
로그의 각 라인은 무언가 잘못될 경우 되돌아갈 수 있는 시점을 나타냅니다. 이제 첫 번째 프로젝트를 성공적으로 추적했습니다!
버전 관리란 단순히 코드를 저장하는 것이 아니라, 프로젝트를 망칠 걱정 없이 자유롭게 실험할 수 있는 자유를 의미합니다.