git リベースの使用
1 つのアイデアは、ブランチをチェックアウトし、反復リベースを使用してすべてのコミットを 1 つにスカッシュし、プッシュを強制してプル リクエストを更新し、マージすることです (ただし、この作業の一部は Bob に委任できます)。
ブランチからのすべてのコミットを最初のブランチに自動的にスカッシュし、これをプル リクエストに適用するには、次のコマンドを使用できます。
$ git checkout pull-req
$ GIT_SEQUENCE_EDITOR='sed -i "2,\$s/^pick/s/g" $1' git rebase -i origin/master
$ git push --force
GIT_SEQUENCE_EDITORは、リベース コミット リストの一時エディターを設定するための Git 環境変数です。最初の行を除くすべての行の先頭にある単語pick
をs
(という意味の ) に置き換えるインライン スクリプトに設定します(これはパターン内の です)。スクリプトに渡されるコミット リストは単純なテキスト ファイルです。その後、Git はリベースを続行し、最終的なコミット メッセージを編集できるようにします。squash
2,\$
sed
また、git フックを使用すると、必要に応じてこの最終メッセージを多かれ少なかれ簡単に編集できます (たとえば、押しつぶされたコミットのメッセージ間に視覚的な区切りを追加します)。
git merge --squash の使用
を介して押しつぶすことも可能git merge --squash
です。2 つの方法の違いについては、こちらを参照してください。次のスクリプトは、merge コマンドを使用して、ブランチのコミットを単一のコミットに押しつぶします。また、ブランチのバックアップも作成します (念のため)。
MAINBRANCH="master"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# update current feature branch with master
git pull origin $MAINBRANCH
# delete existing backup
git branch -D "$CURRENT_BRANCH-backup"
# backup current feature branch and go back
git checkout -b "$CURRENT_BRANCH-backup" && git checkout -
# checkout and update master branch
git checkout $MAINBRANCH && git pull
# create new branch from master
git checkout -b "$CURRENT_BRANCH-squashed"
# set it to track the corresponding feature branch
git branch "$CURRENT_BRANCH-squashed" --set-upstream-to "$CURRENT_BRANCH"
# merge and squash the feature branch into the one created
git merge --squash $CURRENT_BRANCH
# commit the squashed changes
git commit
# force push to the corresponding feature branch
git push -f . HEAD:$CURRENT_BRANCH
# checkout the feature branch
git checkout $CURRENT_BRANCH
# delete the squashed copy
git branch -D "$CURRENT_BRANCH-squashed"