Git 명령어 치트시트
Git은 파일 변경을 저장소 안 일련의 커밋으로 기록합니다. 이 명령어들은 저장소 만들기, 작업 저장, 브랜치, 머지, 원격 공유 같은 매일의 흐름을 다룹니다. 배우는 동안 곁에 두세요.
| 명령어 | 하는 일 |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
참고
- 대부분의 명령어는 플래그를 받습니다(-m은 메시지, -a는 전체). 전체 목록은 'git help <명령어>'를 실행하세요.
- 브랜치 이름은 가볍고 버리기 쉽습니다. 기능 브랜치에서 수시로 커밋한 뒤 main에 머지하세요.
- rebase나 reset 같은 파괴적 명령어 전에는 작업이 커밋되거나 stash되었는지 확인하세요.
자주 묻는 질문
- merge와 rebase의 차이는?
- merge는 두 부모를 가진 새 커밋으로 히스토리를 합칩니다. rebase는 커밋을 다른 브랜치 위로 다시 올려 선형 히스토리를 만들지만 커밋 ID를 다시 씁니다.
- 커밋을 되돌리려면?
- 'git revert <커밋>'은 변경을 취소하는 새 커밋을 추가합니다(안전, 공유 히스토리 유지). 'git reset'은 히스토리를 다시 쓰므로 공유 브랜치에선 쓰지 마세요.
- git pull과 git fetch의 차이는?
- fetch는 원격에서 커밋과 참조를 내려받되 작업 트리는 건드리지 않습니다. pull은 fetch한 뒤 그 변경을 현재 브랜치에 merge하거나 rebase합니다.
- git stash는 언제 쓰나요?
- stash는 커밋하지 않은 변경을 임시 보관해 브랜치 전환이나 깔끔한 pull을 가능하게 하고, 나중에 'git stash pop'으로 복원합니다.