git 기본 명령어 (init, add, commit, push)

단간단간·2024년 3월 26일

Git

목록 보기
1/1
post-thumbnail

git 이름 및 이메일 지정하기

git 이름 지정

$ git config --global user.name "이름"

git 이메일 지정

$ git config --global user.email 이메일

git 저장소 생성하기

git 저장소 생성

$ git init

git 저장소 생성은 프로젝트 당 한번씩만 실행한다.
git init을 실행하면 하위 디렉토리에 .git 디렉토리가 생성된다. .git 디렉토리를 제거할 경우 git 이력도 함께 제거된다.

예시

$ cd /path/to/my/codebase
$ git init      (1)
$ git add .     (2)
$ git commit    (3)
  1. Create a /path/to/my/codebase/.git directory.
  2. Add all existing files to the index.
  3. Record the pristine state as the first commit in the history.

git add & git commit

working directory : 프로젝트에서 실제로 작업하는 공간(디렉토리)
staging area : 커밋하기 전에 변경사항들을 등록하는 곳. 중간 단계
repository : 저장소 (.git 폴더)

Commit WorkFlow

working directory -> git add -> staging area -> git commit -> repository(local)

  • working directory에 변경사항을 만든다.
  • git add는 변경사항을 staging area에 올린다.
  • git commitstaging area에 올려진 변경사항들을 repository에 올린다.
    git commit을 하면 .git 안에 있는 것들을 변경해서 새 커밋을 폴더에 등록하게 되며, 이는 깃 저장소를 업데이트 하는 것이다.

git add 명령어

파일 1개의 변경사항만 반영하는 경우

$ git add file1

파일 2개 이상의 변경사항을 반영하는 경우 (파일 이름 공백으로 분리)

$ git add file1 file2

모든 파일의 변경사항들을 반영하는 경우

$ git add .

git commit 명령어

git commit -m "커밋 메세지"
(커밋 메세지 : 해당 커밋에 포함되는 변경사항들에 대한 요약 내용)

ex) 
$ git commit -m "회원 로그인 API 추가"

git push

Push WorkFlow

working directory -> git add -> staging area -> git commit -> repository(local) -> push -> repository(remote)


git commit & git push 차이

  • git commit : 로컬 저장소에 코드 변경 이력 저장
    (로컬 저장소란? git clone 명령어를 통해 내 로컬에 저장해 둔 원격 저장소의 복사본)
  • git push : 로컬 저장소에 있는 변경 이력들이 원격 저장소로 전송됨

git push 명령어

git push <저장소명> <브랜치명>

ex) 
$ git push origin main

원격 저장소명은 보통 origin 이다.
git remote 명령어로 정확한 저장소명을 확인할 수 있다.

$ git remote
origin

참고

profile
simple is best

0개의 댓글