Git提示¶
重新基于主数据库¶
这会使用上游的更改更新要素分支 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 master branch
git rebase upstream/master
如果您对上游也已更改的文件进行了更改,则可能会生成需要解决的合并冲突。看见 below 在这种情况下寻求帮助。
最后,在成功执行REBASE后删除备份分支::
git branch -D tmp
注解
与上游合并回到您的分支机构相比,根据主分支机构重新建立基础更可取。使用 git merge
和 git 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}
如果您实际上没有搞砸,但存在合并冲突,则需要解决这些冲突。这可能是最难做好的事情之一。有关如何执行此操作的详细说明,请参见 this article on merging conflicts 。
重写提交历史记录¶
注解
仅对您自己的功能分支执行此操作。
你的承诺里有令人尴尬的打字错误吗?或者,也许你做了几次错误的开始,你不想让后人看到。
这可以通过以下方式完成 交互式重定基址 。
假设提交历史如下所示::
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.
...
和 6ad92e5
中的最后一次提交。 master
布兰奇。假设我们要进行以下更改:
重写以下内容的提交消息
13d7934
去做一些更明智的事。组合提交
2dec1ac
,a815645
,eadc391
变成一件事。
我们的做法如下:
# 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
如果它出了问题,正如所解释的那样,恢复是可能的。 above 。
删除上的分支 github¶
git checkout master
# delete branch locally
git branch -D my-unwanted-branch
# delete branch on github
git push origin :my-unwanted-branch
(请注意冒号 :
在此之前 test-branch
。另请参阅:https://github.com/guides/remove-a-remote-branch
几个人共享一个存储库¶
如果您希望与其他人一起处理某些内容,并且都提交到同一存储库,甚至同一分支中,那么只需通过 github.
第一个将SciPy分叉到您的帐户,如从 制作您自己的SciPy副本(分叉) 。
然后,转到您的分叉存储库GitHub页面,比如 https://github.com/your-user-name/scipy
单击“Admin”按钮,然后将其他任何人添加到回购中作为协作者:
现在所有这些人都可以做::
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
您还可以查看 network graph visualizer 为您的 github 回购。
回溯¶
回迁是复制中提交的新功能/修复的过程 scipy/master 回到稳定的发布分支。要做到这一点,您可以在您要回传到的分支上创建一个分支,从其中精选您想要的提交 scipy/master
,然后提交包含后端口的分支的拉取请求。
首先,您需要创建要处理的分支。这需要基于旧版本的SciPy(不是MASTER):
# 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
现在,您需要使用以下命令将主目录中的更改应用到此分支 git cherry-pick ::
# Update remote git fetch upstream # Check the commit log for commits to cherry pick git log upstream/master # 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
你可能会在这里遇到一些冲突,樱桃采摘。这些问题的解决方式与合并/变更基准冲突的方式相同。除了这里你可以用 git blame 查看主分支和后端分支之间的区别,以确保不会出错。
将新分支推送到您的Github存储库::
git push -u origin backport-3324
最后,使用Github发出拉取请求。请确保它是针对维护分支而不是针对主机,Github通常会建议您针对主机进行拉取请求。
将更改推送到主回购¶
这仅在您对主SciPy回购拥有提交权限时才相关。
当功能分支中的一组“就绪”更改准备好用于本网站时 master
或 maintenance
分支,您可以将它们推到 upstream
具体如下:
首先,合并或重新建立目标分支的基础。
只有少数几个不相关的提交会倾向于重新设置基数::
git fetch upstream git rebase upstream/master
看见 重新基于主数据库 。
如果所有提交都是相关的,请创建合并提交::
git fetch upstream git merge --no-ff upstream/master
检查您要推送的内容是否合理::
git log -p upstream/master.. git log --oneline --graph
推送到上游::
git push upstream my-feature-branch:master
注解
通常情况下,使用 -n
标志为 git push
首先检查您是否要将您想要的更改推送到您想要的位置。