在代码审查期间使用GIT跟踪更改(Stash vs.分支)



为了保持代码评论小而简洁,我提交了比完整功能更小的代码审核。这是在更大更改之前的清理工作,但是为了避免用清理处理最终的评论,我已经进行了此评论。

我以后的工作将基于此当前活跃的审查,并且由于审查而需要进行更改。但是,我也想在此代码正在评论时继续研究最终功能。

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

当前场景:

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适当感"

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

当您想提交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

或在代码审查分支顶部重新列入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"

最新更新