Let's Run Jinyeah

Git Branch & Merge 본문

Programming/Git&Github

Git Branch & Merge

jinyeah 2020. 2. 3. 01:12

Q. 수정 사항들을 local repository의 master branch에 바로 반영하고 싶지 않을 때는 어떻게 해야 할까?

새로운 branch를 생성하고 생성한 branch에서 수정한 후, 수정 사항들을 master branch에 merge한다.

 

1. 새로운 branch 생성 및 삭제

//branch 생성
git branch <branch_name>
//branch 삭제
git branch -d <branch_name>
//branch 강제 삭제
git branch -D <branch_name>

 

 

2. active(current) branch를 새로운 branch로 변경

  • active(current) branch란? local repository에 해당하는 branch
    • branch를 변경하면 local repository와 git status는 해당 branch의 내용을 보여준다.
    • 반면, remote repository(Github)는 항상 master branch를 보여준다.
git checkout <branch_name>

 

 

3. 새로운 branch를 생성하면서 동시에 새로운 branch로 변경

git checkout -b <branch_name>

 

4. 새로운 branch에서의 수정사항을 master branch에 반영

<working-directory> (new_branch)$ git commit -m "<message>" //새로운 branch에서 수정사항을 commit
<working-directory> (new_branch)$ git checkout master  //master branch로 이동
<working-directory> (master)$ git merge <name-of-branch-to-merge-in>  //새로운 branch를 병합

 

※ 주의!!

새로운 branch의 수정사항을 commit하지 않고 다른 branch로 이동하면, 자동으로 수정사항이 checkout하는 branch로 반영이 되고 충돌이 일어날 수 있다.

 

Q. commit하지 않고 다른 branch로 이동하려면 어떻게 해야 할까?

다른 공간에 변경 내역을 기록해 두고 다른 branch로 이동해서 작업한 후에 다시 돌아와서 변경 내역을 불러와 계속 작업을 이어나간다.

 

git stash  //코드를 잠시 stash 영역에 저장
git stash list  //stash 영역에 저장한 목록 확인
git stash apply  //가장 최근 stash 불러오기
git stash apply <stash 이름>  //특정 stash 불러오기
git stash drop //가장 최근 stash 제거하기

 

 

 

 

 

 

Comments