/ GIT

Git 사용방법

Local Repository 생성 (PC)

새로운 git 프로젝트를 만들 때 사용

  • 원하는 위치에 프로젝트 폴더를 생성
  • git bash 상에서 해당 폴더로 이동해 git init 명령 실행 (Local Repository .git/ 생성)
  • 해당 폴더에서 소스코드파일을 만듦 (ex. test.c)
    • git status 명령 실행하면 untracked 파일로 test.c이 있다(변경사항이 있다)고 나옴 (Modified)
  • git add test.c 명령 실행하여 test.c 파일이란 변경사항을 관리할 준비가 되었다고 알림
    • git status 명령 실행하면 commit할 변경들이 있다고 나옴 (Staged)
  • git commit -m “test.c commit” 명령 실행하여 최종 변경을 git에 저장함 (Committed)

Remote Reposity 활용 (Github)

새 프로젝트 시작

  1. Github에 프로젝트 Repository를 생성
  2. Local Repository 내 개발 환경설정/개발사항 저장
  3. Local Repository와 Github의 Repository를 remote 연결
  4. Local Repository 저장상황을 Remote Repository에 push
  5. (공동작업시) 해당 Remote Repository를 clone
    • (공동작업자) clone을 통해 로컬로 복사한 프로젝트로 작업 진행

기존 프로젝트 수정

기존 Local Repository에 존재

  1. Github Repository와 remote -v명령으로 연결 확인
    1. 연결안되어 있으면 다시 remote 연결
  2. Github Repository 수정사항들을 Local Repository로 fetch 또는 pull함
    • fetch : Github의 commit들을 Local Repository로 가져옴
      • 개발자가 자신의 작업과 적절히 merge하여 다시 push해야 함
    • pull : Github의 commit들을 가져오면서 자동으로 Local Repository와 병합
      • 편하지만 conflict 발생 확인 어려움

Local로 Github Branch 가져오기

git pull으로 가져오지 못함 (Local에 없다면)

  1. git remote에 대한 업데이트 실행
    git remote update
    
  2. Github Repository의 branch 내역 확인
    • remote branch 내역
      git branch -r
      
    • local/remote branch 내역
      git branch -a
      
  3. 원하는 branch를 Local로 가져와서 해당 branch 사용
    git checkout -t  <Local Branch Name>  "Remote Branch Name"
    
    • Local Branch Name 생략가능하나 Remote와 다르게 하고 싶다면 적어도 됨

Git Remote 관련 명령

  • git remote add “사용할 원격저장소이름” “Github Repository URL”

    git remote add origin https://github.com/HweeJoon-Chung/RemoteProject.git
    
  • git remote -v

  • git push “원격저장소이름” “local branch명”

    git push origin main
    

    ​ 모든 branch push

      git push origin --all (모든 branch)
    
  • git fetch “원격저장소이름”

    git fetch origin
    
  • git pull

  • git clone “Github Repository URL”

    git clone https://github.com/HweeJoon-Chung/RemoteProject.git
    
-->