Git 4

BlackLabel·2023년 11월 5일
0

Git 기초

목록 보기
4/6

Log and Diff

  • Log 명령어 : 버전 관리 이력을 조회
  • 버전 간에 Diff를 보는 방법 배우기

Git Graph

  • 특정 버전에서 브랜치를 통해 가지가 뻗어나가거나 버전이 높아지거나 낮아지는 것을 텍스트가 아니라 그래프로 확인할 수 있는 기능

![](https://velog.vel

실습환경 만들기

  • Local Repository 에 Clone
    git_ws 폴더에 복제 (Token 잊지않기)

  • 파일 추가 후 저장: Control + D 키로 입력한 내용 저장

cat

  • cat hell.py : hell.py 의 내용을 보여달라
  • cat > hell.py : hell.py이 존재한다면 내용을 덮어쓰고, 없다면 만들고 내용을 써라
  • cat >> hell.py : 꺽쇠 2번이면 붙여쓴다
  • 내용 마지막엔 Ctrl + d 누르면 저장이 됨

  • commit 하나 만듦(위 그림에서 파란줄이 생김)
  • 아래에서 commit 을 하나 더 만들어보자
git_ws % cd log_project
log_project % cat > hello.py
print('hello, world')
log_project % git add hello.py
log_project % git commit -m 'create' hello.py
[main 01c9205] create
1 file changed, 1 insertion(+)
create mode 100644 hello.py
  • 파일 수정 후 저장

  • commit 하나 더 만듦(위 그림에서 파란줄이 하나 더 생김)
log_project % cat > hello.py
print('hello, cat')
log_project % git commit -m "modify 1" hello.py
[main 23cd33c] modify 1
  1 file changed, 1 insertion(+), 1 deletion(-)
  • Branch 생성 후 이동

  • Branch 생성 후 이동

Git Log

  • Branch 별 변경이력을 볼 수 있음
  • 현재 위치한 Branch의 변경 이력을 보여줌
  1. 처음 만들때 hello, world 로 만들고 commit
  2. main에서 commit을 한 상태에서 내용을 hello, cat 으로 수정함
  3. dev 라는 Branch를 만든 다음에 hello, dog 으로 수정함
  • 요약하자면 main은 modify 1 까지의 이력이 있어야 하고, dev는 modify 2 까지의 이력이 있어야 한다.

  • git log

실습 1

  • main branch 로 이동 (나는 master로 이동)
log_project % 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)
  • main branch 로그 확인
log_project % git log 
#
# main 에서 git log 했기 떄문에 아래는 main의 log 
#
# Initial commit > create > modify 1 아래부터 위로 순서대로 나옴
# 
# commit 다음에 오는건 버전넘버
# Date, Author, commit 버전넘버 등이 나옴
# 
commit  23cd33c423fdbf9759c1a0923a0a2bc51beb2fd2 (HEAD -> main)
Author: zerobasegit <zerobase.git@gmail.com>
Date: Sun Nov 7 20:35:05 2021 +0900

  modify 1

commit 01c920522ae2dccc7205a6d31a0333854816bc66
Author: zerobasegit <zerobase.git@gmail.com>
Date: Sun Nov 7 20:31:22 2021 +0900

  create

commit ee811c4ec1f2da8d345ae59f440b400e382f968b (origin/main, origin/HEAD)
Author: zerobasegit <93780130+zerobasegit@users.noreply.github.com>
Date: Sun Nov 7 20:24:55 2021 +0900

  Initial commit

실습 2

  • dev branch 로 이동
log_project % git checkout dev
Switched to branch 'dev'
  • dev branch 로그 확인
# 위 main 의 log 이력 + modify 2 까지 나온다.
#
log_project % git log
commit 7347c01ea5fef07a249a1657bf7900de78a6b7d6 (HEAD -> dev)
Author: zerobasegit <zerobase.git@gmail.com>
Date: Sun Nov 7 20:39:42 2021 +0900

  modify 2

commit 23cd33c423fdbf9759c1a0923a0a2bc51beb2fd2 (main)
Author: zerobasegit <zerobase.git@gmail.com>
Date: Sun Nov 7 20:35:05 2021 +0900

  modify 1

commit 01c920522ae2dccc7205a6d31a0333854816bc66
Author: zerobasegit <zerobase.git@gmail.com>
Date: Sun Nov 7 20:31:22 2021 +0900

  create

commit ee811c4ec1f2da8d345ae59f440b400e382f968b (origin/main, origin/HEAD)
Author: zerobasegit <93780130+zerobasegit@users.noreply.github.com>
Date: Sun Nov 7 20:24:55 2021 +0900

  Initial commit

Git Editor 설정

  • git config --global core.editor

  • 기본 에디더가 vim 이다

  • Git Editor 설정
    --wait 옵션은 git command로 인해서 vscode를 실행할 때 그 command가 대기 상태로 있다가 vscode를 다 사용하고 닫는 순간에 완료가 된다

git config --global core.editor <editorname> --wait
  • vscode로 에티더 변경(code 가 vscode 임)

Git Diff Tool 설정

  • Git Configuration 파일 열기

  • git config --global -e

  • Git Diff 설정 추가
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = "code --wait --diff $LOCAL $REMOTE"

-> 복붙해서 넣고 Ctrl + s

Git Diff

  • Local Branch 간 비교(최신 버전의 차이를 보여줌)

-> 현재 dev Branch 에는 'hello, dog', master Branch 에는 'hello, cat'

git diff <branch1> <branch2>

  • Git Diff - Local Branch 간 비교 실습

  • Git Diff - Local Branch 간 비교 실습 2

  • Git Diff - Commit 간 비교
git diff <commithash> <commithash>
  • Git Diff - Commit 간 비교 실습
    Commit Hashcode 확인 (commit 뒤의 문자열)

-> modify 1 의 Hashcode 76721367f4a18e51dd94b1cd.....
commit의 Hashcode 78e9c16cdf2e186dff3d2a3f7c15...

  • create 과 modify 1 비교

  • Git Diff - 마지막 Commit 과 이전 Commit 비교
# ^가 이전이란 뜻
git diff HEAD HEAD^
git difftool HEAD HEAD^

  • Git Diff - 마지막 Commit 과 이전 Commit 비교 실습

  • Git Diff - 마지막 Commit 과 현재 수정사항 확인

  • git diff HEAD

  • Git Diff - 마지막 Commit 과 이전 Commit 비교 실습

  • Git Diff - Local and Remote 간 비교
git diff <branch> origin/<branch2>
  • Git Diff - 마지막 Commit 과 이전 Commit 비교 실습

일단 현재 Main Branch 상태를 Remote Repository 로 Push

  • 아까 수정한 파일을 Commit (Local Repository 의 Main Branch 에만 반영됨)
log_project % git commit -m "modify 3" hello.py
[main a9f87e0] modify 3
  1 file changed, 1 insertion(+), 1 deletion(-)
  • Local Repository 와 Remote Branch 비교

  • Git Graph 확인
    현재 폴더를 VSCode 로 열기 위해 아래와 같은 명령어 입력

문제

1. GitHub 에서 Remote Repository 생성

• 이름 : diff_project
• 옵션 : README.md

2. Local 에 Clone

• 위치 : git_ws 폴더

3. Local Repository 에서 파일 생성 후 Push

  1. 파일 생성

• text.txt
• 파일 내용 : my name is noma.

-> diff_project 이동 후 작성, 마지막에 Ctrl + d 두번해서 저장하고 빠져나옴

  1. push

(1) git add text.txt
(2) git status
(3) git commit -m 'create text.txt' text.txt
(4) git status
(5) git push origin main 여기서 깃허브에 생성됨

4. Main Branch 에서 파일 수정 후 마지막 commit 한 내용과 비교

• 파일 수정 : my name is zero.

  1. 파일 수정

(1) ls
(2) cat text.txt 내용 확인
my name is noma.
(3) cat > text.txt
my name is zero. 내용수정
(Ctrl + d 두번 해서 저장)
(4) cat text.txt 수정한 내용 확인
my name is zero.

  1. 마지막 commit 한 내용과 비교

5. Main Branch 에서 수정한 내용을 commit 한 뒤 Remote Server 와 비교(push 전)

  1. Main Branch 에서 수정한 내용을 commit
  • git commit -m 'modify name -zero' text.txt

  • Remote Server 와 비교(push 전)

  • git difftool main origin/main

-> local은 my name is zero. / remote는 my name is noma.

6. Dev Branch 생성 후 이동, 파일을 수정한 뒤 Master Branch 와 비교

  • Branch간 비교를 하려는 것

    • Branch 이름 : dev
    • 파일 수정 : my name is base. (commit)

  1. Dev Branch 생성 후 이동
  • git checkout -b dev

  1. 파일을 수정한 뒤 Master Branch 와 비교

7. Dev Branch 에서 두번째 commit 과 마지막 commit 비교

• 두번째 Commit Message : create test.txt
• 마지막 Commit Message : modify name - base

<아래에서부터 위로 순서>
3. 마지막 Commit Message : modify name - base
2. 두번째 Commit Message : create test.txt
1. 두번째, 마지막의 commit 값을 복사해줌

  • git difftool 두번째 commit 값 마지막 commit 값

8. Git Grpah 확인

• VSCode 에서 diff_project 의 Git Graph 를 Branch 별로 확인

  • diff_project 폴더에서 code . 해서 vscode 실행함

profile
+database

0개의 댓글

관련 채용 정보