• 카테고리

    질문 & 답변
  • 세부 분야

    개발 도구

  • 해결 여부

    미해결

git fetch 정말 필요할까요?

21.12.29 23:52 작성 조회수 1.4k

0

local에서 push한 후에 remote에서 수정해서 커밋을 했습니다.

그리고 fetch를 하려고했는데, fetch하기 전에 한번

git checkout origin/main

을 해보았습니다

 

fetch를 하지 않아서 원격에서 커밋한 내용이 안보일 줄 알았는데 잘 보이더라구요..

 

fetch 안해도 잘 보이던데 꼭 해야되나요?

답변 2

·

답변을 작성해보세요.

1

안녕하세요 :) 두 번째 질문에 대해 답해드립니다.

 

git fetch 를 하지 않고는 FETCH_HEAD로 checkout 하실 수 없습니다.

아래 코드를 보시면 fetch 전에는 FETCH_HEAD로 checkout 하실 수 없는 걸 알 수 있습니다.

(어쩌면 pull 혹은 fetch를 질문자님이 기억하지 못하는 사이에 하신게 아닐까 짐작해봅니다..)

 

minchul@DESKTOP-9KULGUE MINGW64 /c/test
$ git init
Initialized empty Git repository in C:/test/.git/

minchul@DESKTOP-9KULGUE MINGW64 /c/test (master)
$ echo "A" >> test.txt

minchul@DESKTOP-9KULGUE MINGW64 /c/test (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

minchul@DESKTOP-9KULGUE MINGW64 /c/test (master)
$ git commit -m "1"
[master (root-commit) 67abdd8] 1
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

minchul@DESKTOP-9KULGUE MINGW64 /c/test (master)
$ git branch -M main
git push -u origin main
minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git remote add origin https://github.com/kangtegong/test-repo.git

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 206 bytes | 206.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/kangtegong/test-repo.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git checkout FETCH_HEAD
error: pathspec 'FETCH_HEAD' did not match any file(s) known to git

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 629 bytes | 57.00 KiB/s, done.
From https://github.com/kangtegong/test-repo
   67abdd8..e3c05aa  main       -> origin/main

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git checkout FETCH_HEAD
Note: switching to 'FETCH_HEAD'.

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 e3c05aa Update test.txt

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((e3c05aa...))
$ cat test.txt
A
B

 

 

그리고, 원격저장소가 변경되면 fetch를 해야만 로컬 저장소에 반영이 됩니다.

아래 예시를 보시면, 

git checkout FETCH_HEAD로 확인한 test.txt의 내용이

fetch를 하고 다시 git checkout FETCH_HEAD 하기 전까지는 그대로인걸 알 수 있습니다.

 

---- 원격 저장소에서 C라는 문자 추가 -----

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((e3c05aa...))  # 현재 head(e3c05aa...)는 C라는 문자가 추가되기 전 FETCH_HEAD
$ cat test.txt
A
B

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((e3c05aa...))
$ git fetch                      # 다시 fetch 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 628 bytes | 52.00 KiB/s, done.
From https://github.com/kangtegong/test-repo
   e3c05aa..899c073  main       -> origin/main

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((e3c05aa...))     # (e3c05aa...)는 여전히 C라는 문자가 추가되기 전 FETCH_HEAD
$ cat test.txt
A
B

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((e3c05aa...))
$ git checkout main                 # main으로 checkout
Previous HEAD position was e3c05aa Update test.txt
Switched to branch 'main'
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git checkout FETCH_HEAD                   # 다시 FETCH_HEAD(방금 fetch한 내용)로 checkout
Note: switching to 'FETCH_HEAD'.

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 899c073 Update test.txt

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((899c073...))         # (899c073...)는 방금 fetch한 내용!
$ cat test.txt
A
B
C

 

rweteam님의 프로필

rweteam

질문자

2021.12.30

친절하게 답변해주셔서 감사합니다.

오늘 다시 시도하니까 말씀해주신 것처럼 원격에서만 커밋한 내용은

git checkout fetch_head 를 해도 cat해보면 원격에서 수정한 내용이 보이지 않고있습니다.

감사합니다.

네 언제든 궁금한게 있으시면 질문 주세요. 좋은 질문 해주셔서 저 또한 감사드립니다 :)

1

강의에서 언급했던 pull이나 fetch를 하지 않고서는 원격저장소의 변경 사항을 로컬에서 알 수 없습니다.

(강의에서는 pull을 주로 설명했지만,)

fetch와 pull의 기본적인 차이는

pull은 원격 저장소의 내용을 로컬 저장소의 내용과 합치며 가지고 오는 것이고

fetch는 원격 저장소의 내용을 로컬 저장소의 (FETCH_HEAD 라는) 특정 브랜치로 일단 가지고 오는 것입니다.    

 

직접 보여드릴게요..!

 

가령 로컬 저장소에 test.txt를 만들고 그 안에 A 라는 문자를 저장했다고 해봅시다.

그리고 test-remote라는 원격 저장소로 아래와 같이 push 했다고 가정해볼게요. 

 

 

 

여럿이서 협업하여 원격 저장소를 다루다보면 원격 저장소의 내용이 변경될 수 있다고 강의에서 말씀드렸습니다.

그런 상황을 가정하고, 직접 원격 저장소를 아래와 같이 변경해보겠습니다.

 

 

결과적으로 원격 저장소의 test.txt 안에는

A

B

가 저장됩니다.

 

 

이 때, 로컬 저장소는 원격 저장소의 이런 변경 사항을 모릅니다

그렇기에 아래와 같이 아직 A라는 글자만을 저장하고 있는 걸 볼 수 있습니다.

 

 

 

여기서 로컬 저장소가 원격 저장소의 변경사항을 알아내는 방식에는 두 가지가 있습니다.

pull과 fetch 입니다.

pull을 하면 test.txt가 바로 

A

B

로 업데이트되지만,

fetch를 하면 아래와 같이 FETCH_HEAD라는 특정 브랜치에 변경사항이 담기게 됩니다.

 

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git fetch                                                                        # 원격 저장소 fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 628 bytes | 48.00 KiB/s, done.
From https://github.com/kangtegong/test-remote
   02f58f0..8d96ecb  main       -> origin/main

minchul@DESKTOP-9KULGUE MINGW64 /c/test (main)
$ git checkout FETCH_HEAD                                           # fetch한 내용 FETCH_HEAD에서 보기
Note: switching to 'FETCH_HEAD'.

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 8d96ecb Update test.txt

minchul@DESKTOP-9KULGUE MINGW64 /c/test ((8d96ecb...))
$ cat test.txt                       # 원격저장소에서 수정한 내용이 비로소 보인다.
A
B

 

요는, 로컬 저장소가 원격 저장소의 변경사항을 알고 싶다면 pull 혹은 fetch 과정을 반드시 거쳐야 합니다.

 

감사합니다.  

 

rweteam님의 프로필

rweteam

질문자

2021.12.30

안녕하세요 우선 친절하게 답변해주셔서 감사합니다.

답변을 보고 혹시 어제 잘못했나 싶어 똑같이 해봤지만 아직 의문이 해결되지 않았습니다..

remote에서 수정하신 후에

local 에서 'git fetch' -> 'get checkout FETCH_HEAD' -> 'cat test.txt' 를 해주셨는데요

혹시 맨 앞의 'git fetch'를 빼고 한번 수행해보시면 fetch를 하지 않더라도 fetch_head 브랜치에서 AB둘다 보이는 걸 보실 수 있으실거같아요.

 

그래서 fetch 명령어가 필요한가? 의문이 들었습니다. 안거쳐도 보여서요