Git · 코드 기록

Git 버전 비교 작업 되돌리기 임시저장 브랜치

짱코딩러 2022. 11. 14. 23:38

 

버전 비교하기

1. 최근 커밋과 현재 작업 디렉터리 비교하기(차이 출력)

a.txt에 'D'를 추가해 주었음

git diff
$ git diff
diff --git a/a.txt b/a.txt
index b1e6722..8422d40 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 A
 B
 C
+D

 

2. 최근 커밋과 스테이지에 추가된 항목 비교

git diff --cached
#또는
git diff --staged
#변경 사항을 스테이지에 추가
$ git add a.txt

#차이 출력
$ git diff --staged
diff --git a/a.txt b/a.txt
index b1e6722..8422d40 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 A
 B
 C
+D

 

3. 커밋끼리 비교

비교 커밋이 달라진 점을 출력함

git diff 기준커밋 비교커밋
#a.txt 파일 커밋
$ git commit -m "fourth commit"
[master e93f91a] fourth commit
 1 file changed, 1 insertion(+)

#커밋 해시 확인
$ git log --oneline
e93f91a (HEAD -> master) fourth commit
5c5d7b9 (tag: v1.0.0) third commit
35856e9 (tag: v0.0.1) second commit
4f5a437 first commit

#세번째 기준으로 네번째 커밋의 달라진 점
$ git diff 5c5d7b9 e93f91a
diff --git a/a.txt b/a.txt
index b1e6722..8422d40 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 A
 B
 C
+D

 

+커밋 해시 입력 귀찮으면 최신 커밋으로 구분할 수 있음(^ 또는 ~n으로)

HEAD^ 또는 HEAD~1 == 현재 브랜치의 최신 커밋에서 하나 이전 커밋

HEAD^^ 또는 HEAD~2 == 현재 브랜치의 최신 커밋에서 두 개 이전 커밋

 

4. 브랜치끼리 비교

git diff 기준브랜치 비교브랜치

#master를 기준으로 foo가 달라진 점을 확인
$ git diff master foo

 

작업 되돌리기

 

1.reset(예전 커밋으로 되돌아가기)

#커밋이 적용된 그 순간만 되돌려줌(스테이지로 추가한 순간으로 되돌아감)
git reset --soft HEAD^
#수정했던 사실까지 완전하게 되돌림(세번째 커밋으로 reset하면 네번째 커밋 정보가 사라짐)
git reset --hard HEAD^

 

2.revert(취소된 새로운 커밋 만들기)

#revert 명령
git revert HEAD

# :wq 입력 (커밋 메세지 변경해도 됨)
Revert "fourth commit"

This reverts commit e93f91a49ca9701bf9bdc4805b2a1d13d1abf3b0.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Changes to be committed:
#       modified:   a.txt
#
# Untracked files:
#       git log
#
~
~
F:/test/.git/COMMIT_EDITMSG [unix] (00:07 15/11/2022)                    1,1 All

 

작업 임시 저장

 

1. 변경 사항 임시 저장(스태시)

추적되는(tracked) 파일에만 사용가능

git stash -m ["메시지"] #또는
git stash --message ["메시지"]

#C삭제한거 저장
$ git stash -m "delete C"
Saved working directory and index state On master: delete C

#D추가한거 저장
$ git stash -m "add D"
Saved working directory and index state On master: add D

 

2. 임시 저장된 작업 내역 조회하기

최근에 저장한 작업일수록 작은 숫자가 붙어서 출력됨

git stash list

$ git stash list
stash@{0}: On master: add D
stash@{1}: On master: delete C

 

3. 임시 저장된 작업 적용하기

실제 파일을 확인해보면 적용되어있는 것을 확인할 수 있다.

git stash apply 스태시  #스태시 default stash@{0}

$ git stash apply stash@{0}
On branch master
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:   a.txt

 

4. 임시 저장된 작업 삭제하기

git stash drop 스태시

 

브랜치 관리

 

1. 브랜치 생성

git branch 브랜치명

$ git branch foo

 

2. 현재 브랜치 목록 확인

*표시 : 작업중인 브랜치

git branch

$ git branch
  foo
* master

 

3. 체크아웃

git checkout 브랜치명

$ git checkout foo
Switched to branch 'foo'

 

4. 병합

git merge 브랜치명
#먼저 master로 체크아웃
$ git checkout master
Switched to branch 'master'
#병합
$ git merge foo
Updating 73036d9..606b591
Fast-forward
 foo_d.txt | 1 +
 foo_e.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 foo_d.txt
 create mode 100644 foo_e.txt

 

5. 충돌 해결

  1) 병합 시에 충돌이 있으면

$ git merge foo
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.

  2) 파일 내용이 변경된 것을 확인할 수 있다.

     둘중에 변경할 내용만 골라서 남기고 저장해줌

  3) 커밋

#스테이지에 올리기
$ git add a.txt
#커밋
$ git commit
#커밋 메세지 저장 후 Vim 닫기
$:wq

 

6. 브랜치 삭제

삭제할 브랜치가 아닌 다른 브랜치에 체크아웃 상태여야 함.

git branch -d 브랜치명
git branch --detele 브랜치명

 

7. 브랜치 재배치

git rebase master
#재배치하려는 브랜치로 체크아웃
$ git checkout bar
Switched to branch 'bar'
#재배치
$ git rebase master
Successfully rebased and updated refs/heads/bar.
#그래프확인
$ git log --oneline --branches --graph
* c8fe824 (HEAD -> bar) bar second
* 2d3bc9e bar first
* 4d9da68 (master) tenth commit
* b6509a9 ninth commit
*   850b0a8 Merge branch 'foo'
|\
| * e1893c4 (foo) foo commit
* | 7dcd427 master commit
|/
* 606b591 fifth commit
* 8f28e93 fourth commit
* 73036d9 third commit
* 5c31ea9 second commit
* 1bf0a8e first commit