【问题标题】:Avoiding merge commits in pull requests in git避免在 git 的拉取请求中合并提交
【发布时间】:2016-03-02 05:09:39
【问题描述】:

所以,我想创建一个仅包含对 file1、file2 和 file3 的更改(即功能更新)的拉取请求。

我使用的工作流程:

git clone https://github.com/eghm/project.git project-eghm-clean
cd project-eghm-clean
git remote add upstream https://github.com/forkeduser/project.git
git fetch upstream
git merge upstream/master
git push
git branch featurebranch
git checkout featurebranch
#edit files: file1, file2, file3
git add file1
git add file2
git add file3
git commit -m 'updates for feature'
git push --set-upstream origin featurebranch

然后在 github 上,我转到我的分叉存储库,选择 Branch: featurebranch,然后单击 Pull request。该请求包括我通过同步我的 fork 进行的合并:

以后如何避免这种情况?

【问题讨论】:

    标签: git github


    【解决方案1】:

    您应该只针对您发出拉取请求的分支git rebase。例如:

    git rebase origin/master
    

    因为这会改变你的分支的历史,你最好在一个新的分支中做这个变基。所以:

    git checkout -b new_rebased_branch
    git rebase origin/master
    

    为了将来参考,如果您不想在拉取请求中合并提交,您应该尝试通过变基而不是合并来更新您的分支。同样,我总是建议签出一个单独的分支来执行此操作,因为变基会更改您的提交历史记录,您可能需要保留原始提交历史记录的备份。

    【讨论】:

      【解决方案2】:

      由于Dec. 4th 2019,您可以尝试保护您的 PR 分支,并通过启用 Require linear history 拒绝任何包含合并提交的推送。

      这将确保您必须使用git pull --rebase,否则您将无法推送。

      【讨论】:

        【解决方案3】:

        正如@mkrufky 指出的那样,您应该使用git rebase 将您的工作重新定位到主分支上。然而,没有必要创建一个新的分支,因为 rebase 并没有真正重写历史,而是将提交“重放”到新的基础上。 Git 甚至会在一段时间内跟踪 git reflog 中的所有旧提交,从而防止您以这种方式丢失历史记录。

        正确的工作流程是:

        git fetch origin master
        git rebase origin/master
        

        但是,这正是git pull --rebase 所做的!可以选择将您的工作重新定位到您正在拉动的分支上,而不是合并两个分支。

        您可以在这里找到更多解释:Difference between git pull and git pull --rebase 和 git 手册的 Rebase 章节:https://git-scm.com/book/en/v2/Git-Branching-Rebasing

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-29
          • 2013-02-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多