Git 技巧#

在 main 上变基#

这会将您的功能分支与上游 SciPy github 仓库的更改同步。如果您不是绝对需要这样做,请尽量避免这样做,除非您已经完成。第一步将是用上游的新提交来更新远程仓库

git fetch upstream

接下来,您需要更新功能分支

# go to the feature branch
git checkout my-new-feature
# make a backup in case you mess up
git branch tmp my-new-feature
# rebase on upstream main branch
git rebase upstream/main

如果您更改了上游也已更改的文件,这可能会产生需要您解决的合并冲突。有关此情况的帮助,请参阅下文

最后,在成功变基后删除备份分支

git branch -D tmp

注意

在 main 上变基比将上游合并回您的分支更可取。在功能分支上工作时,不鼓励使用 git mergegit pull

从混乱中恢复#

有时,您会搞砸合并或变基。幸运的是,在 Git 中,从这些错误中恢复相对简单。

如果在变基过程中搞砸了

git rebase --abort

如果您在变基后发现搞砸了

# reset branch back to the saved point
git reset --hard tmp

如果您忘记创建备份分支

# look at the reflog of the branch
git reflog show my-feature-branch

8630830 my-feature-branch@{0}: commit: BUG: io: close file handles immediately
278dd2a my-feature-branch@{1}: rebase finished: refs/heads/my-feature-branch onto 11ee694744f2552d
26aa21a my-feature-branch@{2}: commit: BUG: lib: make seek_gzip_factory not leak gzip obj
...

# reset the branch to where it was before the botched rebase
git reset --hard my-feature-branch@{2}

如果您实际上没有搞砸,但存在合并冲突,则需要解决这些冲突。这可能是最棘手的事情之一。有关如何执行此操作的良好描述,请参阅这篇关于合并冲突的文章

重写提交历史#

注意

只对您自己的功能分支执行此操作。

您提交中有一个令人尴尬的错别字?或者您多次错误开始,不希望后人看到。

这可以通过交互式变基来完成。

假设提交历史如下所示

git log --oneline
eadc391 Fix some remaining bugs
a815645 Modify it so that it works
2dec1ac Fix a few bugs + disable
13d7934 First implementation
6ad92e5 * masked is now an instance of a new object, MaskedConstant
29001ed Add pre-nep for a copule of structured_array_extensions.
...

并且 6ad92e5main 分支中的最后一次提交。假设我们想进行以下更改

  • 13d7934 的提交消息重写为更合理的文本。

  • 将提交 2dec1aca815645eadc391 合并为一个。

我们这样做

# make a backup of the current state
git branch tmp HEAD
# interactive rebase
git rebase -i 6ad92e5

这将打开一个编辑器,其中包含以下文本

pick 13d7934 First implementation
pick 2dec1ac Fix a few bugs + disable
pick a815645 Modify it so that it works
pick eadc391 Fix some remaining bugs

# Rebase 6ad92e5..eadc391 onto 6ad92e5
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

为了实现我们的目标,我们将对其进行以下更改

r 13d7934 First implementation
pick 2dec1ac Fix a few bugs + disable
f a815645 Modify it so that it works
f eadc391 Fix some remaining bugs

这意味着 (i) 我们想编辑 13d7934 的提交消息,以及 (ii) 将最后三个提交合并为一个。现在我们保存并退出编辑器。

Git 会立即弹出一个编辑器来编辑提交消息。修改后,我们得到输出

[detached HEAD 721fc64] FOO: First implementation
 2 files changed, 199 insertions(+), 66 deletions(-)
[detached HEAD 0f22701] Fix a few bugs + disable
 1 files changed, 79 insertions(+), 61 deletions(-)
Successfully rebased and updated refs/heads/my-feature-branch.

历史现在看起来像这样

0f22701 Fix a few bugs + disable
721fc64 ENH: Sophisticated feature
6ad92e5 * masked is now an instance of a new object, MaskedConstant

如果出错了,可以像上面解释的那样再次恢复。

github 上删除分支#

git checkout main
# delete branch locally
git branch -D my-unwanted-branch
# delete branch on GitHub
git push origin :my-unwanted-branch

(注意 test-branch 前面的冒号 :。另请参阅:guides/remove-a-remote-branch

多人共享一个仓库#

如果您想与其他人一起处理一些事情,并且所有人都提交到同一个仓库,甚至是同一个分支,那么只需通过 github 共享它。

首先将 SciPy 分叉到您的帐户中,如制作您自己的 SciPy 副本(分叉)所述。

然后,转到您分叉的仓库 GitHub 页面,例如 https://github.com/your-user-name/scipy

单击“Admin”按钮,并将其他人添加为仓库的协作者

../../_images/pull_button.png

现在所有这些人都可以做

git clone git@github.com:your-user-name/scipy.git

请记住,以 git@ 开头的链接使用 ssh 协议,并且是读写的;以 git:// 开头的链接是只读的。

您的协作者可以使用常规命令直接提交到该仓库

git commit -am 'ENH - much better code'
git push origin my-feature-branch # pushes directly into your repo

探索您的仓库#

要查看仓库分支和提交的图形表示

gitk --all

要查看此分支的提交的线性列表

git log

您还可以查看您的 github 仓库的网络图可视化工具

回溯#

回溯是将提交到 scipy/main 中的新功能/修复复制回稳定发布分支的过程。为此,您可以从要回溯的分支创建一个分支,挑选 scipy/main 中您想要的提交,然后为包含回溯的分支提交拉取请求。

  1. 首先,您需要创建您将要工作的分支。这需要基于较旧版本的 SciPy(而不是 main)

    # Make a new branch based on scipy/maintenance/1.8.x,
    # backport-3324 is our new name for the branch.
    git checkout -b backport-3324 upstream/maintenance/1.8.x
    
  2. 现在您需要使用 git cherry-pick 将 main 中的更改应用到此分支

    # Update remote
    git fetch upstream
    # Check the commit log for commits to cherry pick
    git log upstream/main
    # This pull request included commits aa7a047 to c098283 (inclusive)
    # so you use the .. syntax (for a range of commits), the ^ makes the
    # range inclusive.
    git cherry-pick aa7a047^..c098283
    ...
    # Fix any conflicts, then if needed:
    git cherry-pick --continue
    
  3. 您可能会在这里遇到一些冲突。这些冲突的解决方式与合并/变基冲突相同。但在这里您可以使用 git blame 查看 main 和回溯分支之间的差异,以确保没有任何东西被搞砸。

  4. 将新分支推送到您的 Github 仓库

    git push -u origin backport-3324
    
  5. 最后使用 Github 创建拉取请求。确保它是针对维护分支而不是 main,Github 通常会建议您针对 main 创建拉取请求。

将更改推送到主仓库#

这仅在您对主 SciPy 仓库拥有提交权限时才相关。

当您在功能分支中有一组“准备好”的更改,可以推送到 SciPy 的 mainmaintenance 分支时,您可以按如下方式将它们推送到 upstream

  1. 首先,在目标分支上合并或变基。

    1. 只有少数不相关的提交时,首选变基

      git fetch upstream
      git rebase upstream/main
      

      请参阅在 main 上变基

    2. 如果所有提交都相关,则创建合并提交

      git fetch upstream
      git merge --no-ff upstream/main
      
  2. 检查您将要推送的内容是否合理

    git log -p upstream/main..
    git log --oneline --graph
    
  3. 推送到上游

    git push upstream my-feature-branch:main
    

注意

通常最好使用 git push-n 标志,首先检查您是否正在将您想要的更改推送到您想要的位置。