[깃초] 3부 - 깃 명령어 알아보기
by codeEater
깃 기본 명령어와 활용 프로세스
깃을 효과적으로 사용하기 위해서는 기본 명령어들을 이해하고 각 명령어가 어떤 과정에서 사용되는지 아는 것이 중요합니다. 실제 개발 과정에서 자주 사용되는 명령어들을 하나씩 살펴보겠습니다.
최초 설정 명령어
프로젝트를 시작하기 전, 깃의 기본 설정이 필요합니다.
# 전역 사용자 설정 git config --global user.name "your_name" git config --global user.email "your_email" # 새로운 git 저장소 초기화 git init # 원격 저장소 복제 git clone https://github.com/사용자명/저장소명.git
출력 예시:
$ git config --global user.name "CodeEater" $ git config --global user.email "codeEater@tistory.com" $ git config --list user.name=CodeEater user.email=codeEater@tistory.com core.autocrlf=input core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.ignorecase=true core.precomposeunicode=true $ git init 현재 폴더에서 빈 Git 저장소를 초기화했습니다 $ git clone https://github.com/CodeEater/my-project.git 'my-project'로 복제하는 중... remote: Enumerating objects: 147, done. remote: Counting objects: 100% (147/147), done. remote: Compressing objects: 100% (89/89), done. remote: Total 147 (delta 49), reused 147 (delta 49), pack-reused 0 오브젝트를 받는 중: 100% (147/147), 22.96 KiB | 4.59 MiB/s, 완료. 델타를 적용하는 중: 100% (49/49), 완료.
기본 작업 흐름 명령어
일상적인 개발 과정에서 가장 자주 사용되는 명령어들입니다.
1. 변경 사항 확인
# 현재 git 상태 확인 git status # 변경 사항 상세 확인 git diff git diff --staged # 스테이징된 변경 사항 확인
출력 예시:
$ git status 현재 브랜치 main 브랜치가 'origin/main'에 맞게 업데이트된 상태입니다. 커밋하도록 정하지 않은 변경 사항: (무엇을 커밋할지 바꾸려면 "git add <파일>..."을 사용하십시오) (use "git restore <file>..." to discard changes in working directory) 수정함: src/components/Login.js 추적하지 않는 파일: (커밋할 사항에 포함하려면 "git add <파일>..."을 사용하십시오) src/styles/login.css $ git diff src/components/Login.js diff --git a/src/components/Login.js b/src/components/Login.js index 8a1b678..9cd5f23 100644 --- a/src/components/Login.js +++ b/src/components/Login.js @@ -1,5 +1,6 @@ import React from 'react'; +import { useState } from 'react'; -function Login() { +function Login({ onSubmit }) { // 변경된 내용
2. 파일 스테이징과 커밋
# 특정 파일 스테이징 git add 파일명 # 모든 변경 사항 스테이징 git add . # 변경 사항 커밋 git commit -m "커밋 메시지" # add와 commit 동시 진행 (tracked 파일만) git commit -am "커밋 메시지"
출력 예시:
$ git add src/components/Login.js $ git status 현재 브랜치 main 커밋할 변경 사항: (use "git restore --staged <file>..." to unstage) 수정함: src/components/Login.js 추적하지 않는 파일: (커밋할 사항에 포함하려면 "git add <파일>..."을 사용하십시오) src/styles/login.css $ git commit -m "로그인 컴포넌트 기능 추가" [main 3e4f5c6] 로그인 컴포넌트 기능 추가 1 file changed, 25 insertions(+), 3 deletions(-)
3. 원격 저장소 관리
# 원격 저장소 추가 git remote add origin 저장소URL # 원격 저장소 목록 확인 git remote -v # 변경 사항 원격 저장소에 올리기 git push origin 브랜치명 # 원격 저장소의 변경 사항 가져오기 git pull origin 브랜치명
출력 예시:
$ git remote add origin https://github.com/CodeEater/my-project.git $ git remote -v origin https://github.com/CodeEater/my-project.git (fetch) origin https://github.com/CodeEater/my-project.git (push) $ git push origin main 오브젝트 나열하는 중: 5, 완료. 오브젝트 개수 세는 중: 100% (5/5), 완료. 델타 압축하는 중: 100% (3/3), 완료. 오브젝트 쓰는 중: 100% (3/3), 356 bytes | 356.00 KiB/s, 완료. Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (2/2), completed with 2 local objects. To https://github.com/CodeEater/my-project.git a1b2c3d..3e4f5c6 main -> main $ git pull origin main remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 오브젝트를 받는 중: 100% (3/3), 285 bytes | 285.00 KiB/s, 완료. 델타를 적용하는 중: 100% (1/1), 완료. 현재 브랜치 main입니다 브랜치가 'origin/main'에 맞게 업데이트된 상태입니다.
브랜치 관련 명령어
브랜치를 활용한 개발은 깃의 핵심 기능입니다.
# 브랜치 목록 확인 git branch # 새 브랜치 생성 git branch 브랜치명 # 브랜치 전환 git checkout 브랜치명 # 브랜치 생성과 전환을 동시에 git checkout -b 브랜치명 # 브랜치 병합 git merge 브랜치명 # 브랜치 삭제 git branch -d 브랜치명
출력 예시:
$ git branch * main feature/login feature/signup $ git checkout -b feature/user-profile 새로운 브랜치 'feature/user-profile'로 전환됨 $ git merge feature/login 업데이트 중 1a2b3c4..4d5e6f7 Fast-forward src/components/Login.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) $ git branch -d feature/signup feature/signup 브랜치 삭제 (과거 757e92c).
변경 사항 관리 명령어
실수를 했거나 이전 상태로 돌아가야 할 때 사용하는 명령어들입니다.
# 마지막 커밋 수정 git commit --amend # 스테이징 취소 git reset HEAD 파일명 # 워킹 디렉토리 변경 사항 취소 git checkout -- 파일명 # 특정 커밋으로 돌아가기 git reset --hard 커밋해시
출력 예시:
$ git commit --amend [main f345c67] 로그인 폼 유효성 검사 추가 Date: Thu Feb 18 15:45:32 2024 +0900 1 file changed, 25 insertions(+), 3 deletions(-) $ git reset HEAD src/components/Login.js 스테이지 취소됨 src/components/Login.js $ git reset --hard a1b2c3d HEAD의 현재 위치는 a1b2c3d입니다
이력 확인 명령어
프로젝트의 히스토리를 확인할 때 사용합니다.
# 커밋 이력 확인 git log # 간단한 로그 확인 git log --oneline # 변경 사항과 함께 로그 확인 git log -p # 특정 파일의 변경 이력 확인 git blame 파일명
출력 예시:
$ git log commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 Author: CodeEater <codeEater@tistory.com> Date: Thu Feb 18 14:35:12 2024 +0900 로그인 컴포넌트 초기 구현 commit b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t Author: CodeEater <codeEater@tistory.com> Date: Thu Feb 18 11:22:33 2024 +0900 프로젝트 초기 설정 $ git log --oneline a1b2c3d 로그인 컴포넌트 초기 구현 b2c3d4e 프로젝트 초기 설정 $ git blame src/components/Login.js a1b2c3d4 (CodeEater 2024-02-18 14:35:12 +0900 1) import React from 'react'; b2c3d4e5 (CodeEater 2024-02-18 14:35:12 +0900 2) import { useState } from 'react'; a1b2c3d4 (CodeEater 2024-02-18 14:35:12 +0900 3) a1b2c3d4 (CodeEater 2024-02-18 14:35:12 +0900 4) function Login() {
다음 단계로 나아가기
이제 깃의 기본 명령어들을 살펴보았습니다. 다음 글에서는 이러한 명령어들을 효과적으로 활용하는 깃 플로우 전략에 대해 알아보겠습니다.
명령어들이 너무 많아 보일 수 있지만, 실제로는 일상적으로 사용하는 명령어는 많지 않습니다.
저 또한 현재는 깃을 다루는 소프트웨어를 쓰고 있습니다.
처음에는 기본적인 add, commit, push, pull 정도만 익히고, 점차 다른 명령어들을 학습해 나가는 것을 추천드립니다.
블로그의 정보
코드먹방
codeEater활동하기
코드먹방코드라도 먹고 싶다