IT/Others

[GIT] git 기초 사용법

승돌 2023. 2. 22. 17:24
반응형

- 로그인

로그인 계정 설정

//--global 옵션을 사용하면 한 번만 설정하면됨
git config --global user.name "~~"
git config --global user.email ~~.@gmail.com

로그인된 계정 확인

git config user.name
git config user.email

계정 변경

git config --global user.name
git config --global user.email

- git remote & branch

//연결
git remote add origin 원격 저장소 주소

//연결된 저장소 삭제
git remote remove origin

//현재 연결되어있는 git 저장소 확인
git remote -v

//git remote branch 조회
git branch -a

//git 브랜치 생성
git branch 생성할 브랜치이름

//branch 삭제
git branch -d 브랜치이름

//remote branch 삭제 적용
git push origin --delete 삭제한 브랜치이름

//git remote branch 변경
git switch 브랜치이름

//git merge
//기준 branch로 이동
git switch main
//main 브랜치에 aaa 브랜치를 병합
git merge aaa

//git remote 별칭 변경
git remote rename origin aa

- local → remote

//local git directory 생성
git init

/.git 파일 삭제
rm -rf .git

//로컬 저장소 -> staging area
git add .

//staging area에 존재하는 파일, 디렉토리 확인
git status

//staging area -> git directory
git commit -m "커밋메시지"

//로컬 저장소 -> 원격 저장소
git push [remote 별칭] [branch 이름]
ex) git push origin main

- add

//staging area에서 제거
git rm (-r) --cached sample.txt

- commit

//commit 이력 조회
git log

//가장 최근 commit 삭제
git reset HEAD^

//이전 commit 버전으로 변경
git checkout commit log ID
ex) git checkout dad9348

//브랜치의 가장 최근 버전 commit
git checkout 브랜치이름
ex) git checkout main

//commit 메시지 수정
1. git rebase -i
텍스트 에디터 pick f234 커밋메시지 → edit f234 커밋메시지 수정, --abort
2. git commit --amend -m "커밋메시지 수정"

- push

//원격 저장소 파일 삭제
git rm --cached sample.txt
git commit -m "커밋메시지"
git push origin main

 
- gitignore

프로젝트 디렉토리 최상단에 .gitignore 파일 생성
아래와 같이 무시될 파일, 디렉토리를 적어줌
sample.txt
DATA/
*.json

- error

1. 로컬, 원격 저장소의 연결고리가 없어서 생기는 오류

 

오류
! [rejected]        main -> main (fetch first)
error: failed to push some refs to '원격주소지'

원인
로컬 저장소, 원격 저장소의 상태가 달라서 push하는데 오류가 생기는 경우

//해결방법
git pull


오류
fatal: refusing to merge unrelated histories


원인
서로 관련 기록이 없는 두 프로젝트를 병합할 때 git 거절하는 경우

//해결방법
git pull origin main --allow-unrelated-histories

 

오류
Please move or remove them before you merge


원인
로컬의 untracked 파일과 원격 저장소 파일들이 충돌해서 pull할 수 없는 상황

//해결방법
//현재 디렉토리의 파일을 임시로 백업하고 pull, 임시 저장한 파일을 병합
//Untracked -> tracked 변경
git add *
//임시 백업
git stash
//원격 저장소 파일 덮어쓰기
git pull
//Untracked였던 파일들은 Untracked 파일로 다시 복원
git stash pop

 

2. 용량이 너무 큰 파일을 push한 경우

//오류
//[git - error] RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8)

//버퍼 늘리기
git config --global http.postBuffer 524288000

//용량이 큰 파일명과 함께 다시 오류 메시지
//직전 commit 취소
git reset HEAD^
//.gitignore에 용량이 큰 파일명 추가
git add .gitignore
git commit -m "gitignore update"
//다시 git add, commit, push

 

반응형