PR(Pull Request) merge

PR(Pull Request) merge

보통 upstream의 repository를 fork한 후, origin에서 feature branch를 만들어 작업한 후 작업이 완료되면 upstream branch에 반영하기 위해 pull request를 요청해 merge 합니다. (upstream repository에 push 권한이 있는 사람이 merge를 완료할 수 있습니다.)

merge conflict가 없다면 pull request를 GitHub에서 merge 할 수 있습니다. 만약, pull request를 merge 할 때, conflict가 있다거나 merge를 하기전에 테스트를 하고 싶다면 command line을 이용해 local에서 pull request를 check out하고 merge할 수 있습니다.

만약, upstream branch에 merge를 원하지 않는다면 pull request를 merge 하지 않고 종료(close)할 수도 있습니다.

pull request를 merge 할 때는 (1)Feature branch의 모든 커밋을 유지하거나 (2)모든 커밋을 단일 커밋으로 squash하거나 (3)각각의 커밋을 rebase해서 pull request를 merge 할 수 있습니다. 3가지 방법에 대해서 알아보겠습니다.

Merging a pull request on GitHub

GitHub repository에서 사용할 수 있는 merge option은 다음과 같습니다.

  • Create a merge commit
  • Squash and merge
  • Rebase and merge

Create a merge commit

All commits from this branch will be added to the base branch via a merge commit.

default 옵션인 Create a merge commit 은 feature branch의 모든 커밋을 base branch에 merge 커밋으로 추가합니다. 해당 pull request는 —no-ff 옵션을 사용해 merge됩니다.

—no-ff

fast-forward(ff)가 가능하지만 fast-forward를 하지 않고, merge 커밋을 생성합니다.

Squash and merge

The 4 commits from this branch will be combined into one commit in the base branch.

Squash and merge 옵션을 선택하면, pull request의 커밋들이 하나의 커밋으로 squash(압축)됩니다. 즉, topic branch에서 contributor가 작업한 모든 커밋은 하나의 커밋으로 결합되어 base branch로 merge됩니다. 해당 pull request는 –ff 옵션을 사용해 merge합니다.

—ff

fast-forward(ff)로 merge가 된다면, 새로운 merge 커밋을 만들지 않습니다.

Squash and merge는 repository에서 git history를 조금 더 간소화되게 만들 수 있습니다. (Create a merge commit 옵션 과 비교해보면 새로운 merge 커밋이 없습니다.)

Rebase and merge

The 4 commits from this branch will be released and added to the base branch.

Rebase and merge 옵션을 선택하면, pull request의 커밋들을 하나의 커밋으로 병합하지 않고 개별적으로 base branch에 추가됩니다. 해당 pull request는 –ff 옵션을 사용해 merge합니다.

GitHub에서의 Rebase and merge는 git의 rebase와는 조금 다릅니다. GitHub의 Rebase and merge는 항상 committer의 정보를 업데이트하고 새로운 commit SHA를 생성하지만, git의 rebase는 조상 commit 후에 rebase를 할 경우 committer의 정보를 변경하지 않습니다.

만약, conflict로 인해 GitHub에서 자동적으로 Rebase and merge를 할 수 없다면, local에서 직접 command line을 이용해서 rebase를 하고 merge conflict를 해결해야 합니다. 그 후 pull request의 topic branch에 Force-push를 하면됩니다.

참고