본문 바로가기

공부방/Upstage AI Lab 4기

GIT | Git 쓰는 법 다시 정리!!! (초보용)

1. Git으로 관리하고 싶은 폴더를 만든다.

2. 생성한 폴더로 이동한 다음에 git init 으로 초기화를 해준다.

3-1. Github 레포지토리를 연결한다. (새로운 깃 레포지토리를 만들었을 때)
git remote add origin 주소
git remote -v 를 하면 잘 연결됐는지 확인할 수 있다.

3-2. 기존 github에 레포지토리를 가져올 때
git clone 주소
cd repo-name 

4. 초기 커밋을 생성하고 푸시한다. (이 방식을 사용하면 로컬에서 작업을 시작하고 필요할 때 원격 레포지토리와 동기화할 수 있습니다.)
git add .
git commit -m "Initial commit"

더보기

*문제상황

git remote add origin 주소를 하고 내 로컬에서 새로운 브랜치를 만들려고 할 때

fatal: not a valid object name: 'main'

이런 오류가 났다. 이건 Git 저장소가 아직 초기화되지 않았거나, 첫 번째 커밋이 없는 경우에 발생한다고 하길래 참조할 커밋이 필요하다고.. 그래서 일단 커밋 한번 하고 브랜치를 새로 만들거나, git checkout -b 브랜치이름 이렇게 하면 또 그냥 됨.

5. 브랜치 만들기
git branch 브랜치이름
git checkout -b : 브랜치를 생성하면서 동시에 그 브랜치로 이동
git checkout 브랜치이름

5-1. 브랜치에 정리한 파일 push
git push origin 브랜치이름

 

더보기

* 문제상황 (main에 바로 합칠 수 없도록 금지된 경우) 

git push origin main을 했을 때

Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 16 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 298 bytes | 298.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: error: GH013: Repository rule violations found for refs/heads/main. remote: Review all repository rules at http://github.com/jihwanK/regression/rules?ref=refs%2Fheads%2Fmain remote: remote: - Changes must be made through a pull request. remote: To github.com:jihwanK/regression.git ! [remote rejected] main -> main (push declined due to repository rule violations) error: failed to push some refs to 'github.com:jihwanK/regression.git'

이거는 main 브랜치에 직접 변경사항을 푸시하는 것이 금지되어 있다는 뜻이라고.

해결 방법: 새로운 브랜치 생성 및 푸시

새로운 브랜치를 생성한 다음(또는 작업할 브랜치로 가서)에 git pull origin main을 하면 새 브랜치를 main의 최신 상태로 업데이트됨. 

예를 들면
git branch dev
git checkout dev
git pull origin main (git pull origin dev 아님!! dev는 깃허브에 존재하지 않는 브랜치) 이 명령어는 원격 저장소(origin)의 main 브랜치에서 현재 브랜치(new)로 변경사항을 가져와 병합합니다.

이렇게 하면 dev 브랜치가 origin main의 최신상태로 업데이트됨. 그래서 이걸로 업데이트한 다음에 작업하고
git add .
git commit -m "커밋명"
git push origin 내가작업하고있던브랜치!