Git - Merge & Conflict

jaam._.mini·2024년 1월 3일
0

💻 GIT

목록 보기
3/5
post-thumbnail

실습 환경


  • Remote Repository 생성 : merge_project

  • Local Repository에 Clone

    • git_ws 폴더에 복제
      PC@DESKTOP-7USISEH MINGW64 ~
    $ cd Documents/
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents
    $ cd git_ws/
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws
    $ git clone https://JaeminOh94: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@github.com/JaeminOh94/merge_project.git
    Cloning into 'merge_project'...
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (3/3), done.
  • merge_project로 이동

    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws
    $ ls
    HelloGit/        exam_project/  merge_project/
    branch_project/  log_project/   test_project/
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws
    $ cd merge_project/
  • 파일 추가 후 저장

    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ cat > test.txt
    my name is noma
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            test.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ git add test.txt
    warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ git status
    On branch main
    Your branch is up to date with 'origin/main'.
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   test.txt
  • Commit

      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git commit -m 'create' test.txt
      warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
      [main 2c5bd9d] create
       1 file changed, 1 insertion(+)
       create mode 100644 test.txt
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git status
      On branch main
      Your branch is ahead of 'origin/main' by 1 commit.
        (use "git push" to publish your local commits)
    
      nothing to commit, working tree clean
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ cat test.txt
      my name is noma
  • dev Branch만들고 바로 이동 (git checkout -b dev)

      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git checkout -b dev
      Switched to a new branch 'dev'
  • Branch 생성 후 파일 수정

    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ cat test.txt
    my name is noma
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ cat >> test.txt
    
    are you?
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ cat test.txt
    my name is noma
    
    are you?
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ git status
    On branch dev
    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:   test.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ git add test.txt
    warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ git status
    On branch dev
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   test.txt
    
  • commit
    의미있는 수정 작업이 끝났을 때, 마침을 알리는 작업
    git commit -m 메세지 : -m은 메세지를 남기겠다는 얘기

    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ git commit -m 'modify 1' test.txt
    warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
    [dev 582ef95] modify 1
     1 file changed, 2 insertions(+)
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ git status
    On branch dev
    nothing to commit, working tree clean
  • git log 확인

      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
      $ git log
      commit 582ef95d91447019b57b1ee73b56203e6b4ebdc9 (HEAD -> dev)
      Author: JaeminOh94 <94woals@naver.com>
      Date:   Wed Jan 3 11:08:12 2024 +0900
    
          modify 1
    
      commit 2c5bd9d20e9691f7de12c6b3af0797cc7ce08b70 (main)
      Author: JaeminOh94 <94woals@naver.com>
      Date:   Wed Jan 3 11:03:16 2024 +0900
    
          create
    
      commit 338a6026e0bf06dbe4a06f44e3c92fe3cb54348d (origin/main, origin/HEAD)
      Author: JaeminOh94 <155232890+JaeminOh94@users.noreply.github.com>
      Date:   Wed Jan 3 10:56:21 2024 +0900
    
          Initial commit

1. Merge tool 설정


  • Git Configuration 파일 열기
    Git config --global -e

  • Git Merge 설정 추가

[merge]
	tool = vscode
[mergetool "vscode"]
	cmd = "code --wait $MERGED"

2. Merge


앞전에 한 dev Branch를 main에 Merge하고자 함

git merge <branch name>
  • merge_project > main Branch로 이동

    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev)
    $ git checkout main
    Switched to branch 'main'
    Your branch is ahead of 'origin/main' by 1 commit.
      (use "git push" to publish your local commits)
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ git merge dev
    Updating 2c5bd9d..582ef95
    Fast-forward
     test.txt | 2 ++
     1 file changed, 2 insertions(+)
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ cat test.txt
    my name is noma
    are you?
  • Log (Merge 결과 확인)

    $ git log
    commit 582ef95d91447019b57b1ee73b56203e6b4ebdc9 (HEAD -> main, dev)
    Author: JaeminOh94 <94woals@naver.com>
    Date:   Wed Jan 3 11:08:12 2024 +0900
    
        modify 1
    
    commit 2c5bd9d20e9691f7de12c6b3af0797cc7ce08b70
    Author: JaeminOh94 <94woals@naver.com>
    Date:   Wed Jan 3 11:03:16 2024 +0900
    
        create
    
    commit 338a6026e0bf06dbe4a06f44e3c92fe3cb54348d (origin/main, origin/HEAD)
    Author: JaeminOh94 <155232890+JaeminOh94@users.noreply.github.com>
    Date:   Wed Jan 3 10:56:21 2024 +0900
    
        Initial commit
  • $ code .

3. Conflict 경험


(1) Conflict 발생 환경 설정

  • Main Branch에서 파일 수정

    • git branch > main 이동
    • Hello, noma 만들기 (cat > test.txt)
    • commit : git commit -m "reset" test.txt
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git branch
        dev
      * main
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ cat > test.txt
      hello, noma
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git commit -m 'reset' test.txt
      warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
      warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
      [main 00395a2] reset
       1 file changed, 1 insertion(+), 3 deletions(-)
  • Conflict Test를 위한 Branch 생성

    • Hello, noma를 카피 한다 (이동은 하지 않음)
    • Hello, noma를 복사한 다른 Branch가 만들고 싶음
    git branch dev2
  • Main Branch에서 파일 수정

    • git branch > main 이동
    • Hello, zero 만들기 (cat > test.txt)
    • commit : git commit -m "modify -zero" test.txt
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git branch
        dev
      * main
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git branch dev2
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git branch
        dev
        dev2
      * main
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git checkout dev2
      Switched to branch 'dev2'
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev2)
      $ git checkout main
      Switched to branch 'main'
      Your branch is ahead of 'origin/main' by 3 commits.
        (use "git push" to publish your local commits)
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ cat > test.txt
      hello, zero
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git commit -m 'modify -zero' test.txt
      warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
      warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
      [main 6d84bb2] modify -zero
       1 file changed, 1 insertion(+), 1 deletion(-)
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ cat test.txt
      hello, zero
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
      $ git checkout dev2
      Switched to branch 'dev2'
    
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev2)
      $ cat test.txt
      hello, noma
  • dev2 Branch에서 파일 수정

    • Checkout : 다른 브랜치로 전환하여 작업할 때 사용하는 명령어
    • Hello, base 만들기 (cat > test.txt)
    • commit : git commit -m "modify -base" test.txt
      PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev2)
    $ cat > test.txt
    hello, base
    
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev2)
    $ git commit -m "modify -base" test.txt
    warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
    warning: in the working copy of 'test.txt', LF will be replaced by CRLF the next time Git touches it
    [dev2 d74fd19] modify -base
     1 file changed, 1 insertion(+), 1 deletion(-)
  • Main 에서 dev2를 Merge

    • main으로 이동 (git checkout main)
    • git merge dev2
    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (dev2)
    $ git checkout main
    Switched to branch 'main'
    Your branch is ahead of 'origin/main' by 4 commits.
      (use "git push" to publish your local commits)
  • Conflict 발생

    PC@DESKTOP-7USISEH MINGW64 ~/Documents/git_ws/merge_project (main)
    $ git merge dev2
    Auto-merging test.txt
    CONFLICT (content): Merge conflict in test.txt
    Automatic merge failed; fix conflicts and then commit the result.

(2) Conflict 해결 방법

  • MergeTool 실행
    Conflict 발생 이후 아래와 같이 MergeTool 을 실행하면 Conflict 난 파일들이 차례로 열림

    • git mergetool (>> vscode에서 수정)
  • 맞는 코드를 선택해 수정하고 저장 (Diff 부분도 삭제)

    • 다 삭제, 정확한 데이터 1개만 남김 (+저장:ctrl+s)
  • Coflict 해제
    • git add + git commit

4. 실습


  1. Local Repository 생성
    • 위치 : git_ws 폴더 하위
    • 이름 : conflict_project
PC@DESKTOP-7USISEH MINGW64 ~
$ cd Documents/

PC@DESKTOP-7USISEH MINGW64 ~/Documents
$ cd git
bash: cd: git: No such file or directory

PC@DESKTOP-7USISEH MINGW64 ~/Documents
$ mkdir conflict_project

📌 git으로관리하겠다, 명령
PC@DESKTOP-7USISEH MINGW64 ~/Documents
$ git init
Initialized empty Git repository in C:/Users/PC/Documents/.git/

📌 Local에서 만들어서 'master'로 생성됨
PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$
  1. Merge 실습
    • Default Branch 이름 확인 : main or master
    • [master] 에서 conflict.txt 생성 후 commit : This is
    • [main] 에서 Branch 생성 후 이동 : dev
PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$ cat > conflict.txt
This is

PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$ git status
On branch master

No commits yet

PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$ git  add conflict.txt
warning: in the working copy of 'conflict.txt', LF will be replaced by CRLF the next time Git touches it

PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   "con\357\254\202ict.txt"

PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$ git commit -m 'create' conflict.txt
warning: in the working copy of 'conflict.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 2e726ab] create
 1 file changed, 1 insertion(+)
 create mode 100644 "con\357\254\202ict.txt"

PC@DESKTOP-7USISEH MINGW64 ~/Documents (master)
$ git chekout -b dev
git: 'chekout' is not a git command. See 'git --help'.

The most similar command is
        checkout
profile
비전공자의 데이터 공부법

0개의 댓글