본문 바로가기
Development/Git

[Git_2] 기본 명령어 모음 (git init, git status, git add, git commit)

by yoonsoodev 2022. 10. 6.

1. 저장소 만들기 (git init)

 명령어 입력

  • 명령어 종류 및 설명
    • mkdir (디렉토리이름)
      • 디렉토리 생성
    • cd (이동하고자 하는디렉토리)
      • 해당 디렉토리로 이동
    • touch (파일 이름)
      • 빈 파일 생성
    • echo "[글자]" >> [파일]
      • 파일에 해당 글자 추가
  • 명령어 입력
mkdir sample
cd sample
touch red orange
echo "빨강" >> red
echo "주황" >> orange
git init

❓ 출력 결과

  • sample 디렉토리에 Git 저장소 생성
  • 디렉토리 하위에 .git 디렉토리 생성 - Git과 관련된 정보 저장
    • 이때 sample디렉토리> 보기 메뉴 > 숨긴파일 체크 시 git 파일이 보인다
  • 쉘 프롬프트가 ➜ sample에서 ➜ sample git:(main)로 변경

 

2. 현재 상태 확인 (git status)

 명령어 입력

  • zsh 단축어: gst
    • ohmyzsh설치 후 사용 가능하다
    • git bash에 들어가 zsh 입력 후 사용 가능하다
  • 명령어 입력
git status

 출력 결과

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	orange
	red

nothing added to commit but untracked files present (use "git add" to track)
  • 현재 브랜치 확인 → On branch main
  • 커밋 상태 확인 → No commits yet
  • Untracked files(추적하지 않는 파일) 존재 확인

 

3. 현재 상태 추적 (git add)

 명령어 입력

  • 명령어 설명
    • 파일의 변경 사항을 인덱스(=Staging Area)에 추가
    • git은 commit 전에 인덱스, 즉 Staging Area에 커밋할 파일을 추가한다
    • zsh 단축어로 git add -A는 gaa사용
  • 명령어 입력
    1. -A 옵션을 이용하여 전체 파일(orange, red)을 인덱스에 추가
    2. 상태 확인
git add -A
git status

 

❓ 출력 결과

On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   orange
	new file:   red
  • Untracked files의 변경
    • use "git add <file>..." to include in what will be committed  use "git rm --cached <file>..." to unstage로 변경
    • orange와 red에 대해서 new file임을 알려줌

 

 

5. 기본 명령어 - 현재 상태 저장 (git commit)

 명령어 입력

  • zsh 단축어: gc -m “v1 commit”
  • 명령어 설명
    • Staging Area에 추가된 변경사항을 이력에 추가함
    • -m 옵션: 커밋에 대한 메시지 작성
git commit -m "v1 commit"

❓ 출력 결과

[main (root-commit) 25354ae] v1 commit
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 orange
 create mode 100644 red

커밋 생성!

'Development > Git' 카테고리의 다른 글

[Git_1] Git 기초 지식 (Introduction of Git)  (0) 2022.10.06