Log, Diff, Merge, Conflict

InSung-Na·2023년 2월 28일
0

Part 06. Git

목록 보기
3/4
post-thumbnail

해당 글은 제로베이스데이터스쿨 학습자료를 참고하여 작성되었습니다

📌Git Log

  • 변경내역들이 모두 기록되고, 이를 이용하여 해당 버전으로 이동할 수 있음

Git graph

실습환경 구축

깃허브 -> log_project 생성(README파일 포함) -> git clone -> 폴더로 들어가서 VScode 실행 -> VScode 확장팩 -> git graph 설치 -> git graph로 Log 확인

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ git clone https://InSung-Na:TOKEN@github.com/InSung-Na/log_project.git
Cloning into 'log_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.

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws
$ cd log_project/

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (master)
$ code .


참고 : cat 명령어

  • cat file : 파일 내용 확인
  • cat > file : 내용 덮어쓰기
  • cat >> file : 내용 이어쓰기
  • ctrl D : 저장

파일 만들고 수정하기

  • hello.py를 만들고 add, commit 후 hello.py를 수정하고 commit한 다음 Log 확인
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ cat > hello.py
print("hello, world")

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ cat hello.py
print("hello, world")

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ git add hello.py
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ git commit -m "create" hello.py
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it
[main 389d3d4] create
 1 file changed, 1 insertion(+)
 create mode 100644 hello.py

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ cat > hello.py
print("Hello, cat")

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ git commit -m "modify 1" hello.py
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it
[main ffcfb54] modify 1
 1 file changed, 1 insertion(+), 1 deletion(-)


새로운 Branch에서 파일 수정하고 커밋하기

  • Branch:dev를 만들고 hello.py를 수정하고 commit한 다음 Log 확인
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ git checkout -b dev
Switched to a new branch 'dev'

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (dev)
$ cat hello.py
print("Hello, cat")

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (dev)
$ cat > hello.py
print("Hello, dog")

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (dev)
$ git commit -m "modify 2" hello.py
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'hello.py', LF will be replaced by CRLF the next time Git touches it
[dev a0af043] modify 2
 1 file changed, 1 insertion(+), 1 deletion(-)


bash에서 log 확인

  • 현재 접속 중인 branch에서 log를 확인할 수 있음
    git log
  • main과 dev에서 log 확인
HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (dev)
$ git checkout main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

HPcom@DESKTOP-TJ3L1B9 MINGW64 ~/Documents/git_ws/log_project (main)
$ git log
commit ffcfb54210d7ebe636fa6174066fed994e5ed05a (HEAD -> main)
Author: InSung-Na <lht98323@gmail.com>
Date:   Wed Feb 22 10:09:50 2023 +0900

    modify 1

commit 389d3d45dd8577212c7cb6345bd6d11b1ec3eb33
Author: InSung-Na <lht98323@gmail.com>
Date:   Wed Feb 22 10:08:03 2023 +0900

    create

commit e4224862d9adcaec81acd6a4cdc9030ac5b7fac6 (origin/master, origin/main, origin/HEAD)
Author: InSung-Na <118172599+InSung-Na@users.noreply.github.com>
Date:   Wed Feb 22 09:44:44 2023 +0900

    Initial commit

참고사항 : editor 변경

git config --global core.eidtor "code"

참고사항 : diff editor 설정

git config --global -e 입력 후 생성되는 창에 아래 내용 입력

[diff]
	tool = vscode
[difftool "vscode"]
	cmd = "code --wait --diff &LOCAL $REMOTE"

📌Git Diff

  • branch간의 차이를 알 수 있음

Local Branch간 비교

  • git diff branch1 branch2 : bash에서 확인
  • git difftool branch1 branch2 : 설정한 툴에서 확인

Local commit간 비교

  • git diff commithash1 commithash2
  • git difftool commithash1 commithash2

commithash는 git log시 commit 뒤의 문자열


마지막 commit과 이전 commit 비교

  • git diff head head^
  • git difftool head head^

마지막 commit과 현재 수정사항 비교

  • git diff head
  • git difftool head
HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/log_project (main)
$ git diff head head^
diff --git a/hello.py b/hello.py
index c988194..9f69d32 100644
--- a/hello.py
+++ b/hello.py
@@ -1 +1 @@
-print("Hello, cat")
+print("hello, world")

Local과 Remote Branch 비교

  • git difftool branch1 origin/branch2
HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/diff_project (main)
$ git diff main origin/main
diff --git a/test.txt b/test.txt
index f239f1c..a8e0267 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-my name is zero.
+my name is noma.
  • Git Graph를 통해 모든 변경내역을 알 수 있고, 클리가면 상세내용도 알 수 있다



참고사항 : merge editor 설정

git config --global -e 입력 후 생성되는 창에 아래 내용 입력

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

📌Git Merge & Conflict

  • Merge : 현재 위치한 branch를 다른 branch을 병합

  • Merge Conflict : Merge, Push와 Pull 과정에도 충돌 발생가능

실습환경 구축

merge_project를 만들고 git clone
dev branch 생성
(main) cat > test.txt : my name is noma
(dev) cat >> test.txt : are you?
test.txt commit하기


병합하기

  • main 으로 이동하고 git merge dev 입력
HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/merge_project (dev)
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/merge_project (main)
$ git branch
  dev
* main

HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/merge_project (main)
$ git merge dev
Updating ad3d2d8..85de5c3
Fast-forward
 test.txt | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 test.txt

HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/merge_project (main)
$ git log
commit 85de5c3d1be7e55d3e2d752558b35cf139616271 (HEAD -> main, dev)
Author: InSung-Na <lht98323@gmail.com>
Date:   Wed Feb 22 12:48:58 2023 +0900

    modify 1

commit ad3d2d8a5d441fba0e16b7b79dd04310ec096c33 (origin/main, origin/HEAD)
Author: InSung-Na <118172599+InSung-Na@users.noreply.github.com>
Date:   Wed Feb 22 12:44:34 2023 +0900

    Initial commit

충돌확인

  • main에서 cat > test.txt : hello, zero.
  • dev2를 만들고 cat > test.txt : hello, base.
  • main에서 git merge dev2 입력시 충돌
HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/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.

충돌해결

  • git mergetool 입력시 vscode 열림
  • 여기서 원하는 부분만 남기고 제거 -> 파일수정완료
  • git add file + git commit 입력

HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/merge_project (main|MERGING)
$ git add test.txt

HPcom@DESKTOP-TJ3L1B9 MINGW64 /C/Users/HPcom/Documents/git_ws/merge_project (main|MERGING)
$ git commit
[main 7b3c854] Merge branch 'dev2'
  • git commit 입력시 충돌 내용 보여줌

0개의 댓글