Study

Git 관리하는 법

반응형

Git 명령어 정리

1. git 저장소 처음 생성

  • git 저장소 생성
git init
  • commit 하기위한 file 생성
echo "init git" >> README.md

git 저장소 생성후 commit 한 후에 branch가 생성가능.
commit 안하고 branch 생성할 경우 아래 error 메세지 발생

fatal: Not a valid object name: 'master'
  • add, commit 진행
git add README.md
git commit -m "init commit"
  • local 저장소와 remote 저장소 연결
git remote add origin [git_URL]
  • master branch 생성 후 push
git branch -M master
git push origin master

branch -M 옵션 : master 이름의 branch가 있어도 덮어쓰기로 생성

2. branch 생성, 원격 저장소와 연결

  • local 저장소에 develop branch 생성
git branch develop master

git branch [생성 branch 이름] [파생할 branch 이름]

  • add, commit, push 과정 진행
git add .
git commit -m "message"
git push origin develop

push할 경우 remote 저장소에 develop branch가 자동으로 생성
그냥 push 할 경우 error 발생(원격 저장소에 해당 branch가 없어서 발생)

git push  (에러 발생)
git push --set-upstream origin develop

원격 branch와 local branch 연결하는 방법

  • (원격 develop branch 생성후 local develop branch와 연결)
    git push --set-upstream origin develop
  • (원격 저장소에 develop branch가 있어야 됨)
    git branch --set-upstream-to origin/develop

3. pull request 하기

  • 가정
    • 원격 저장소에 origin/master, origin/develop 2개 branch가 있다.
    • Local 저장소에 master, develop 2개의 branch가 있다.(각 branch는 원격 branch와 연결 되어있음)
  • Local 저장소 develop branch에서 새로운 branch 1개 생성(작업할 branch)
# 방법 1
git branch DeepLearning develop
# 방법 2
git checkout -b DeepLearning
# checkout -b 옵션은 DeepLearning branch가 없을 경우 branch를 생성하고 해당 branch로 이동한다.
  • 변경 내용 add, commit, push 과정 진행
git add .
git commit -m "[Fix] aaaa, [Add] b"
git push origin DeepLearning

위 명령어 입력 후 원격 저장소에 DeepLearning branch가 새로 생기고 push가 완료

  • Github repository에 가서 pull request 진행
    • Github repository에 compare & request 버튼이 생기고 버튼 클릭.
    • 수정 내용 및 comment 작성
    • create pull request 버튼 클릭하여 pull request 완료

Git 상황별 정리

Local에서 원격 저장소의 branch가 안나올 때

git fetch --all --prune  
git remote prune origin  

위 명령어 입력 후 아래 명령어로 remote branch 확인

git branch -r  
반응형

'Study' 카테고리의 다른 글

MarkDown 사용법 정리  (0) 2020.09.06