취미/프로그래밍

[gitHub] gitHub commit 텍스트 고치기

D.Dic. 2018. 6. 30. 12:37
를 참고해 만들었습니다.

 

git commit

git은 commit을 통해 어떤 변화 또는 내용을 남깁니다. 하지만 새로 남기는 건 쉬운데 이전에 남긴 commit 내용을 바꾸는 방법은 쉽지가 않네요. 4가지 경우로 나누어 각각 하는 법을 알아보겠습니다.


push하지 않은 가장 최근의 commit을 고칠 때

이 경우가 가장 간단합니다. 

ㅣ커맨드라인

$git commit --amend
$git push origin master

위와 같이 입력하면 내가 썼던 commit 텍스트가 에디터 위에 보일 겁니다. 마음대로 고치고 저장하시면 됩니다. 그리고 push까지 마무리.


push한 가장 최근의 commit을 고칠 때

이 경우도 어렵진 않습니다. 수정 자체는 push하지 않았을 때와 같지만 push할 때 변경사항이 있습니다.

ㅣ커맨드라인

$git commit --amend
$git push origin master --force

이렇게 push를 강제로 해주셔야 합니다.


push하지 않은 이전의 commit을 고칠 때

여기서부턴 조금 어렵습니다. 먼저 가장 최근걸 1번이라고 할때 거기서부터 몇번째 commit인지를 확인합니다. 3번째 commit이라고 가정하겠습니다.

ㅣ커맨드라인

$git rebase -i HEAD~3

저렇게 입력하면 아래처럼 텍스트가 나올 겁니다.

pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

아래로 갈수록 최근이고 제가 고치고자 하는 commit은 3번째이므로 가장 위의 있는 commit이 제가 고칠 commit입니다. 여기서 제일 앞에 보이는 pickreword로 고칩니다. 그리고 이 파일을 저장합니다.


commit의 텍스트만 담긴 에디터가 새로 나타날 겁니다. 여기서 그 텍스트만 고치고 다시 저장. 그리고 아래와 같이 마무리를 해주면 됩니다.

ㅣ커맨드라인

$git commit --amend
$git rebase --continue
$git push origin master

push한 이전의 commit을 고칠 때

앞서와 과정은 똑같은데 마지막의 push만 조금 달라집니다.

ㅣ커맨드라인

$git push origin master --force

이렇게 하면 마무리됩니다.