(4)-4.GIT - 5.+Training+Set+D

[Git & Gerrit Home으로 돌아가기]

5. Training Set D

  • 0. Summary
  • 1.준비 - 원격 저장소를 준비하고, 첫번째 Working Directory를 준비
  • 2. Workdir1에서 branch 만들고 파일 생성 및 Commit
  • 3. Workdir2 master에서 파일 생성 , Commit ,Push
  • 4. Workdir1로 돌아와 branch merge하고 ,trying push, conflict
  • 5. Conflict at workdir1,  Pull , Resolve conflict, Push
  • 6. Pull at workdir2

0. Summary

  • 이번 Training Set은 실무에서 많이 나타날 수 있는 상황입니다.
  • 한 프로젝을 여러 명의 개발자가 서버에서 받아 각자 개발이 진행되는 경우, 서버에 Push할 때 Conflict이 날 수 있습니다.
  • 이 때는 Remote Repository에서 최신 내용을 fetch 받아와서 merge후 conflict을 해결해야, 서버에 Push할 수 있습니다.
  • 두 명의 개발자가 workdir1, workdir2 로 작업한다고 가정했을 때 conflict을 해결하고, 각자가 동일한 내용을 가지게 되는 상황을 가정해 보았습니다.
  • 차근 차근 따라해 보세요.
  • 전체 진행 시나리오는 다음과 같습니다.

clone 된, workdir1, branch2 에서 branch2.txt 파일을 생성 : 내용 - workdir1 branch2 file created
clone 된, workdir2, master 에서 branch2.txt 파일을 생성 : 내용 - Workdir2, master , file created -> push
workdir1으로 돌아와 branch2 merge 후 push : 컨플릭 나서 push 못함, pull 해서 컨플릭 해결해야 push 할 수 있음
workdir1에서 git pull , conflict 해결 push
workdir2로 가서, 최신 내용 pull

→ 최종 : workdir1, workdir2 모두 최신내용을 가짐.
workdir1 br2 file created
workdir2, master , file created

1.준비 - 원격 저장소를 준비하고, 첫번째 Working Directory를 준비

$ git c:/opt/repository
$ git init --bare remote.git
Initialized empty Git repository in C:/opt/repository/remote.git/

$ cd ~/gittest/
$ git clone file://c:/opt/repository/remote.git workdir1
Cloning into 'workdir1'...
warning: You appear to have cloned an empty repository.
$ cd workdir1
$ git init

$ touch init.txt : 빈 디렉토리는 관리하지 않으므로, initial commit 을 한번 해줌.
$ git add *
$ git commit -m "init"

2. Workdir1에서 branch 만들고 파일 생성 및 Commit

$ git checkout -b branch2
Switched to a new branch 'branch2'
$ vi branch2.txt
$ cat branch2.txt : workdir1, branch2 에서 branch2.txt란 파일을 생성 : 내용 - workdir1 branch2 file created
workdir1 branch2 file created
$ git add *
$ git commit -m "branch2"
[branch2 (root-commit) 30a64b7] branch2
1 file changed, 1 insertion(plus)
create mode 100644 branch2.txt

3. Workdir2 master에서 파일 생성 , Commit ,Push

$ cd ..
$ git clone file://c:/opt/repository/remote.git workdir2
$ cd workdir2
$ vi branch2.txt : master branch에서 변경후 커밋
$ cat branch2.txt : workdir2, master 에서 branch2.txt란 파일을 생성 : 내용 - Workdir2, master , file created
Workdir2, master , file created
$ git add *
$ git commit -m "master branch2.txt" : Commit
$ git push origin master : push
Counting objects: 3, done.
Writing objects: 100% (3/3), 252 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To file://c:/opt/repository/remote.git

  • [new branch] master -> master
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    nothing to commit, working tree clean


    4. Workdir1로 돌아와 branch merge하고 ,trying push, conflict

    $ cd ../workdir1
    $ git branch
  • branch2
    master
    $ git checkout master
    $ git merge branch2
    Updating 9418755..ce25c17
    Fast-forward
    branch2.txt | 1 +
    1 file changed, 1 insertion(plus)
    create mode 100644 branch2.txt
    $ git push origin master
    To file://c:/opt/repository/remote.git
    ! [rejected] master -> master (fetch first)
    error: failed to push some refs to 'file://c:/opt/repository/remote.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    5. Conflict at workdir1,  Pull , Resolve conflict, Push

    $ git fetch
    warning: no common commits
    remote: Counting objects: 3, done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From file://c:/opt/repository/remote
  • [new branch] master -> origin/master
    $ git status
    On branch master
    Your branch and 'origin/master' have diverged,
    and have 2 and 1 different commits each, respectively.
    (use "git pull" to merge the remote branch into yours)
    nothing to commit, working tree clean
    $ git pull
    fatal: refusing to merge unrelated histories
    $ git pull origin --allow-unrelated-histories
    Auto-merging branch2.txt
    CONFLICT (add/add): Merge conflict in branch2.txt
    Automatic merge failed; fix conflicts and then commit the result.
    $ vi branch2.txt : 충돌 해결
    $ cat branch2.txt : 충돌 해결 결과
    workdir1 br2 file created
    workdir2, master , file created
    $ git add branch2.txt
    $ git commit -m "master branch2.txt"
    $ git push origin master
    Counting objects: 9, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (6/6), done.
    Writing objects: 100% (9/9), 787 bytes | 0 bytes/s, done.
    Total 9 (delta 0), reused 0 (delta 0)
    To file://c:/opt/repository/remote.git
    90b8401..2f1dc1d master -> master
    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    nothing to commit, working tree clean

    6. Pull at workdir2

    $ cd ../workdir2
    $ ls
    branch2.txt
    $ cat branch2.txt
    workdir2, master , file created
    $ git pull
    remote: Counting objects: 9, done.
    remote: Compressing objects: 100% (6/6), done.
    remote: Total 9 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (9/9), done.
    From file://c:/opt/repository/remote
    90b8401..2f1dc1d master -> origin/master
    Updating 90b8401..2f1dc1d
    Fast-forward
    branch2.txt | 1 +
    init.txt | 0
    2 files changed, 1 insertion(plus)
    create mode 100644 init.txt
    $ cat branch2.txt
    workdir1 br2 file created
    workdir2, master , file created