1、Git 用户手册(1.5.3 及后续版本适用) 翻译 : 罗峥嵘 (Robin Steven) 英文版本 : http:/www.kernel.org/pub/software/scm/git/docs/user-manual.html Contents1. Preface 前言 2. Chapter 1. Repositories and Branches 第一章. 版本库与分支 1. How to get a git repository 如何获取一个版本库 2. How to check out a different version of a project 如何提取项目的不同版本 3
2、. Understanding History: Commits 理解历史: 交付 1. Understanding history: commits, parents, and reachability 交付,父交付与可及性 2. Understanding history: History diagrams 历史沿革示图 3. Understanding history: What is a branch? 理解历史:什么是分支4. Manipulating branches 操作分支 5. Examining an old version without creating a new b
3、ranch 不通过创建新分支来调查旧版本 6. Examining branches from a remote repository 调查远程版本库上的分支 7. Naming branches, tags, and other references 命名分支,标签,与其他引用 8. Updating a repository with git-fetch 用 git fetch 更新版本库 9. Fetching branches from other repositories 获取其他版本库的分支3. Chapter 2. Exploring git history 第二章. 检索 gi
4、t 历史 1. How to use bisect to find a regression 如何用平分来定位撤退 2. Naming commits 交付的称谓 3. Creating tags 创建标签 4. Browsing revisions 浏览修订 5. Generating diffs 生成差异 6. Viewing old file versions 查看旧的文件版本 7. Examples 实例 1. Counting the number of commits on a branch 统计一个分支中的交付数目 2. Check whether two branches po
5、int at the same history 检查两分支是否在同一历史时期 3. Find first tagged version including a given fix 找到包含给定修正的第一个标签版本 4. Showing commits unique to a given branch 显示仅属于某个分支的交付 5. Creating a changelog and tarball for a software release 为软件的发行制作变更日志和压缩包 6. Finding commits referencing a file with given content 寻找一
6、个指向包含给定内容的文件的交付4. Chapter 3. Developing with git 第三章. 用 git 进行研发 1. Telling git your name 告诉 git 你的名字 2. Creating a new repository 创建新的版本库 3. How to make a commit 如何制作一个交付 4. Creating good commit messages 写好交付信息 5. Ignoring files 忽略文件 6. How to merge 如何合并 7. Resolving a merge 解决合并冲突 1. Getting confl
7、ict-resolution help during a merge 在合并过程中取得冲突解决帮助8. Undoing a merge 撤销合并 9. Fast-forward merges 快进合并 10. Fixing mistakes 修复失误 1. Fixing a mistake with a new commit 修复一个新的交付中的失误11. Fixing a mistake by rewriting history 通过重写历史来修复失误 12. Checking out an old version of a file 提取一个文件的旧版本 13. Temporarily s
8、etting aside work in progress 临时放下手头上的工作 14. Ensuring good performance 确保好的性能 15. Ensuring reliability 确保伸缩性 1. Checking the repository for corruption 检查版本的损坏 2. Recovering lost changes 恢复丢失的变更5. Chapter 4. Sharing development with others 第四章. 与他人协同研发 1. Getting updates with git-pull 用 git-pull 取得更新
9、 2. Submitting patches to a project 向项目提交补丁 3. Importing patches to a project 给项目导入补丁 4. Public git repositories 发布 git 版本库 5. Setting up a public repository 建立一个公共版本库 6. Exporting a git repository via the git protocol 通过 git 协议公开版本库 7. Exporting a git repository via http 通过 http 协议公开版本库 8. Pushing
10、changes to a public repository 将变更推入到公共版本库 9. What to do when a push fails 推入失败之后该怎么处理 10. Setting up a shared repository 建立共享版本库 11. Allowing web browsing of a repository 容许 Web 浏览版本库 12. Examples 例子 1. Maintaining topic branches for a Linux subsystem maintainer | Linux 子系统维护者如何维护主题分支6. Chapter 5.
11、Rewriting history and maintaining patch series 第五章 . 改写历史与维护补丁串 1. Creating the perfect patch series 创建出色的补丁串 2. Keeping a patch series up to date using git-rebase 使用 git-rebase 保持补丁串的新颖 3. Rewriting a single commit 重写单个交付 4. Reordering or selecting from a patch series 在补丁串中选取与重新排序 5. Other tools 第三
12、方工具 6. Problems with rewriting history 重写历史带来的问题 7. Why bisecting merge commits can be harder than bisecting linear history 为何定位合并交付中的问题要比在线性历史中困难7. Chapter 6. Advanced branch management 第六章. 高级分支管理 1. Fetching individual branches 2. git fetch and fast-forwards 抓取与快进 3. Configuring remote branches 配
13、置远程分支8. Chapter 7. Git concepts 第七章. Git 概念 1. The Object Database 对象数据库 1. Commit Object 交付对象 2. Tree Object 树对象 3. Blob Object 片对象 4. Trust 信赖 5. Tag Object 标签对象 6. How git stores objects efficiently: pack files | git 如何高效地储存对象: 打包文件 7. Dangling objects 悬空对象 8. Recovering from repository corruptio
14、n 从损坏中恢复2. The index 索引9. Chapter 8. Submodules 子模块 1.1. Pitfalls with submodules 子模块陷阱10. Chapter 9. Low-level git operations 第九章. 底层 git 操作 1. Object access and manipulation 对象访问与操作 2. The Workflow 运作流程 1. working directory - index 工作树 - 索引 2. index - object database 索引 - 对象数据库 3. object database
15、- index 对象数据库 - 索引 4. index - working directory 索引 - 工作目录 5. Tying it all together 全盘了解3. Examining the data 检验数据 4. Merging multiple trees 合并多个树 5. Merging multiple trees, continued 合并多个树,续完11. Chapter 10. Hacking git 第十章. git 的开发 1. Object storage format 对象的存储格式 2. A birds-eye view of Gits source
16、code 鸟瞰 git 源代码12. Chapter 11. GIT Glossary 第十一章 . GIT 字典 13. Appendix A. Git Quick Reference 附录 A. Git 快速参考 1. Creating a new repository 创建一个新的版本库 2. Managing branches 管理分支 3. Exploring history 勘查历史 4. Making changes 制作变更 5. Merging 合并 6. Sharing your changes 共享你的变更 7. Repository maintenance 版本库的维护
17、 8. Appendix B. Notes and todo list for this manual 附录 B. 备忘与本手册的工作计划Preface 前言Git is a fast distributed revision control system. Git 是一个快速的分布式版本控制系统 This manual is designed to be readable by someone with basic UNIX command-line skills, but no previous knowledge of git. 这个手册是面向那些具有基本的 Unix 命令行使用技能,但
18、是没有 Git 知识的人设计的。 Chapter 1, Repositories and Branches and Chapter 2, Exploring git history explain how to fetch and study a project using gitread these chapters to learn how to build and test a particular version of a software project, search for regressions, and so on. 第一章 版本库与分支 和 第二章 考查 git 历史 将展
19、示如何用 git 来获取和研究一个项目,通过阅读这些章节,我们学习如何建立和测试一个具体的软件项目的版本,学习“撤退”等等。 People needing to do actual development will also want to read Chapter 3, Developing with git and Chapter 4, Sharing development with others. 人们是需要开展真正的研发工作的,那么就学习 第三章, 用 git 进行开发 和 第四章,与他人共享研发成果。 Further chapters cover more specialized
20、topics. 更多的一些章节会涉及到许多的专题话题。 Comprehensive reference documentation is available through the man pages, or git-help(1) command. For example, for the command “git clone “, you can either use: 参考文档可以通过系统的手册页命令,或者是 git-help(1) 命令来查看。譬如,你想参考 “git clone “, 你可以用下面的两种方式: $ man git-cloneor: 或者: $ git help clo
21、neWith the latter, you can use the manual viewer of your choice; see git-help(1) for more information. 晚一点你就有机会用到这些手册查看器的;看 git-help(1) 会得到比较多的信息。 See also Appendix A, Git Quick Reference for a brief overview of git commands, without any explanation. 阅读 附录 A,那里是一个 git 命令的快速纵览,但是它不带任何的解说。 Finally, se
22、e Appendix B, Notes and todo list for this manual for ways that you can help make this manual more complete. 最后,看看 附录 B,这份手册的工作备忘和计划,通过它你可以帮助这份文档变得更完善。 Chapter 1. Repositories and Branches 第一章. 版本库与分支How to get a git repository 如何获取一个版本库It will be useful to have a git repository to experiment with a
23、s you read this manual. 有一个实验性的 git 版本库对我们阅读这份手册将非常有用。 The best way to get one is by using the git-clone(1) command to download a copy of an existing repository. If you dont already have a project in mind, here are some interesting examples: 获取一个已经存在的版本库,最佳的方法是用 git-clone 命令,如果你还没有什么心目中的项目的话,那么这里有些有
24、趣的例子: # git itself (approx. 10MB download):$ git clone git:/git.kernel.org/pub/scm/git/git.git# the Linux kernel (approx. 150MB download):$ git clone git:/git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.gitThe initial clone may be time-consuming for a large project, but you will only need
25、 to clone once. 对于一个大型项目来说,初始性的克隆是挺费时的,不过克隆只需要做一次。 The clone command creates a new directory named after the project (“git“ or “linux-2.6“ in the examples above). After you cd into this directory, you will see that it contains a copy of the project files, called the working tree, together with a spe
26、cial top-level directory named “.git“, which contains all the information about the history of the project. 克隆命令会创建一个新的目录,并根据项目的名称来命名这个项目(譬如上说例子中的 “git” 和 “linux-2.6”)。当你进入这个目录的时候,你可以看到它已经包含了项目的所有文件,我们称之为工作树,在顶层目录中还连带了一个叫 “.git“ 的特殊的目录,里面包含了项目的发展历史的所有信息。 How to check out a different version of a pro
27、ject 如何提取项目的不同版本Git is best thought of as a tool for storing the history of a collection of files. It stores the history as a compressed collection of interrelated snapshots of the projects contents. In git each such version is called a commit. 最好将 git 当作是文件发展的历史纪录的收集工具,它压缩并保存了项目的发展的关联性快照。在 git 中,每个
28、这些变更称作交付(commit)。 Those snapshots arent necessarily all arranged in a single line from oldest to newest; instead, work may simultaneously proceed along parallel lines of development, called branches, which may merge and diverge. 这些快照并不需要按从旧到新单线索地发展;它可以是同步并行地发展的,称之为分支,它们是可以合并和分割的。 A single git reposi
29、tory can track development on multiple branches. It does this by keeping a list of heads which reference the latest commit on each branch; the git-branch(1) command shows you the list of branch heads: 一个 git 版本库可以跟踪多个分支的发展,它通过保存一个分支头列表的方式来做到这一点,每个分支头都是一个引用(reference),它指向该分支最后的一个交付(commit); git-branc
30、h(1) 命令可以向你展示每个分支头: $ git branch* masterA freshly cloned repository contains a single branch head, by default named “master“, with the working directory initialized to the state of the project referred to by that branch head. 一个刚刚克隆的版本库只包含一个分支头,默认叫 “master” (主分支),并且工作目录已经被初始化为这个分支头所指向的项目状态。 Most pro
31、jects also use tags. Tags, like heads, are references into the projects history, and can be listed using the git-tag(1) command: 大部分的项目还用到标签(tags)。标签(Tags)就好像头(heads),它指向项目的某个历史场面,它们可以通过 git-tag(1) 命令列举出来: $ git tag -lv2.6.11v2.6.11-treev2.6.12v2.6.12-rc2v2.6.12-rc3v2.6.12-rc4v2.6.12-rc5v2.6.12-rc6v
32、2.6.13.Tags are expected to always point at the same version of a project, while heads are expected to advance as development progresses. Tags 被当做是项目统一的版本来对待,而 heads 则是项目前进的每一个步伐。 Create a new branch head pointing to one of these versions and check it out using git-checkout(1): 下面创建一个新的分支头,使其指向其中的某个
33、版本,同时将它提取出来,可以用 git-checkout(1) 命令: $ git checkout -b new v2.6.13The working directory then reflects the contents that the project had when it was tagged v2.6.13, and git-branch(1) shows two branches, with an asterisk marking the currently checked-out branch: 工作目录将被镜像为项目中标记为 v2.6.13 的版本的内容,用 git-bra
34、nch(1) 命令展示这个两个分支,前面带星号(*)的就是当前抽取的分支。 $ git branchmaster* newIf you decide that youd rather see version 2.6.17, you can modify the current branch to point at v2.6.17 instead, with 如果你打算看看 2.6.17 的版本,你可以迁移你当前的分支,让它指向 2.6.17, 使用一下命令: $ git reset -hard v2.6.17Note that if the current branch head was yo
35、ur only reference to a particular point in history, then resetting that branch may leave you with no way to find the history it used to point to; so use this command carefully. 注意,如果当前的分支头是你唯一的指向具体的历史场面的引用的话,那么复位 (resetting) 这个分支将令你无法找回这个分支以前的所有历史纪录,所以这个命令要慎用。 Understanding History: Commits 理解历史: 交付
36、Every change in the history of a project is represented by a commit. The git-show(1) command shows the most recent commit on the current branch: 项目的每一个历史变更体现为每一个交付 (commit)。git-show(1) 命令展示当前分支的最新交付: $ git showcommit 17cf781661e6d38f737f15f53ab552f1e95960d7Author: Linus Torvalds Date: Tue Apr 19 14:
37、11:06 2005 -0700Remove duplicate getenv(DB_ENVIRONMENT) callNoted by Tony Luck.diff -git a/init-db.c b/init-db.cindex 65898fab002dc6 100644- a/init-db.c+ b/init-db.c -7,7 +7,7 int main(int argc, char *argv)- char *sha1_dir = getenv(DB_ENVIRONMENT), *path;+ char *sha1_dir, *path;int len, i;if (mkdir(
38、“.git“, 0755) o create a new branch named , referencing the same point in history as the current branch o 创建一个新的分支,并引用当前分支作为同一历史沿革 git branch o create a new branch named , referencing , which may be specified any way you like, including using a branch name or a tag name o 创建一个名叫 的新分支,引用 ,它是可以任意指定的,可
39、以是现存的分支的名称或者是标签的名称 git branch -d o delete the branch ; if the branch you are deleting points to a commit which is not reachable from the current branch, this command will fail with a warning. o 删除一个叫 的分支;如果你要删除的这个分支所指向的当前分支中一个不可及的交付的话,那么命令将返回失败并作出提示 git branch -D o even if the branch points to a com
40、mit not reachable from the current branch, you may know that that commit is still reachable from some other branch or tag. In that case it is safe to use this command to force git to delete the branch. o 尽管需要删除一个当前分支不可及的交付,但是你知道那个交付仍然可有其他的分支或者是标签可及。在这种情况下,用这个命令强制删除一个分支是安全的。 git checkout o make the c
41、urrent branch , updating the working directory to reflect the version referenced by o 提取分支,也即是引用 版本状态更新工作目录的内容 git checkout -b o create a new branch referencing , and check it out. o 引用 创建一个叫 的分支,并且将它提取出来。 The special symbol “HEAD“ can always be used to refer to the current branch. In fact, git uses
42、 a file named “HEAD“ in the .git directory to remember which branch is current: 特殊的标号 “HEAD“ 总是被用作引用,指向当前分支。事实上,git 是用 .git 目录中的名叫 “HEAD“ 的文件来记住那个是当前分支。 $ cat .git/HEADref: refs/heads/masterExamining an old version without creating a new branch 不通过创建新分支来调查旧版本The git-checkout command normally expects
43、 a branch head, but will also accept an arbitrary commit; for example, you can check out the commit referenced by a tag: git-checkout 命令按常规是抽取分支头的,但是也可以接受任意的交付;例如,你可以引用一个标签来进行提取。 $ git checkout v2.6.17Note: moving to “v2.6.17“ which isnt a local branchIf you want to create a new branch from this che
44、ckout, you may do so(now or later) by using -b with the checkout command again. Example:git checkout -b HEAD is now at 427abfa. Linux v2.6.17The HEAD then refers to the SHA1 of the commit instead of to a branch, and git branch shows that you are no longer on a branch: 此时,HEAD 将指向交付的 SHA1 来代替分支名称, gi
45、t branch 命令表明你现在的项目状态不从属于任何一个分支: $ cat .git/HEAD427abfa28afedffadfca9dd8b067eb6d36bac53f$ git branch* (no branch)masterIn this case we say that the HEAD is “detached“. 这种情况,我们说 HEAD 是 “游离的” 。 This is an easy way to check out a particular version without having to make up a name for the new branch. Y
46、ou can still create a new branch (or tag) for this version later if you decide to. 这是一个方便的途径,既可以提取某个版本,又避免了创建与命名一个新的分支。如果你愿意,你当然还可以以这个版本为标本来创建一个新分支(或者是标签)。 Examining branches from a remote repository 调查远程版本库上的分支The “master“ branch that was created at the time you cloned is a copy of the HEAD in the
47、repository that you cloned from. That repository may also have had other branches, though, and your local repository keeps branches which track each of those remote branches, which you can view using the “-r“ option to git-branch(1): “master“ 分支是你克隆版本库的时候创建的,它是你的克隆的那个远程版本库上的 HEAD 的复件。那么远程版本库上可能还存在其他
48、的分支,故而你的本地版本库中也保留了那些远程分支的踪迹。你可以用 git branch(1) 命令加上 “-r“ 选项来查看。$ git branch -rorigin/HEADorigin/htmlorigin/maintorigin/manorigin/masterorigin/nextorigin/puorigin/todoYou cannot check out these remote-tracking branches, but you can examine them on a branch of your own, just as you would a tag: 你不可能将这
49、些远程分支提取出来,但是你可以用一个你自己的分支来调查他们,你当他们是一个标签好了。 $ git checkout -b my-todo-copy origin/todoNote that the name “origin“ is just the name that git uses by default to refer to the repository that you cloned from. 注意一下,“origin“ 是 git 用来指向你的版本库的克隆来源的默认名称。 Naming branches, tags, and other references 命名分支,标签,与其他引用Branches, remote-tracking branches, and tags are all references to commits. All references are named with a slash-separated path name starting with “refs“; the names weve been using so far are actually shorthand: 分支,远程踪迹分支,与标签所有这些都是交付的引用。它们都被命名为以