지금까지 Git 을 이용하여 로컬의 작업내용을 원격의 repository 에 보관하여 작업하는 방법을 알아보았다.

 

그런데 만약 멀티 유저환경이라면 그래서 누군가가 내가 마지막으로 보관했던 커밋이후에 신규커밋을 추가했다면 내 로컬 커밋들에 그 신규 커밋을 어떻게 반영해야할까?

 

이런 상황에 사용할 수 있는 명령이 git fetch 이다. fetch 명령은 타겟으로 지정한 리포지토리의 변경내역을 로컬로 가지고 와서 그 내역들을 HEAD라는 특수한 branch에 담는다.

 

이 정보를 이용해서 로컬의 작업내역들과 비교할 수 있고 만약 누군가가 생성한 신규 커밋들이 로컬에서 작업한 부분과 중복되는 부분이 있다면 이에 대한 정보를 제공해준다.

 

아래 예제를 참조

 

1. 현재 log 확인

* git ll 은 git log --oneline 을 이용한 alias 이다. 이와 관련한 정보는 'Git 간단한 사용법 - alias' 참조할 것.

[~/itips_2] (master) $ git ll
2afe186 26 minutes ago I Tips Merge branch 'master' of /home/itips/repo (HEAD -> master, origin/master, origin/HEAD) I Tips
f234ec6 27 minutes ago I Tips Add second.txt file I Tips
d7addb2 29 minutes ago I Tips Add new file I Tips
9875c2a 5 days ago I Tips I will make a commit only with first.txt I Tips

현재 HEAD-> master, origin/master, origin/HEAD 모두 commit 2afe186 을 가리키고 있다.

 

2. fetch를 한후 다시 로그를 확인해 보면 아래와 같다.

[~/itips_2] (master) $ git fetch
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 7 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (7/7), done.
From /home/itips/repo
   2afe186..d7c0da0  master     -> origin/master
[~/itips_2] (master) $ git ll
2afe186 26 minutes ago I Tips Merge branch 'master' of /home/itips/repo (HEAD -> master) I Tips
f234ec6 27 minutes ago I Tips Add second.txt file I Tips
d7addb2 29 minutes ago I Tips Add new file I Tips
9875c2a 5 days ago I Tips I will make a commit only with first.txt I Tips

로컬에 있는 가장 최근 커밋인 2afe186 은 변경없이 그대로 있지만 그 커밋을 가리키고 있던 origin/master 와 origin/HEAD 가 사라지고 HEAD-> master 만 남아 있다. 

fetch 실행 후 메세지에서 '2afe186..d7c0da0 master  -> origin/master' 이부분은 로컬의 마스터 브렌치에서 리포지토리 origin 에 있는 master 브렌치 사이에 커밋들이 있음을 알려주는 것이다. 

 

3. 이제 merge 를 실행

[~/itips_2] (master) $ git merge origin/master
Updating 2afe186..d7c0da0
Fast-forward
 second.txt | 1 +
 third.txt  | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 third.txt

커밋 2afe186에서 커밋 d7c0da0 로 업데이트가 진행 되었음을 보여준다.

 

4. 다시 커밋로그를 확인

[~/itips_2] (master) $ git ll
d7c0da0 2 minutes ago I Tips Merge remote-tracking branch 'origin/master' (HEAD -> master, origin/master, origin/HEAD) I Tips
8c07aa4 25 minutes ago I Tips Add third.txt I Tips
2afe186 27 minutes ago I Tips Merge branch 'master' of /home/itips/repo I Tips
f234ec6 28 minutes ago I Tips Add second.txt file I Tips
d7addb2 30 minutes ago I Tips Add new file I Tips
9875c2a 5 days ago I Tips I will make a commit only with first.txt I Tips

커밋 2afe186 이후로 2개의 커밋이 추가되었고 HEAD-> master, origin/master, origin/HEAD 가 예전처럼 가장 최근의 커밋을 가리키고 있다. 

 

 

정리하면, fetch 가 진행되기 전까지 로컬작업 환경은 리포지토리의 상황을 알지 못하며 fetch 를 통해 repository 의 정보를 가져오며 이때 로컬파일들에 어떠한 변경도 만들지 않는다. 

 

이렇게 업데이트 된 정보를 이용해서 merge, log 등의 작업을 진행한다. 따라서, 특히 리포지토리에 push 등의 작업을 할 경우 내 로컬 작업 환경에 리포지토리의 커밋내역을 정확히 가지고 있지 않으면 정상적으로 push 가 되지 않고 에러가 난다.

 

그래서 일반적(충돌- conflict 이 일어나지 않는 경우)으로 push 할 때 작업수행은 아래와 같이 진행한다. 

 

git add . && git commit -m 'message' && git fetch origin master && git merge origin/master && git push origin master

 

 

------------------------------------------------------------------------------------------------------------------------------

 

 

+ Recent posts