본문 바로가기
기타/Git

Git log 옵션/reset/commit 옵션

by jungmin.park 2023. 10. 27.

log -p (버전에 대해 파악)

  • log -p 옵션을 사용하면 버전간의 차이점과 과거 어느시점의 내용을 파악할 수 있다.
  • 각각의 커밋과 커밋사이 소스상의 차이점을 알 수 있다.
commit 96f4ef4bc9d69df35bdd54c1529680eba9fc61b2 (HEAD -> master)
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Dec 27 13:57:17 2022 +0900

    4

diff --git a/f2.txt b/f2.txt
index 2456b16..d170595 100644
--- a/f2.txt
+++ b/f2.txt
@@ -1 +1 @@
-source : 2
+source : 4

commit 5d6ec50fbacee1d06a4c2d7e67a7a516321ca53a
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Dec 27 11:25:23 2022 +0900
  • 버전 4에 대한 내용은 +++/b/f2.txt 가 해당되며, 버전 3에 대한 내용은 ---a/f2.txt 에 해당된다.
  • f2.txt 파일의 내용이 source : 2 에서 source : 4 수정이 되었다는 것을 의미한다.

commit 8ac12a40e451112ae8bfe4a72d81e8344fa10929
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Dec 27 11:25:08 2022 +0900

    2

diff --git a/f2.txt b/f2.txt
new file mode 100644
index 0000000..2456b16
--- /dev/null
+++ b/f2.txt
@@ -0,0 +1 @@
+source : 2

commit 981e45f57274b9f040b687a0c82739c8956e18b5
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Dec 27 11:12:27 2022 +0900
  • 버전 2에 대한 내용 +++ b/f2.txt
  • 버전 1에 대한 내용 --- /dev/null -> 버전 1에서는 해당 f2.txt 파일이 없음을 의미
  • f2.txt 파일이 추가되었으며 source : 2 의 내용은 f2.txt의 초기 내용이다.

 

log [commit 고유 주소]

  • 전체 커밋에 해당하는 메시지가 아닌 commit 고유 주소 이전의 메세지만 보이게 됨
XXXX@DESKTOP-PTQ94IS MINGW64 ~/Desktop/Documents/gitfth (master)
$ git log 8ac12a40e451112ae8bfe4a72d81e8344fa10929
commit 8ac12a40e451112ae8bfe4a72d81e8344fa10929
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Dec 27 11:25:08 2022 +0900

    2

commit 981e45f57274b9f040b687a0c82739c8956e18b5
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Dec 27 11:12:27 2022 +0900

    1

 

log diff [commit 고유 주소]...[commit 고유 주소]

  • 각각의 커밋에 해당하는 소스코드 사이에 차이점을 보여줌
  • 96f~ 와 8ac12~ 의 각각에 코드 상의 차이점을 보여줌.
$ git diff 96f4ef4bc9d69df35bdd54c1529680eba9fc61b2..8ac12a40e451112ae8bfe4a72d81e8344fa10929
diff --git a/f1.txt b/f1.txt
index e61c57f..e2eaf76 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1 +1 @@
-source : 3
+source : 1
diff --git a/f2.txt b/f2.txt
index d170595..2456b16 100644
--- a/f2.txt
+++ b/f2.txt
@@ -1 +1 @@
-source : 4
+source : 2

 

diff

  • 커밋을 하기전 문제가 있는지 리뷰를 할 수 있는 마지막 기회를 제공
  • f1.txt 텍스트를 f1.txt:5 로 내용을 수정한 뒤 바로 git diff 입력
  • f1.txt 를 git add 한 뒤 git diff를 입력하면 메시지가 출력되지 않는다.
$ git diff
warning: LF will be replaced by CRLF in f1.txt.
The file will have its original line endings in your working directory
diff --git a/f1.txt b/f1.txt
index e61c57f..e3a30dc 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1 +1 @@
-source : 3
+f1.txt : 5

 

reset [commit 고유 주소] --hard

  • HEAD가 [commit 고유 주소]를 가르키도록 한다.
  • 실행을 시키면 HEAD가 [commit 고유 주소]를 가르키고 있으며, 저장소에 변경사항도 없는 상태이다.
commit 4ed6a0e82546df905cc77363d0a1c8766718c711 (HEAD -> master)
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:48:30 2023 +0900

    5

diff --git a/f1.txt b/f1.txt
index 2456b16..7686a17 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1 +1 @@
-source : 2
+source : 5

commit 3aecb4fce0ef7220fcdcb00cf9c502c86863c8a8
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:48:11 2023 +0900

    4

diff --git a/f2.txt b/f2.txt
index e61c57f..d170595 100644
--- a/f2.txt
+++ b/f2.txt
@@ -1 +1 @@
-source : 3
+source : 4

commit 7f2bff9784afc9b233d9bb5d6ac144e833233626
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:46:59 2023 +0900

    3

diff --git a/f2.txt b/f2.txt
new file mode 100644
index 0000000..e61c57f
--- /dev/null
+++ b/f2.txt
@@ -0,0 +1 @@
+source : 3

commit 90624b36a2cacd5622cc7071e95e33067c2b376c
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:45:59 2023 +0900

    2

diff --git a/f1.txt b/f1.txt
index e2eaf76..2456b16 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1 +1 @@
-source : 1
+source : 2

commit 0f1e4fc717750c0a2e3d8951cd1008a7017529c3
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:44:47 2023 +0900

    1

diff --git a/f1.txt b/f1.txt
new file mode 100644
index 0000000..e2eaf76
--- /dev/null
+++ b/f1.txt
@@ -0,0 +1 @@
+source : 1
version 상태 내용 commit id
1 f1.txt 생성 source : 1 0f1e4fc717750c0a2e3d8951cd1008a7017529c3
2 f2.txt 수정 source : 1 -> source : 2 90624b36a2cacd5622cc7071e95e33067c2b376c
3 f2.txt 생성 source : 3 7f2bff9784afc9b233d9bb5d6ac144e833233626
4 f2.txt 수정 source : 3 -> source : 4 3aecb4fce0ef7220fcdcb00cf9c502c86863c8a8
5 f1.txt 수정 source : 2 -> source : 5 4ed6a0e82546df905cc77363d0a1c8766718c711
  • version 3으로 가려는 경우
  • git reset 7f2bff9784afc9b233d9bb5d6ac144e833233626 —hard
$ git log -p
commit 7f2bff9784afc9b233d9bb5d6ac144e833233626 (HEAD -> master)
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:46:59 2023 +0900

    3

diff --git a/f2.txt b/f2.txt
new file mode 100644
index 0000000..e61c57f
--- /dev/null
+++ b/f2.txt
@@ -0,0 +1 @@
+source : 3

commit 90624b36a2cacd5622cc7071e95e33067c2b376c
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:45:59 2023 +0900

    2

diff --git a/f1.txt b/f1.txt
index e2eaf76..2456b16 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1 +1 @@
-source : 1
+source : 2

commit 0f1e4fc717750c0a2e3d8951cd1008a7017529c3
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 09:44:47 2023 +0900

    1

diff --git a/f1.txt b/f1.txt

 

commit --help

  • commit에 대한 다양한 옵션을 확인하고 싶을 경우 사용하는 옵션

commit -a

파일의 수정이 끝나면 git add 를 이용해서 storage 로 올린 뒤 git commit 를 이용해 커밋 메시지를 작성하고 repository에 올리는 작업을 했다.
이를 한 번에 해결해주는 것이 -a 옵션
  • -all

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.

  • a의 사용 예시
더보기
$ git commit -a
[master 783f2bc] 6
 2 files changed, 2 insertions(+)

Nethru@DESKTOP-PTQ94IS MINGW64 ~/Desktop/Documents/practice_git (master)
$ git log -p
commit 783f2bc9af4f34eb8e462f1ba8d4d1320db2502d (HEAD -> master)
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 10:12:18 2023 +0900

    6

diff --git a/f1.txt b/f1.txt
index 2456b16..8fa8c8f 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1 +1,2 @@
 source : 2
+6
diff --git a/f2.txt b/f2.txt
index e61c57f..d44ba55 100644
--- a/f2.txt
+++ b/f2.txt
@@ -1 +1,2 @@
 source : 3
+7

 

       
verison 상태 내용 commit id
1 f1.txt 생성 source : 1 0f1e4fc717750c0a2e3d8951cd1008a7017529c3
2 f1.txt 수정 source : 1 → source : 2 90624b36a2cacd5622cc7071e95e33067c2b376c
3 f2.txt 생성 source : 3 7f2bff9784afc9b233d9bb5d6ac144e833233626
4 f2.txt 수정 source : 3 → source : 4 3aecb4fce0ef7220fcdcb00cf9c502c86863c8a8
5 f1.txt 수정 source : 2 → source : 5 4ed6a0e82546df905cc77363d0a1c8766718c711
6 f1.txt, f2.txt 수정 source 2 → source 2 6

——————————-
source 3 → source 3 7
783f2bc9af4f34eb8e462f1ba8d4d1320db2502d

 

commit -am

commit 까지 한 번에 해결해주는 옵션 -a 까지 살펴보았다.
이것에 추가로 -m 옵션을 추가하여 메시지를 입력하여 명령어 입력시 한 번에 commit 할 수 있다.
  • m <msg>
  • -message=<msg>

Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs. The -m option is mutually exclusive with -c, -C, and -F.

 

  • -m 사용예시
더보기
XXXXX@DESKTOP-PTQ94IS MINGW64 ~/Desktop/Documents/practice_git (master)
$ git commit -am "8"
[master 5b7ebec] 8
 1 file changed, 1 insertion(+), 1 deletion(-)


$ git log -p
commit 5b7ebeced9973def48b2a595063c1a6191fd85d8 (HEAD -> master)
Author: jeongmin <pjm9673@gmail.com>
Date:   Tue Jan 3 10:55:42 2023 +0900

    8

diff --git a/f1.txt b/f1.txt
index 8fa8c8f..a076dd3 100644
--- a/f1.txt
+++ b/f1.txt
@@ -1,2 +1,2 @@
 source : 2
-6
+8

 

       
version 상태 내용 commit id
1 f1.txt 생성 source : 1 0f1e4fc717750c0a2e3d8951cd1008a7017529c3
2 f1.txt 수정 source : 1 → source : 2 90624b36a2cacd5622cc7071e95e33067c2b376c
3 f2.txt 생성 source : 3 7f2bff9784afc9b233d9bb5d6ac144e833233626
4 f2.txt 수정 source : 3 → source : 4 3aecb4fce0ef7220fcdcb00cf9c502c86863c8a8
5 f1.txt 수정 source : 2 → source : 5 4ed6a0e82546df905cc77363d0a1c8766718c711
6 f1.txt, f2.txt 수정 source 2 →
source 2
6
——————————-
source 3 →
source 3
7
783f2bc9af4f34eb8e462f1ba8d4d1320db2502d
8 f1.txt 수정 source 2
6 →
source 2
8
5b7ebeced9973def48b2a595063c1a6191fd85d8