在代码审查期间使用git跟踪更改(存储与分支)

时间:2015-05-06 15:26:54

标签: git git-branch git-stash

为了保持代码审查的简洁性,我提交了一个较小的代码审查而不是完整的功能。这是在更大的变化之前进行的清理,但为了避免在清理过程中弄乱最终审核,我已经进行了此次审核。

我以后的工作将建立在目前正在进行的审核之上,审核结果将需要进行更改。但是,我还希望在审核此代码时继续研究最终功能。

如何在仍然可以更改代码审核的同时正确跟踪我对该功能的开发。

当前情况:

master -x-x-x---x---|
feature      \-x-x-x| code review

未来情景(分支)

master -x-x-x---x---|
feature      \-x-x-x|-x--x--|
  feature2          \x--x--x| code review complete (merge)

未来场景(藏匿)

master -x-x-x---x---|
feature      \-x-x-x|-x--x--| code review complete (merge)
  work on feature branch, stash changes if needed to make code review updates

我认为分支模型更有意义,但创建另一个具有相同名称和相同目的的分支似乎违反了某些" git propriety"

2 个答案:

答案 0 :(得分:1)

我认为分支是正确的方法;你可能只想要好的分支名称,而不是为你的工作创建一个新的分支,使用新的分支来"冻结"代码审查代码的快照。

如果您想提交feature进行代码审核,请说明您的存储库看起来像这样。

* -- * -- * -- * -- * -- * -- *  master
      \
       * -- * -- * -- * feature, HEAD

只需创建一个名为(cr/feature)的新分支,其中cr是(您猜对了)的简称"代码审核"。

git branch cr/feature

现在您当前的分支机构有两个分支机构。

* -- * -- * -- * -- * -- * -- *  master
      \
       * -- * -- * -- * feature, cr/feature, HEAD

在继续处理功能时,您不会影响正在进行代码审核的代码。

* -- * -- * -- * -- * -- * -- *  master
      \
       * -- * -- * -- * -- * -- * -- * feature, HEAD
                      |
                    cr/feature

代码审核完成后,已审核的代码是否会合并到master

git checkout master
git merge cr/feature
git branch -d cr/feature  # Optional


* -- * -- * -- * -- * -- * -- * -- *  master
      \                           /
       *   --   *   --   *  --   * -- * -- * -- * feature, HEAD
                                 |
                             cr/feature (if not deleted)

通过这种方式,代码审阅者永远不会看到您继续在feature上工作,直到您通过明确创建一个分支供他们查看来提交代码视图。

如果已审核的代码需要更新,您可以将其添加到cr/feature分支:

* -- * -- * -- * -- * -- * -- *  master
      \
       * -- * -- * -- * -- * -- * -- * feature, HEAD
                      \
                       * -- * cr/feature

并将cr/feature合并回feature

* -- * -- * -- * -- * -- * -- *  master
      \
       * -- * -- * -- * -- * -- * -- * -- * feature, HEAD
                      \                  /
                       *      -----     * cr/feature
在代码审查分支

之上

或rebase feature

* -- * -- * -- * -- * -- * -- *  master
      \
       * -- * -- * -- *       * -- * -- * feature, HEAD
                      \      /
                       * -- *  cr/feature

答案 1 :(得分:1)

我最终做的事情就像chepner和larsks所说:

我已经做了一些改变,所以我需要先收藏我先做的事情。在分支feature上。

git stash

然后创建了代码审查分支,该分支仅包含我已提交给审核的内容。

git checkout -b feature-cr

最后,将我的藏匿更改重新放回我的功能分支

git checkout feature
git stash pop
git commit -am "Some descriptive commit message"