Git commit 취소, 히스토리 보기

정연희·2021년 9월 19일
0

Git와 Github

목록 보기
3/3

목차

  • git commit 히스토리 보기
  • git commit 취소하기

git commit 히스토리 보기

1) git log

//커맨드
$git log

//결과
commit 62fcdead08fdc117d9a637cbc5c6c4ebf450dd41 (HEAD -> yeonhee)
Author: Yeonhee_Jung <anyEmail@gmail.com>
Date:   Sun Sep 19 21:33:04 2021 +0900

    docs : changed function movieList to component MovieList	//commit 메시지

commit a543f47f6a65a769d2e45707963eeee01414ce92 (origin/yeonhee)
Author: Yeonhee_Jung <anyEmail@gmail.com>
Date:   Sat Sep 18 20:49:08 2021 +0900

    style: change margin	//commit 메시지

commit 3bc9e9ef3a58519dff20790e9cb12f5ccf889200
Author: Yeonhee_Jung <anyEmail@gmail.com>
Date:   Sat Sep 18 20:45:25 2021 +0900

    push for vercel	//commit 메시지

-> 코드 출처: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

2) git log --pretty=oneline

git log 결과를 보시면 한 커밋당 정보가 매우 많습니다. 하지만 만약 커밋 히스토리를 한줄로 요약해서 보고 싶다면 --pretty=oneline 옵션을 사용하시면 됩니다. 그럼 위 결과가 다음과 같이 변합니다.

$ git log --pretty=oneline
62fcdead08fdc117d9a637cbc5c6c4ebf450dd41 (HEAD -> yeonhee) docs : changed function movieList to component MovieList
a543f47f6a65a769d2e45707963eeee01414ce92 (origin/yeonhee) style: change margin
3bc9e9ef3a58519dff20790e9cb12f5ccf889200 push for vercel

또다른 옵션은 --oneline인데요, --pretty=oneline 처럼 커밋을 요약해서 보여주되, 위 코드처럼 매줄을 시작하는 40자 짜리 SHA-1 체그섬을 모두 보여주는 것이 아니라 몇 자만 보여주도록 하는 옵션입니다(체크섬 요약 옵션은 --abbrev-commit). 이 옵션을 사용하면 위 터미널 결과가 다음처럼 변합니다.

$git log --oneline
62fcdea (HEAD -> yeonhee) docs : changed function movieList to component MovieList
a543f47 (origin/yeonhee) style: change margin
3bc9e9e push for vercel

3) git log -p

-p 또는 -patchgit log 뒤에 쓰면 commit history와 더불어 어떤 코드 변화를 주었는지 확인할 수 있습니다.

% git log -p
commit 62fcdead08fdc117d9a637cbc5c6c4ebf450dd41 (HEAD -> name)
Author: Name
<anyEmail@gmail.com>
Date:   Sun Sep 19 21:33:04 2021 +0900

    docs : changed function movieList to component MovieList

diff --git a/src/components/ProfileCard.js b/src/components/ProfileCard.js
index d68e197..a60f2f9 100644
--- a/src/components/ProfileCard.js
+++ b/src/components/ProfileCard.js
@@ -8,16 +8,9 @@ export default function ProfileCard({person}) {
         setLikes(likes + 1)
     );
 
 //기존 코드
-    function movieList(){
-        return(
-            <>
-                {person.movies.map((movie, index) =>
-                    <li key = {index}>{movie}</li>
-                )}
-            </>
-        );
-
-    }

//바뀐 코드
+    const MovieList = person.movies.map((movie, index) =>
+        <li key = {index}>{movie}</li>
+    );

4) git log -{숫자}

git log는 모든 커밋 히스토리를 보여주지만, 만약 최근 몇 개만 보고 싶다면 다음과 같이 -2처럼 숫자를 git log에 붙이면 됩니다. 참고로, 이런 옵션은 다른 옵션들과 함께 사용할 수 있습니다. 가령, git log -p -2처럼 커맨드하면, 커밋 히스토리 최근 2개 커밋만 보여주되 코드 변화도 보여달라는 메시지가 shell에게 전달됩니다.

$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    Change version number

diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
 spec = Gem::Specification.new do |s|
     s.platform  =   Gem::Platform::RUBY
     s.name      =   "simplegit"
-    s.version   =   "0.1.0"
+    s.version   =   "0.1.1"
     s.author    =   "Scott Chacon"
     s.email     =   "schacon@gee-mail.com"
     s.summary   =   "A simple gem for using Git in Ruby code."

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    Remove unnecessary test

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index a0a60ae..47c6340 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -18,8 +18,3 @@ class SimpleGit
     end

 end
-
-if $0 == __FILE__
-  git = SimpleGit.new
-  puts git.show
-end

-> 코드 출처: https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History

git log에서 나가는 방법

많은 사람들이 몰라 불편해하는 부분은 git log를 커맨드한 후 다른 터미널 커맨드를 할 수 없다는 점입니다. git log에서 나가고 싶다면 q나 z를 입력하시면 됩니다. 저도 git log를 exit하는 방법을 몰라 터미널을 아예 닫아서 다시 시작하곤 했는데 여러분들은 이제 저처럼 힘들어하지 않으셨으면 좋겠습니다^^.



git commit 취소하기

git commit은 git reset 커맨드로 취소할 수 있지만, 취소 범위와 조건에 따라 사용해야 하는 옵션이 다릅니다. git reset 옵션은 세 가지로 범위를 나눌 수 있는데요, 그것은 바로 soft reset, mixed reset(아무 설정 안하면 이것이 default), 그리고 hard reset입니다. 이 옵션들에 대해 구체적으로 알아보기 전에 알아야 할 용어들이 있습니다.

Working Directory
: 자신이 작업하고 있는 디렉토리입니다. 자신이 위치한 로컬 컴퓨터 파일이라고 생각하시면 됩니다.
Staging Area
: Staging Area는 git commit할 때 Git이 처리할 것들이 있는 곳입니다.
Index
: 이는 바로 다음 커핏할 것들을 지칭하는 용어로, Staging Area에 있는 것입니다.

이 용어들의 의미를 염두에 두신 상태에서 이제 git reset 옵션들에 대해 알아보겠습니다.

1) soft reset: git commit만 취소하기

만약 이미 commit한 것에 더 추가적인 변화를 주고 다시 commit을 하고 싶은 경우, 즉 기존 코드 변화를 저장한 채 commit 자체만 취소하고 싶은 경우에는 soft 옵션을 사용해야 합니다. 이 옵션은 Staging Area에 있는 Index와 Working Directory에 영향을 주지 않는 상태에서 commit 자체만 취소합니다.

git reset --soft HEAD^	//가장 최신 커밋 1개 취소
git reset --soft HEAD^^ //가장 최신 커밋 2개 취소
git reset --soft HEAD~3 //가장 최신 커밋 3개 취소

이때, Head뒤의 꺽쇠들은 꺽쇠 개수만큼 커밋이 삭제하라는 의미이며, 최근 것 기준으로 삭제됩니다. 꺽쇠 대신 ~{취소 개수}도 사용할 수 있습니다.

2) mixed reset: git commit, index까지 취소하기

soft 옵션은 commit만 취소하고 index에는 영향을 주지 않는 한편, mixed는 index까지도 취소합니다(soft처럼 working directory는 변하지 않습니다). 다른 말로 add한 것까지 취소된다는 것입니다.

git reset --mixed HEAD^	//가장 최신 커밋 1개 취소
git reset HEAD^^ //아무 옵션 안쓰면 mixed로 간주

3) hard reset: git commit, index, working directory까지 다 취소하기

git reset --hard HEAD^	//가장 최신 커밋 1개 취소
git reset --hard HEAD^^ //가장 최신 커밋 2개 취소

hard는 commit, index, working directory까지 다 취소하는 옵션으로, mixed에서 한 단계 더 나아가 로컬 컴퓨터에 저장했던 코드 변화도 삭제합니다.


출처

0개의 댓글