Git에서 파일의 일부분만 커밋하기


여러 명이서 개발을 할 경우, Git을 통한 코드의 내역 추적은 정말 중요한 일이다. 이에 따라 많은 개발자들은 모든 커밋 로그를 더 상세하고 명시적으로 나누게 위해 노력한다. 하지만 우리는 여러가지 문제를 해결해야 하며, 추가해야 하는 여러가지 기능들이 있기에 종종 하나의 커밋을 보내기 전에 너무 많은 파일 내 수정을 만들곤 한다.

우리는 다음과 같은 명령을 통해 한 파일 내에서 커밋을 hunk라고 부르는 Git이 생각하는 단위로 쪼개어 커밋할 수 있다.

git add --patch <filename>

해당 명령을 입력하면 Git은 파일 내에서 내가 수정한 사항들을 여러 개의 hunk로 나누어 어떤 hunk를 커밋할 것인지 질의한다.

Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?

매 hunk에 대한 질의마다 yes 혹은 no에 해당하는 이니셜을 입력하면 된다. 각 이니셜은 다음과 같은 뜻을 갖고 있다.

  • y stage this hunk for the next commit
  • n do not stage this hunk for the next commit
  • q quit; do not stage this hunk or any of the remaining hunks
  • a stage this hunk and all later hunks in the file
  • d do not stage this hunk or any of the later hunks in the file
  • g select a hunk to go to
  • / search for a hunk matching the given regex
  • j leave this hunk undecided, see next undecided hunk
  • J leave this hunk undecided, see next hunk
  • k leave this hunk undecided, see previous undecided hunk
  • K leave this hunk undecided, see previous hunk
  • s split the current hunk into smaller hunks
  • e manually edit the current hunk
  • ? print hunk help

참고자료

Stackoverflow - Commit only part of a file in Git