본문 바로가기

git & github

브랜치 관리

 

HEAD와 브랜치의 개념
$ git init test
Initialized empty Git repository in C:/Users/test/test/.git/

$ cd test/
$ vi c1.txt
1

$ git add c1.txt
warning: LF will be replaced by CRLF in c1.txt.
The file will have its original line endings in your working directory

$ git commit -m "c1"
[master (root-commit) 02f2340] c1
 1 file changed, 1 insertion(+)
 create mode 100644 c1.txt

$ git log
commit 02f234097e825a5584c292b61a848b9a73368525 (HEAD -> master)
Author: unknown <계정명@github.com>
Date:   Tue May 5 20:06:21 2020 +0900

    c1

$ git branch sub
  • HEAD는 현재 작업 트리(워킹 디렉토리)가 어떤버전을 기반으로 작업 중인지 가리킴
  • HEAD는 기본적으로 master 브랜치를 가리킴
  • 브랜치는 기본적으로 브랜치에 담긴 커밋 중 가장 최근의 커멋을 가리킴

 

$ vi c2.txt
2

$ git add c2.txt
warning: LF will be replaced by CRLF in c2.txt.
The file will have its original line endings in your working directory

$ git commit -m "c2"
[master 3cce51d] c2
 1 file changed, 1 insertion(+)
 create mode 100644 c2.txt

$ git checkout sub
Switched to branch 'sub'

$ vi s1.txt
s1

$ git add s1.txt
warning: LF will be replaced by CRLF in s1.txt.
The file will have its original line endings in your working directory

$ git commit -m "s1"
[sub acb447d] s1
 1 file changed, 1 insertion(+)
 create mode 100644 s1.txt

$ git log --oneline --branches
acb447d (HEAD -> sub) s1
3cce51d (master) c2
02f2340 c1

$ git reset 3cce51d
Unstaged changes after reset:
D       c2.txt

$ git log --oneline --branches --graph
* 3cce51d (HEAD -> sub, master) c2
* 02f2340 c1
  • 브랜치가 여러 개일 경우, 현재 브랜치가 아닌 다른 브랜치에 있는 커밋을 골라 최신 커밋으로 지정 가능

 

수정 중인 파일 감추기 및 되돌리기 : git stash

브랜치에서 파일 수정 중에 커밋하지 않은 상태에서 다른 파일을 커밋해야 할 경우,

아직 커밋하지 않고 작업 중인 파일들을 잠시 감춰둘 수 있음.

당장 필요한 작업들이 끝낸 후 다시 감춰둔 파일을 꺼내올 수 있음.

git stash 명령을 사용하려면 파일이 tracked 상태여야 함.(한 번은 커밋한 상태)

$ git init st
Initialized empty Git repository in C:/Users/test/st/.git/

$ cd st

$ vi f1.txt
1

$ git add f1.txt
warning: LF will be replaced by CRLF in f1.txt.
The file will have its original line endings in your working directory

$ git commit -m "f1"
[master (root-commit) 74fdf6e] f1
 1 file changed, 1 insertion(+)
 create mode 100644 f1.txt

$ vi f2.txt
2

$ git add f2.txt
warning: LF will be replaced by CRLF in f2.txt.
The file will have its original line endings in your working directory

$ git commit -m "f2"
[master b6d3857] f2
 1 file changed, 1 insertion(+)
 create mode 100644 f2.txt

$ vi f1.txt
1
1

$ vi f2.txt
2
2

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
        modified:   f2.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash
warning: LF will be replaced by CRLF in f1.txt.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in f2.txt.
The file will have its original line endings in your working directory
Saved working directory and index state WIP on master: b6d3857 f2

$ git status
On branch master
nothing to commit, working tree clean

$ git stash list
stash@{0}: WIP on master: b6d3857 f2
  • 가장 먼저 감춘 것은 stash@{0} 에 들어있음
  • 추후 다른 파일이 추가되면 기존 파일은 stash@{1}로 이동
  • 새로 추가된 파일은 stash@{0} 에 담김
  • 가장 최근에 보관한 것이 stash@{0} 에 담김
  • 스택 형태로 저장됨

 

$ git stash pop
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
        modified:   f2.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (9a825ade5ea14ae164ecbfa5bc5757920d458e4a)

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
        modified:   f2.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash list
  • 감춰둔 파일을 꺼내와 계속 수정하거나 커밋할 수 있음
  • stash목록에서 가장 최근 항목을 꺼내옴

 

$ git stash apply
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
        modified:   f2.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   f1.txt
        modified:   f2.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash list
stash@{0}: WIP on master: b6d3857 f2

$ git stash drop
Dropped refs/stash@{0} (e5a9785b3d71e332f39e15e2de89a0ce160e9a82)

$ git stash list
  • stash 목록에 저장된(숨겨둔) 내용을 나중에 사용할 가능성이 있으면, git stash apply 사용
  • stash 목록에서 가장 최근 항목을 되돌리지만 저장한 내용은 그대로 남겨둠
  • git stash drop 명령은 stash목록에서 최근 항목을 삭제함

 

'git & github' 카테고리의 다른 글

깃허브로 협업하기  (0) 2020.10.31
원격 저장소와 깃허브  (0) 2020.10.31
브랜치 병합  (0) 2020.10.31
깃과 브랜치  (0) 2020.10.31
Git으로 버전 관리하기 2  (0) 2020.10.31