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 副本(fork) 中的说明将 SciPy fork 到您的帐户中。

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

点击“管理”按钮,并将其他人作为协作者添加到仓库中

../../_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 中挑选您想要的提交,然后为包含反向移植的分支提交 pull request。

  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. 您可能会在此处遇到一些 cherry picking 冲突。这些冲突的解决方法与合并/变基冲突相同。不同之处在于,您可以使用 git blame 来查看 main 和反向移植分支之间的差异,以确保没有任何问题。

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

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

将更改推送到主仓库#

这仅在您具有主 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. 推送到 upstream

    git push upstream my-feature-branch:main
    

注意

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