Git(5) : checkout

notepad·2023년 5월 7일
0

git

목록 보기
5/14

대부분의 경우 git checkout <commit-hash> 을 하면
detached head 상태가 된다.
head 는 기본적으로 특정 브랜치를 참조한다.
이말은 head는 특정브랜치의 최신 commit을 가르킨다는 것이다.
(branch ref 와 head의 위치가 동일)

이것이 일반적인 상태라 보면되고

이것이 detached head가 된 상태라 보면된다.

일단 detached 상태에서 git switch <branch> 를 통해 정상적인 상태로 복귀 가능하다.

veritas@veritas:~/gitserver/gittest$ git branch
  dev1
* master
veritas@veritas:~/gitserver/gittest$ git log --oneline
0832b08 (HEAD -> master) change
9604190 modified merge
3085d7f (dev1) change2
c89332b change1
07e0d34 init2
6793199 init
veritas@veritas:~/gitserver/gittest$ git checkout 07e0d34
Note: switching to '07e0d34'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 07e0d34 init2
veritas@veritas:~/gitserver/gittest$ git status
HEAD detached at 07e0d34
nothing to commit, working tree clean
veritas@veritas:~/gitserver/gittest$ git switch master
Previous HEAD position was 07e0d34 init2
Switched to branch 'master'
veritas@veritas:~/gitserver/gittest$ git status
On branch master
nothing to commit, working tree clean

detached head는 그렇다면 어디다 써먹는가
특정 <commit-hash>로 이동하여 그상태에서 새로운 branch를 만드는 용도로 쓸수있다.
특정 commit으로 이동하여 그 상태에서 새로운 branch를 만드는 예제

veritas@veritas:~/gitserver/gittest$ git branch
  dev1
* master
veritas@veritas:~/gitserver/gittest$ git log --oneline
0832b08 (HEAD -> master) change
9604190 modified merge
3085d7f (dev1) change2
c89332b change1
07e0d34 init2
6793199 init
veritas@veritas:~/gitserver/gittest$ git checkout c89332b
Note: switching to 'c89332b'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at c89332b change1
veritas@veritas:~/gitserver/gittest$ git switch -c dev2
Switched to a new branch 'dev2'
veritas@veritas:~/gitserver/gittest$ git status
On branch dev2
nothing to commit, working tree clean
veritas@veritas:~/gitserver/gittest$ git branch
  dev1
* dev2
  master
veritas@veritas:~/gitserver/gittest$ git touch c.py
git: 'touch' is not a git command. See 'git --help'.
veritas@veritas:~/gitserver/gittest$ touch c.py
veritas@veritas:~/gitserver/gittest$ vim c.py 
veritas@veritas:~/gitserver/gittest$ git status
On branch dev2
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        c.py

nothing added to commit but untracked files present (use "git add" to track)
veritas@veritas:~/gitserver/gittest$ git add .
veritas@veritas:~/gitserver/gittest$ git commit -m "change"
[dev2 a132643] change
 1 file changed, 3 insertions(+)
 create mode 100644 c.py
veritas@veritas:~/gitserver/gittest$ git status
On branch dev2
nothing to commit, working tree clean
veritas@veritas:~/gitserver/gittest$ git branch
  dev1
* dev2
  master

git checkout HEAD~n

git checkout HEAD~1 git checkout HEAD~2 등으로 현재 상태 기준으로
n번째 commit으로 head 이동가능.
여기서 다시 정상적으로 돌아가려면
git switch <branch> 혹은 git switch -을 하면 가장 직전의 branch로 돌아간다.

git checkout HEAD <file>

git checkout -- <file>

특정파일을 마지막 커밋에서의 내용으로 재설정함.
즉 마지막으로 commit 했을때의 모습으로 되돌림
git checkout HEAD a.txt b.txt 같이 여러개도 동시에 가능
하지만 restore 명령어가 상위호환이므로 딱히 쓸일은 없을것

0개의 댓글