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

Tracking changes using git during code review (stash vs. branch)

为了保持代码审查简洁明了,我提交了一个比完整功能更小的代码审查。这是在进行较大更改之前的清理,但为了避免将最终审查与清理混为一谈,我进行了这次审查。

我以后的工作将建立在当前进行中的审查的基础上,并且由于审查的结果,我需要进行一些更改。但是,我还想在审查此代码期间继续完成最终功能。

我如何正确跟踪我在该功能上的开发,同时仍然能够为代码审查进行更改。

当前情景:

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"

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

当您想提交 feature 代码审查时,说您的存储库看起来像这样。

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

只需创建一个名为 (cr/feature) 的新分支,其中 cr 是(您猜对了)"code review".

的缩写
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

或在代码审查分支上变基 feature

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

我最终做的是 chepner 和 larsks 所建议的:

我已经做了一些改动,所以我需要先把我做的东西藏起来。在分支 feature.

git stash

然后创建代码审查分支,其中只有我已经提交审查的内容。

git checkout -b feature-cr

最后,将我隐藏的更改放回我的功能分支

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