오늘도 개발

Git checkout, restore, switch 본문

웹 프로그래밍/Git

Git checkout, restore, switch

Sueeeeeee 2023. 2. 18. 16:37

Git 공식문서에 따르면 checkout은 deprecated된 명령어이다.

Git checkout 명령이 맡고있던 두 기능은 switch와 restore로 나누어졌다.

 

git switch

다른 브랜치로 이동할 때 사용.

git switch <브랜치명>
git switch -c <새 브랜치명> # 새 브랜치 만들고 이동

 

git restore

파일을 특정 커밋 직후 시점으로 복구하는 명령어.

 

옵션 없이 사용하면 명시한 파일을 Head Commit(최종 커밋)으로 복구.

(=최종 커밋 이후의 작업을 날리고, 최종 커밋 직후의 상태로 파일을 돌려놓음)

git restore <파일명>
# 예 : git restore Desktop/tutorial/snippets.py
# 최종 커밋 직후 상태로 복원

git restore --source <commit hash> <파일명>
# 예 : git restore 044f7bb Desktop/tutorial/snippets.py
# 특정 커밋 직후 상태로 복원

git resotore --staged <filename>
# add 명령어를 사용하여 스테이징에 올린 파일 unstaging

git restore 예시

1. feature/test1 브랜치에서 test.txt에 1, 2, 3, 4, 5 작성 후 커밋(메시지 EDIT: added numbers from 1 to 5)

1
2
3
4
5

2. feature/test1 브랜치에서 test.txt에 6, 7, 8 작성 후 커밋(메시지 EDIT: added numbers from 6 to 8)

1
2
3
4
5

6
7
8

4. git log --oneline

HEAD commit은 83a5a4b

83a5a4b (HEAD -> feature/test1) EDIT: added numbers from 6 to 8
044f7bb EDIT: added numbers from 1 to 5
c46828d (origin/main, main) ADD: added test1.txt

5. test1.txt에 실수로 90 작성 

1
2
3
4
5

6
7
8
90

5. git restore test1.txt => 최종 커밋 직후의 상태로 복원

1
2
3
4
5

6
7
8

 

git restore --staged <파일명> 예시

(위의 5번부터 계속)

6. test1.txt에 9, 10 작성

1
2
3
4
5

6
7
8

9
10

7. git add test1.txt

8. git status

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   test1.txt

8. add하고 보니 test1.txt는 커밋하고 싶지 않음

9. git restore --staged test1.txt 

10. git status

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:   test1.txt

'웹 프로그래밍 > Git' 카테고리의 다른 글

merge conflict 터미널에서 한 번에 해결하기  (0) 2023.02.18
Git stash  (0) 2023.02.18
Git rebase  (0) 2022.08.02
Git flow  (0) 2022.08.02
[Git] push, merge, pull로 브랜치를 항상 최신화하는 방법  (0) 2022.07.09