brunch
매거진 배포

Git

by 내가 사는 세상


처음 시작하기


git init

해당 폴더 git이 감시 시작

repository 생성


git branch -M main

브랜치의 이름을 변경

-M 옵션은 브랜치 이동을 강제로 수행


원격


로컬

그림35.png


git add .

스테이징 하기


git commit -m "first commit"

repository로 이동


git remote add origin git@github.com:<깃허브 아이디>/<원격 저장소 이름>.git

origin이라는 이름으로 GitHub의 <깃허브 아이디>/<원격 저장소 이름>.git 원격 저장소를 추가하는 역할

이후 origin이라는 원격 저장소의 별칭(alias)으로 원격 저장소를 참조하여 작업 가능

git remote add origin : Git에서 원격 저장소를 추가하는 명령어

git@github.com:<깃허브 아이디>/<원격 저장소 이름>.git: 추가할 원격 저장소의 주소


git push -u origin main

git push -u <원격저장소 주소> <로컬 브랜치 이름>

-u 는 upstream 약자




가지치기(branch)


git branch 가지1 : 가지치기

git switch 가지1 : 친 가지로 이동

git branch -d 브랜치명 : 이미 merge된 브랜치 제거

git branch -D 브랜치명 : 아직 merge 안된 브랜치 제거


그림36.png


가치 합치기(merge)

- 3way merge

: 수정 코드가 곂치지 않으면 정상 merge

: 수정 코드가 곂치면 conflict 발생


- fast-forward merge



- rebase & merge



- squash & merge



기타 명령들


git status : 상태 조회

git log -3

git log --oneline --all

git log --oneline --all --graph

git diff

git difftool

git remote -v : 현재 깃 저장소가 뭔지 보여주기



원격 저장소의 잘못 올린 더, 파일 제거

C:\>git rm --cached -r .idea

C:\>git commit -m "잘못 올린 폴더 삭제"

C:\>git push




CI/CD 중 Git 인증 에러

private repo를 사용할 때 문제 발생


에러

err: fatal: could not read Username for 'https://github.com': No such device or address


해결책

git remote set-url origin https://{access_key:developers에서 발급받은 것}@github.com/JeongJaeyoung/iintheworld.git


pull할 때(원격 저장소 내용을 로컬저장소로 불러올 때) conflict 에러나면


git reset --hard HEAD

git pull

https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files




커밋 합치기

#git rebase -i [수정을 시작할 커밋의 이전 커밋]

git rebase -i HEAD~3



참고자료


코딩애플 : 매우쉽게 알려주는 git & github

https://batory.tistory.com/479





keyword
매거진의 이전글Docker 명령어