[제로베이스 데이터 취업 스쿨] 9기 11주차 - Git (6): Log and Diff

Inhee Kim·2023년 1월 13일
0
post-thumbnail

Log and Diff

1. Git Graph

Installing Git Graph on VSCode
VSCode의 Git Extension에서 'git graph'로 검색하여 설치

Open HelloGit
exam_project폴더를 열어서 Graph 확인 (Branch 별도로 확인 가능)

2. 실습환경 만들기

Remote Repository 생성
log_project 생성

Remote Repository 주소 복사

Local Repository에 Clone
git_ws 폴더에 복제

cd Documents/git_ws
git clone https://<username>:<token>@github.com/<repository>.git

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

cd log_project
cat > hello.py
print('hello, world') + ctrl+D
cat hello.py

cat > hello.py
print('hello, cat') + ctrl+D
cat hello.py

위 코드를 실행하면, hello, worldhello, cat으로 수정된 것을 확인 할 수 있다.

만약, 꺽쇠(>)를 두 번 입력하게 되면, 입력한 내용이 덮어씌워지는 것이 아닌, 추가가 된다.

cat >> hello.py
print('hello, world') + ctrl + D
cat hello.py

다시, 실습을 위해 위 내용을 hello, world로 덮어씌워주고, 이후 실습을 진행한다.

cat > hello.py
print('hello, world') + ctrl + D
cat hello.py

commit 진행.

git status
git add hello.py
git status

git commit -m 'create' hello.py

파일 수정 후 저장

cat > hello.py
print('hello, cat') + ctrl + D
git commit -m "modify 1" hello.py

Branch 생성 후 이동

git checkout -b dev
git branch

Branch 생성 후 이동

cat > hello.py
print('hello, dog') + ctrl + D
git commit -m "modify 2" hello.py

3. Git Log

Git Log
Branch 별 변경이력을 볼 수 있음

git log

Git Log 실습 1

  • main branch 로 이동
git checkout main

  • main branch 로그 확인
git log

Git Log 실습 2

  • dev branch 로 이동
git checkout dev

  • dev branch 로그 확인
git log

4. Git Editor 설정

Git Editor 설정
--wait 옵션은 command line으로 VSCode를 실행시켰을 경우, VSCode 인스턴스를 닫을 때까지 command를 대기

git config --global core.editor <editorname> --wait

실습 1

git config --global core.editor "code --wait"
git config --global core.editor

5. Git Diff Tool 설정

Git Configuration 파일 열기
위의 실습을 통해 VSCode로 열리게 됨.

git config --global -e

Git Diff 설정 추가
VSCode에서 아래 내용을 추가 작성 + 저장 + VSCode 종료

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

6. Git Diff

Local Branch 간 비교

# 문법
git diff <branch1> <branch2>

실습 1

git diff main dev

실습 2

git difftool main dev

위 코드를 실행하고 Launch 'vscode' [Y/n]?에서 y를 입력하면 VSCode에서 실행되어 main과 dev의 차이를 확인할 수 있다.

Commit 간 비교

# 문법
git diff <commithash> <commithash>

실습 1

  • Commit Hashcode 확인 (commit 뒤의 문자열)
git checkout main		# main으로 이동 후
git log					# log 보기

  • create와 modify 1 비교
git difftool 8a6b0e3b25d9947eed51bd950b0e13c4ba070638 1d79c3b29efefdcdef982cd4083efe3ea255c7b7

마지막 Commit과 이전 Commit 비교

git diff HEAD HEAD^

실습 1

git checkout dev
git diff HEAD HEAD^

마지막 Commit과 현재 수정사항 확인

git diff HEAD

실습 1

cat > hello.py
print('hello, pig') + ctrl + D
git diff HEAD

Local and Remote 간 비교

git diff <branch> origin/<branch2>

실습 1

git push origin main

  • 수정 후 Commit (Local Repository 의 Main Branch 에만 반영됨)
cat hello.py

cat > hello.py
print('hello, pig')

git commit -m "modify 3" hello.py

  • Local Repository와 Remote Branch 비교
git difftool main origin/main

Git Graph 확인

문제풀이

1. GitHub에서 Remote Repository 생성

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

2. Local에 Clone

  • 위치 : git_ws 폴더
cd Documents/git_ws
git clone https://<username>:<token>@github.com/<repository>.git

ls

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

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

cat > test.txt
my name is noma. + ctrl + D

git status

git add test.txt
git status

git commit -m 'create test.txt' test.txt
git status

git push origin main

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

  • 파일 수정 : my name is zero.
cat test.txt

cat > test.txt + ctrl + D
git diff HEAD

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

git commit -m 'modify name -zero' test.txt
git status

git diff main origin/main

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

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

cat > test.txt
my name is base. + ctrl + D

git status

git commit -m 'modify name -base' test.txt
git status

git diff main dev

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

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

git diff e0422e7d50ac4a66fdb5737f164cf38ca45f49f4 5884927e9c6cd6805f88e5ffea0cf57311f0f165

8. Git Grpah 확인

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

profile
Date Scientist & Data Analyst

0개의 댓글