본문 바로가기
코딩

[Git] Git / Github / Git repository

by Frontend 2022. 5. 19.

*키워드 : Git, Github, Git repository, remote repository, local repository, staging area, commit, snapshot, pull & push, Git 명령어 ... 등

 

 

Git repository 란 ?

파일 또는 폴더를 저장해두는 저장소.

  • Remote Git repository (원격 리포지토리) : 원격 온라인 상의 저장소.
  • Local Git repository (로컬 리포지토리) : 내 컴퓨터의 개인 저장소.

 


 

Local Git repository

  • 로컬 Git repository 만드는 법
    1. 코드를 저장할 directory(폴더) 생성 후, 해당 dir에 로컬 Git repository를 생성한다. 
      명령어 : git init
    2. 작업 공간(work space)의 파일 및 directory를 git 관리 하의 상태로 올려둔다. 올려두는 영역을 *staging area라고 부른다.
      명령어 : git add 
      ex) git add index.html
    3. staging area 에 존재하는 파일 repository로 *commit이 가능하다. commit 후에는 local repository에서 코드 기록이 가능하다. 
      명령어 : git commit

  • *staging area
    commit 하기 전에 내용을 기록하는 장소. 온전히 저장하고 싶은 코드를 모아놓은 묶음.

 

  • *commit
    stating area의 코드 묶음을 repository 에 올리는 행위.
    staging area에 있는 코드 내역을 그대로 snapshot을 찍어서 기록하는 행위.
    commit은 작은 단위로 자주 하는 것이 좋음.
    명령어 : git commit

  • commit message
    stating area 코드의 용도를 적어두는 행위.
    짧고 간결하게 사실적으로 작성하는 것이 좋음(동료 개발자가 참고할 수 있도록).
    명령어 : git commit -m "메시지 내용"
    ex) git commit -m "feat : 인스타 게시글 조회 페이지네이션".

 

  • git status (명령어)
    staging area의 untracked files을 확인할 수 있음.

 

 


 

Remote Git repository

  • 원격 Git repository 만드는 법
    1. Github에서 원격 git repository를 생성한다.
    2. 로컬 git repository에 원격 git repository url 주소를 등록한다. 연결 후, 'git remote -v' 입력하면 연결 유무 확인 가능.
      명령어 : git remote add
      예시) git remote add origin git@github.com:abc@remote-git-repository.git
      • origin : 앞으로 로컬 리포지토리에서 원격 리포지토리 주소를 대신하여 부를 name 
      • git@github.com:abc@remote-git-repository.git : url 주소
    3. 로컬 git repository에 기록한 내역을 원격 git repository에 push한다.
      명령어 : git push 

 

  • git push (명령어)
    로컬에서 원격으로 옮기는 작업.
    매개 변수를 지닐 수 있다 : git push <remote> <branch>.

    예시) git push origin main : 로컬 내의 기록을 원격 저장소인 origin의 main 브랜치로 push. 

  • git pull
    원격에서 로컬로 가져오는 작업.
    매개 변수를 지닐 수 있다.

 


 

기타 이용법

  • Fork
    repository를 복사하는 것.
    보통 Github의 원격 repository를 자신의 Github 원격 repository로 포크.
    이후 자신의 로컬 repository로 git clone을 하여 따로 개발이 가능하다.
  • Pull request 
    public project에 버그 수정 및 기능 추가 요청 작업(push & pull request).