git - newlife-js/Wiki GitHub Wiki

Git 공부

참고


1. init

git init

git config user.name ' '
git config user.email ' '

2. repository 연결

2-1. clone : 기존 repository 복사 및 download

git clone <리포지터리 url> <디렉터리>

2-2. remote add : 작업중이던 영역을 repository에 연결

git remote add <리포지터리 이름>(내가 원하는 대로, 기본은 origin) <리포지터리 url>
git push -u <리포지터리 이름> <branch 이름>
-u(--set-upstream) : remote repository에 branch를 생성하고 local branch가 remote branch를 tracking하도록 설정


3. branch

git branch <branch 이름> // branch 생성
git switch <branch 이름> // branch 이동
git branch -d <branch 이름> // branch 삭제

4. checkout

checkout은 HEAD를 이동하는 역할
HEAD : git 사용자의 현재 위치

4.1 commit id로 checkout

HEAD만 해당 commit으로 이동(detached HEAD상태가 됨)
해당 commit에서 새로운 branch를 만들어 사용할 때 보통 사용함.

git checkout <commit id>

git checkout -b <새로운 branch 이름> <commit id> // 새로운 branch도 만들어줌
git checkout -B <branch 이름> <commit id> // 이미 존재하는 branch를 해당 commit으로 reset(변경사항 있으면 -f로 강제할 수 있음)

4.2 branch로 checkout

HEAD가 해당 branch가 있는 commit으로 이동

git checkout <branch 이름>
git checkout -b <새로운 branch 이름> // 지금 HEAD에 새로운 branch만들어서 attachg

git checkout -f -B <branch 이름> <원하는 branch 이름(remote도 가능)>
// 기존에 존재하는 branch를 원하는 branch로 이동

* reset : HEAD가 가리키던 branch를 다른 commit으로 이동

git reset --soft // working directory, staging area 유지
git reset --mixed // working directory 유지, staging area reset
git reset --hard // working directory, staging area reset

5. stage

working space(directory)의 변경 사항을 staging area(index)에 올리기

git add <파일명>
git add . // 수정사항 있는 모든 파일 올리기

git restore <파일명> // 수정 사항 되돌리기
git reset <파일명> // stage된 파일 다시 내리기

6. commit

staing area에 있는 변경사항을 local repository에 올리기

git commit -m "메시지"

git commit --amend -m "메시지, Change-Id: <change id>"
// 기존 커밋을 push하기 전에 수정한 내용 함께 commit
// amend를 하지 않으면 여러 개의 commit이 올라가게 된다.

7. push

local repository를 remote repository로 올리기

git push <repository 이름> <local branch 이름>:<remote branch 이름>
git push <repository 이름> <local branch 이름>:refs/for/<remote branch 이름>
// gerrit에서 코드 리뷰를 받기 위해서는 refs/for/를 같이 입력해주어야 함.

* gerrit에 해당 remote branch가 생성되어 있어야 함

remote branch의 initial revision은 parent commit의 id


8. fetch & pull

remote repository의 변경 사항 가져오기

8.1 fetch

가져오기만 함

git fetch | <repository 이름>

8.2 pull

가져와서(fetch) 현재 HEAD에 merge

git pull | <repository 이름> <local branch 이름>:<remote branch 이름>

9. merge

현재 branch에 다른 branch의 변경사항을 합치기

git merge <다른 branch 이름>

* merge가 성공적으로 이루어졌을 경우에 자동 commit이 된다.

자동 commit을 방지하기 위해서는 --no-commit 옵션을 추가하면 된다.

* fast-forward : merge하려는 branch가 현재 branch의 자손일 경우, commit 없이 해당 branch로 이동함.

* conflict : merge하려는 branch와 같은 파일을 수정했을 경우 merge conflict가 발생한다.

이 경우에는 해당 파일에 conflict내용이 저장되어 있는데, conflict된 내용을 처리한 후 저장한다.
그 후에 staging -> commit -> push를 해주어야 remote repository에 merge가 반영된다.


10. stash

변경 사항을 임시 저장소(stack)에 저장

git stash save // 저장
git stash apply // 불러오기

작업 중이던 내용을 commit하지 않고 다른 branch에서 작업할 때,

작업 중이던 branch에 pull을 할 때 conflict 해결을 위해 (stash save -> pull -> stash apply -> conflict 파일 수정(merge conflict과 같은 방식) -> 해결)

⚠️ **GitHub.com Fallback** ⚠️