git

[Git] 커밋 메시지 합치기

김홍중 2021. 5. 13. 23:05

Pull request를 하고 커밋 메시지가 많은 경우 merge하기전에 합치는데

가끔 잊는 부분이 있어서 반복된 실수를 잡고자 정리합니다.

 

만약 3개의 커밋 메시지를 합친다고 한다면, 다음을 입력합니다.

git rebase -i HEAD~3

 

그러면 다음과 같이 나올것입니다.

pick 242cwe2 commit1
pick sdf21as commit2
pick 8335sd3 commit3

 

commit1에 commit2와 commit3을 합칩니다.

그러기 위해서 squash를 의미하는 s를 다음과 같이 작성하고 저장(:wq)합니다.

pick 242cwe2 commit1
s sdf21as commit2
s 8335sd3 commit3

 

이후에 다음과 같이 뜨는데 커밋을 하나로 다시 작성할 수 있습니다.

# This is a combination of 3 commits.
# This is the 1st commit message:

commit1

첫번째 커밋입니다.

# This is the commit message #2:

commit2

두번째 커밋입니다.

# This is the commit message #3:

commit3

세번째 커밋입니다.

 

하나의 커밋을 작성합니다.

# This is a combination of 3 commits.
# This is the 1st commit message:

commit1

첫번째 커밋만 남깁니다.

 

이를 push하려고 하고하면 아래와 같이 error가 날 것입니다.

git push origin 브랜치명
$ git push origin 브랜치명
To https://github.com/username/repositoryname
 ! [rejected]        브랜치명 -> 브랜치명 (non-fast-forward)
error: failed to push some refs to 'https://github.com/username/repositoryname'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

 

강제적으로 push하는것을 주의하는 편이지만 원격저장소와 커밋 상태가 달라서 뜨는 오류이므로 어쩔 수 없이 강제로 push합니다.

git push -f origin 브랜치명