Git 教程

这个章节描述了一般的 gitgithub 工作流。

这不是全面的 git 参考。 它是为 github 托管服务量身定制的。 您可能会找到更好或更快的方法来完成git的工作,但这些应该可以让您开始。

有关学习 git 的一般资源,请参阅其他git资源

查看GitHub帮助中提供的github安装帮助页面

安装 git

使用git进行开发完全不需要github。 Git是一个分布式版本控制系统。 为了在您的机器上使用git,您必须安装它

获取代码的本地副本

在命令行中:

  1. git clone git://github.com/numpy/numpy.git

现在,您在新的numpy目录中有了代码树的副本。 如果这样不起作用,您可以尝试使用其他只读网址:

  1. git clone https://github.com/numpy/numpy.git

更新代码

您可能不时需要提取最新的代码。 为此,请执行以下操作:

  1. cd numpy
  2. git fetch
  3. git merge --ff-only

现在,numpy 中的分支树将具有初始存储库中的最新更改记录。

Git开发入门

本节和下一节将详细描述如何设置git以使用NumPy源代码。 如果您已经设置了git, 请跳至开发工作流程

基本的Git设置

  1. git config --global user.email you@yourdomain.example.com
  2. git config --global user.name "Your Name Comes Here"

创建属于你自己的 NumPy 代码

您只需要这样做一次。 这里的说明与 http://help.github.com/forking/ 上的说明非常相似 - 请参阅该页面以获取更多详细信息。 我们在这里重复其中的一些只是为了提供 NumPy 项目的细节,并建议一些默认名称。

设置和配置github帐户

如果您没有 github 帐户,请转到 github 页面并创建一个。

然后,您需要配置您的帐户以允许写访问-请参阅github帮助上的 生成SSH密钥 帮助。

创建自己的 NumPy 分叉副本

  1. 登录到您的 github 帐户。
  2. 转到位于NumPy github上的 NumPy github 主页。
  3. 点击页面上名为 fork 的按钮:

fork

稍等片刻后,您应该就能在你自己的主页上找到你自己的 NumPy 分叉副本。

设置你的Fork

首先,您要遵循有关创建属于你自己的 NumPy 代码说明。

概览

  1. git clone https://github.com/your-user-name/numpy.git
  2. cd numpy
  3. git remote add upstream https://github.com/numpy/numpy.git

详细

克隆你的fork仓库
  1. 使用 git clone https://github.com/your-user-name/numpy.git 将分叉克隆到你的本地计算机。
  2. 查看一下,然后将目录更改为新的repo:cd numpy。然后 git branch -a向您显示所有分支。您将得到如下内容:

    1. * master
    2. remotes/origin/master

    这告诉您您当前在 master 分支上,并且还具有到 origin/master 的远程连接。 哪个远程存储库是 origin/master ?尝试使用 git remote -v 查看远程URL。 他们将指向您的 github 分支。

    现在,您想连接到上游的 NumPy github 存储库,以便可以合并主干中的更改。

将您的存储库链接到上游仓库
  1. cd numpy
  2. git remote add upstream https://github.com/numpy/numpy.git

上游(upstream) 只是我们用来引用 NumPy github 上主要 NumPy 存储库的任意名称。

只是为了您自己的满意,请使用 git remote -v show 向您展示一个新的 ‘remote’,为您提供以下信息:

  1. upstream https://github.com/numpy/numpy.git (fetch)
  2. upstream https://github.com/numpy/numpy.git (push)
  3. origin https://github.com/your-user-name/numpy.git (fetch)
  4. origin https://github.com/your-user-name/numpy.git (push)

为了与NumPy中的更改保持同步,您需要设置存储库,以使其默认情况下从“上游”提取。 这可以通过以下方式完成:

  1. git config branch.master.remote upstream
  2. git config branch.master.merge refs/heads/master

您可能还想轻松访问发送到NumPy存储库的所有拉取请求:

  1. git config --add remote.upstream.fetch '+refs/pull/*/head:refs/remotes/upstream/pr/*'
  2. Your config file should now look something like (from ``$ cat .git/config``):
  1. [core]
  2. repositoryformatversion = 0
  3. filemode = true
  4. bare = false
  5. logallrefupdates = true
  6. ignorecase = true
  7. precomposeunicode = false
  8. [remote "origin"]
  9. url = https://github.com/your-user-name/numpy.git
  10. fetch = +refs/heads/*:refs/remotes/origin/*
  11. [remote "upstream"]
  12. url = https://github.com/numpy/numpy.git
  13. fetch = +refs/heads/*:refs/remotes/upstream/*
  14. fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*
  15. [branch "master"]
  16. remote = upstream
  17. merge = refs/heads/master

Git 配置

概览

您的个人 git 配置将保存在主目录的 .gitconfig 文件中。 这是一个示例 .gitconfig 文件:

  1. [user]
  2. name = Your Name
  3. email = you@yourdomain.example.com
  4. [alias]
  5. ci = commit -a
  6. co = checkout
  7. st = status -a
  8. stat = status -a
  9. br = branch
  10. wdiff = diff --color-words
  11. [core]
  12. editor = vim
  13. [merge]
  14. summary = true

您可以直接编辑此文件,也可以使用 git config --global 命令:

  1. git config --global user.name "Your Name"
  2. git config --global user.email you@yourdomain.example.com
  3. git config --global alias.ci "commit -a"
  4. git config --global alias.co checkout
  5. git config --global alias.st "status -a"
  6. git config --global alias.stat "status -a"
  7. git config --global alias.br branch
  8. git config --global alias.wdiff "diff --color-words"
  9. git config --global core.editor vim
  10. git config --global merge.summary true

要在另一台计算机上进行设置,您可以复制 ~/.gitconfig 文件,或运行上面的命令。

细节

配置 user.name 和 user.email

最好告诉 git 您是谁,以标记您对代码所做的任何更改。 最简单的方法是从命令行:

  1. git config --global user.name "Your Name"
  2. git config --global user.email you@yourdomain.example.com

这会将设置写入您的git配置文件,该文件现在应包含带有您的姓名和电子邮件的用户部分:

  1. [user]
  2. name = Your Name
  3. email = you@yourdomain.example.com

当然,您需要用您的实际姓名和电子邮件地址替换 Your Nameyou@yourdomain.example.com

别名

您可能会受益于一些常用命令的别名。

例如,您可能希望能够将 git checkout 缩短为 git co。 或者您可能想将 git diff --color-words 别名(给出diff格式正确的输出)的别名为 git wdiff

以下 git config --global 命令:

  1. git config --global alias.ci "commit -a"
  2. git config --global alias.co checkout
  3. git config --global alias.st "status -a"
  4. git config --global alias.stat "status -a"
  5. git config --global alias.br branch
  6. git config --global alias.wdiff "diff --color-words"

将在您的.gitconfig文件中创建一个别名部分,其内容如下:

  1. [alias]
  2. ci = commit -a
  3. co = checkout
  4. st = status -a
  5. stat = status -a
  6. br = branch
  7. wdiff = diff --color-words

编辑器

您可能还需要确保使用了您选择的编辑器

  1. git config --global core.editor vim

合并

在合并时要强制执行摘要(再次需要 ~/.gitconfig 文件):

  1. [merge]
  2. log = true

或从命令行:

  1. git config --global merge.log true

差异规格中的两个和三个点

感谢Yarik Halchenko的解释。

想象一下一系列的提交A,B,C,D …想象有两个分支,主题和母版。 当母版处于提交 “E” 状态时,您从母版中分离了主题。 提交的图形如下所示:

  1. A---B---C topic
  2. /
  3. D---E---F---G master

然后:

  1. git diff master..topic

将输出从G到C的差(即受F和G的影响),而:

  1. git diff master...topic

只会在主题分支中输出差异(即仅A、B和C)。

其他Git资源

教程和摘要

进阶git工作流程

有很多使用 git 的方法。这是其他项目提出的一些经验法则:

  • Linus Torvalds 的 git 管理
  • Linus Torvalds 的 linux git 工作流。摘要:使用git工具使您的编辑历史尽可能清晰;在您进行主动开发的分支中,尽可能少地从上游编辑中合并。

在线手册页

您可以使用(例如 git help push 或(类似的东西)git push --help)在自己的机器上获取这些信息, 但是为了方便起见,以下是一些常用命令的在线手册页: