🔥 git add

346자
4분

개발자가 작업한 파일을 Git의 스테이징 영역(Staging Area)에 추가하려면 git add 명령어를 사용합니다. 이렇게 하면 변경된 파일을 커밋할 준비가 되지요.

예를 들어, 현재 디렉토리에 example.txt 파일을 새로 생성했다고 가정해 봅시다. 이 파일을 스테이징 영역에 추가하려면 아래와 같이 명령어를 실행하면 됩니다.

shell
$ touch example.txt
git add example.txt
shell
$ touch example.txt
git add example.txt

또는 현재 디렉토리의 모든 변경 사항을 한 번에 추가하고 싶다면 아래와 같이 .을 사용할 수 있습니다.

shell
git add .
shell
git add .

git add 명령어는 다양한 옵션을 제공합니다. 예를 들어:

  • p: 변경된 파일의 특정 부분만 선택적으로 추가할 수 있습니다.
  • u: 이미 추적 중인 파일의 변경 사항만 추가합니다.
  • A: 새로운 파일, 수정된 파일, 삭제된 파일 모두를 추가합니다.

실제로 git add를 사용해 보겠습니다. 먼저 새로운 파일을 생성하고 내용을 추가해 보죠.

shell
echo "Hello, Git!" > greeting.txt
shell
echo "Hello, Git!" > greeting.txt

그리고 git status 명령어로 현재 상태를 확인해 보면:

shell
On branch main
Your branch is up to date with 'origin/main'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   example.txt
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        greeting.txt
shell
On branch main
Your branch is up to date with 'origin/main'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   example.txt
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        greeting.txt

새로 생성한 greeting.txt 파일이 "Untracked files"에 있는 것을 확인할 수 있습니다. 이제 git add로 이 파일을 스테이징 영역에 추가해 봅시다.

shell
git add greeting.txt
shell
git add greeting.txt

다시 git status를 실행하면:

shell
$ git status
On branch main
Your branch is up to date with 'origin/main'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   example.txt
        new file:   greeting.txt
shell
$ git status
On branch main
Your branch is up to date with 'origin/main'.
 
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   example.txt
        new file:   greeting.txt

greeting.txt가 "Changes to be committed"로 이동한 것을 볼 수 있습니다. 이제 이 파일은 커밋할 준비가 된 것이지요.

이처럼 git add 명령어를 사용하면 작업한 변경 사항을 손쉽게 스테이징 영역에 추가할 수 있습니다. 개발자는 git add로 커밋할 파일들을 선택한 뒤 git commit으로 변경 사항을 커밋하게 됩니다.

lecture image

위의 다이어그램은 Git의 전체 작업 흐름을 보여주고 있습니다.

  1. git add로 Working Directory의 변경 사항을 스테이징 영역에 추가하고,
  2. git commit으로 스테이징 영역의 내용을 Local Repository에 반영한 뒤,
  3. git push를 사용하여 Local Repository의 커밋을 Remote Repository로 전송합니다.

개발자는 이 과정을 반복하면서 코드의 변경 사항을 관리하게 되는데요. git addgit commit으로 의미 있는 변경 사항을 잘 정리하고, git push로 협업하는 팀원들과 공유하는 것이 중요합니다.

YouTube 영상

채널 보기
class-validator 와 DTO | NestJS 가이드
Writer 펑터와 클라이슬리 카테고리 | 프로그래머를 위한 카테고리 이론
NestJS 파이프가 뭔가요? 컨트롤러를 보호하는 방법 | NestJS 가이드
미들웨어 vs 가드, 왜 NestJS에서는 가드가 더 똑똑할까? | NestJS 가이드
NestJS 빌트인 파이프 ParseIntPipe, ParseUUIDPipe 사용하기 | NestJS 가이드
클로드 섀넌이 들려주는 정보 이론 이야기
존 매카시가 들려주는 인공지능의 탄생 이야기
NestJS 가드, 바이딩과 스코프 | NestJS 가이드