Difference between revisions of "Git (简体中文)"

From ArchWiki
Jump to: navigation, search
(fix header, update interlanguage links (https://github.com/lahwaacz/wiki-scripts/blob/master/update-interlanguage-links.py))
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
[[Category: Version Control System (简体中文)]]
 
[[Category: Version Control System (简体中文)]]
[[Category: 简体中文]]
 
 
[[en:Git]]
 
[[en:Git]]
 +
[[ja:Git]]
 
{{translateme (简体中文)}}
 
{{translateme (简体中文)}}
{{Article summary start|摘要}}
+
{{Related articles start (简体中文)}}
{{Article summary text|安装和使用 Git 版本管理系统}}
+
{{Related|Super Quick Git Guide}}
{{Article summary heading|相关页面}}
+
{{Related|Gitweb}}
{{Article summary wiki|Super Quick Git Guide}}: Generally about contributing to pacman, although it still serves as a practical Git tutorial
+
{{Related|Cgit}}
{{Article summary wiki|Gitweb}}
+
{{Related|Subversion}}
{{Article summary wiki|Cgit}}
+
{{Related|Concurrent Versions System}}
{{Article summary wiki|Subversion}}
+
{{Related articles end}}
{{Article summary wiki|Concurrent Versions System}}
+
{{Article summary link|github|http://github.com/}}
+
{{Article summary end}}
+
  
[http://git-scm.com/ Git] 是一个 Linus Torvalds 编写的版本控制系统 (VCS),现在被用来维护 Linux 内核以及数以千计的其他项目,包括 [[Pacman (简体中文)|Pacman]](Arch 的软件包管理器)
+
[http://git-scm.com/ Git] 是一个由 Linux 内核作者 Linus Torvalds 编写的版本控制系统(VCS),现在被用来维护 Linux 内核以及数以千计的其他项目,包括 Arch 的软件包管理器 [[Pacman (简体中文)|Pacman]]
  
有一个很全面的文档,包含了手册和教程,可以从 [http://git-scm.com/documentation 官方网站] 获得。
+
在官方网站可以获得一份完整的、包含参考与教程的 [http://git-scm.com/documentation 文档]
  
 
==安装==
 
==安装==
{{Pkg|git}} 可以用 [[pacman (简体中文)|Pacman]] 从 [extra] 仓库安装。如果你关心使用 Git 配合其他的 VCS 软件,邮件服务器,或者使用 Git 的图形界面,注意看可选的依赖。
+
{{Pkg|git}} 可以通过 [[pacman (简体中文)|Pacman]] 从 [extra] 官方源安装。如果你希望搭配其它 VCS、邮件服务器或 Git 的图形界面使用 git,请注意安装时提示的可选依赖。
  
Bash 自动完成 (例如,按 tab 来完成你正在键入的命令),只需要:
+
如果需要 Bash 命令补完(也即按下 {{ic|Tab}} 来完成你正在键入的命令),请在{{ic|~/.bashrc}}文件中添加如下内容:
 
{{bc|source /usr/share/git/completion/git-completion.bash}}
 
{{bc|source /usr/share/git/completion/git-completion.bash}}
另外,你可以安装 {{Pkg|bash-completion}} 软件包来自动为新的外壳加载自动完成。
 
  
如果你想使用 Git 内建的图形界面 (例如 {{Ic|gitk}} 或者 {{Ic|git gui}}) 你需要安装 {{Pkg|tk}} 软件包,否则你会遇到一个隐晦的错误信息:
+
你也可以安装 {{Pkg|bash-completion}} 来自动为 shell 提供命令补完。
 +
 
 +
如果你想使用 Git 内建的图形界面(例如 {{Ic|gitk}} 或者 {{Ic|git gui}}),你需要安装 {{Pkg|tk}} 软件包,否则你会遇到一个隐晦的错误信息:
 
{{bc|/usr/bin/gitk: line 3: exec: wish: not found.}}
 
{{bc|/usr/bin/gitk: line 3: exec: wish: not found.}}
  
 
== 配置 ==
 
== 配置 ==
Git 从几个 INI 格式的配置文件中读取配置信息。在每一个 git 仓库中 {{ic|.git/config}} 被用来指定与本仓库有关的配置选项。每一个用户 ("global") 的配置文件在 {{ic|$HOME/.gitconfig}} 被用来作为仓库配置的备用配置。你也可以直接编辑文件,但是更推荐的方法是使用 git-config 工具。例如,
+
Git 从若干 INI 格式的配置文件中读取配置信息。在每一个 git 版本库中,{{ic|.git/config}} 用于指定与该版本库有关的配置选项。在 {{ic|$HOME/.gitconfig}} 中的用户 ("global") 的配置文件将被用作仓库配置的备用配置。你可以直接编辑配置文件,但是更推荐的方法是使用 git-config 工具。例如,
 +
 
 
  $ git config --global core.editor "nano -w"
 
  $ git config --global core.editor "nano -w"
会添加 {{Ic|<nowiki>editor = nano -w</nowiki>}} 到 {{ic|~/.gitconfig}} 文件的 {{Ic|<nowiki>[core]</nowiki>}} 部分中。
 
  
[http://www.kernel.org/pub/software/scm/git/docs/git-config.html git-config 工具的 man page] 有一个很长的参数列表,列出所以可以设置的变量。
+
会在 {{ic|~/.gitconfig}} 文件的 {{Ic|<nowiki>[core]</nowiki>}} 部分中添加 {{Ic|<nowiki>editor = nano -w</nowiki>}}。
 +
 
 +
[http://www.kernel.org/pub/software/scm/git/docs/git-config.html git-config 工具的 man page] 提供了完整的选项列表。
  
 
这是一些你可能用到的常见的配置:
 
这是一些你可能用到的常见的配置:
 
  $ git config --global user.name "Firstname Lastname"
 
  $ git config --global user.name "Firstname Lastname"
 
  $ git config --global user.email "your_email@youremail.com"
 
  $ git config --global user.email "your_email@youremail.com"
 +
 +
===在 Git 命令行下启用彩色输出===
 +
配置 {{ic|color.ui}} 选项可以令 Git 以彩色输出信息。
 
  $ git config --global color.ui true
 
  $ git config --global color.ui true
  $ git config --global --list
+
 
 +
===解决 Git 在命令行下中文文件名显示为数字的问题===
 +
  $ git config --global core.quotepath false
  
 
==基本用法==
 
==基本用法==
  
 
===克隆一个版本库===
 
===克隆一个版本库===
 +
 +
以下命令可以将一个 Git 版本库克隆至本地目录的新文件夹中:
  
 
  git clone <repo location> <dir>
 
  git clone <repo location> <dir>
  
will clone a Git repository in a new directory inside your current directory. Leaving out {{ic|<dir>}}
+
如果留空 {{ic|<dir>}} 字段,就会以 Git 版本库的名称命名新文件夹,例如:
will cause it to name the folder after the Git repository. For example,
+
  
 
  git clone git@github.com:torvalds/linux.git
 
  git clone git@github.com:torvalds/linux.git
  
clones Github's mirror of the Linux kernel into a directory named "linux".
+
可以将 GitHub 上 Linux 内核的镜像克隆至名为「linux」的文件夹中。
  
 
===提交(commit)文件到版本库===
 
===提交(commit)文件到版本库===
  
Git's commit process involves two steps:
+
Git 的提交过程分为两步:
  
# Add new files, add changes for existing files (both with {{ic|git add <files>}}), and/or remove files (with {{ic|git rm}}). These changes are put in a staging area called the index.
+
# 添加新文件、修改现有的文件(均可通过 {{ic|git add <files>}} 完成), 或者删除文件(通过 {{ic|git rm}} 完成)。这些修改将被存入名叫 index 的文件中。
# Call {{ic|git commit}} to commit the changes.
+
# 使用 {{ic|git commit}} 提交修改。
  
Git commit will open up a text editor to provide a commit message.
+
Git 提交时会打开文本编辑器,填写提交信息。你可以通过 {{ic|git config}} 命令修改 {{ic|core.editor}} 来选择编辑器。
You can set this editor to whatever you want by changing the {{ic|core.editor}} option with {{ic|git config}}.
+
  
Alternatively, you can use {{ic|git commit -m <message>}} to supply the commit message without opening the text editor.
+
此外,你也可以直接用 {{ic|git commit -m <message>}} 命令在提交时填写提交信息,这样就不会打开编辑器。
  
Other useful commit tricks:
+
其它有用的技巧:
  
 
{{ic|git commit -a}} lets you commit changes you have made to files already under Git control
 
{{ic|git commit -a}} lets you commit changes you have made to files already under Git control
 
without having to take the step of adding the changes to the index. You still have to add new files with git add.
 
without having to take the step of adding the changes to the index. You still have to add new files with git add.
  
{{ic|git add -p}} lets you commit specific parts of files you have changed.
+
{{ic|git commit -a}} 命令可以跳过添加修改的部分,但是如果创建新文件依然需要 {{ic|git add}}。
This is useful if you have made a bunch of changes that you think would be best split into several commits.
+
 
 +
{{ic|git add -p}} 命令可以提交修改文件的特定部分。如果你进行了许多修改而且希望将其分多次提交的话,这一选项非常有用。
  
 
===将改动提交(push)到公共版本库===
 
===将改动提交(push)到公共版本库===
  
To push your changes up to a server (such as Github), use
+
以下命令可以将修改提交至服务器(例如 Github):
  
 
  git push <server name> <branch>
 
  git push <server name> <branch>
  
Adding {{ic|-u}} will make this server the default one to push to for this branch.
+
添加 {{ic|-u}} 参数可以将该服务器设为当前分支(branch)提交时的默认服务器。如果你是通过上文的方法克隆的版本库,默认服务器将是你克隆的来源(别名「origin」),默认分支将是 master。也就是说如果你按照上文的方法克隆的话,提交时只要执行 {{ic|git push}} 即可。如果需要的话,可以令 Git 提交至多个服务器,不过这比较复杂。下文将讲解分支(branch)。
If you have cloned the repository as described above, the server will default to the location you cloned the
+
repository from (nicknamed "origin") and the branch will default to the master branch.
+
In other words, if you have followed this guide's instructions in cloning, {{ic|git push}} will suffice.
+
You can set up Git to push to multiple servers if you want, but that is a more advanced topic.
+
Branches will be discussed later in this guide.
+
  
 
===从服务器公共版本库下载修改===
 
===从服务器公共版本库下载修改===
  
If you are working on multiple machines and want to update your local repository to what the server has, you use
+
如果你在多台电脑上工作,并且需要将本地版本库与服务器更新,可以执行:
  
 
  git pull <server name> <branch>
 
  git pull <server name> <branch>
  
Similarly to push, the server name and branch should have sane defaults, so {{ic|git pull}} should suffice.
+
push 类似,server name branch 都可以根据默认来,所以只需执行 {{ic|git pull}}
Git pull is actually shorthand for doing two things:
+
  
# Calling {{ic|git fetch}}, which updates the local copy of what the server has. Such branches are called "remote" branches because they are mirroring remote servers.
+
Git pull 实际上是如下两个命令的简写:
# Calling {{ic|git merge}}, which merges what the remote branch has with what you have. If your commit history is the same as the server's commit history, you will be automatically fast-forwarded to the latest commit on the server. If your history does not match (maybe someone else has pushed commits since you last synced), the two histories will be merged.
+
 
 +
# {{ic|git fetch}},将服务器文件复制至本地,这一分支被称作「remote」也即它是远程服务器的镜像。
 +
# {{ic|git merge}},将「remote」分支的文件与本地文件合并。如果你的本地提交记录与服务器的提交记录相同,就可以直接得到服务器的最新版本。如果你的提交记录与服务器的记录不符(例如在你最后一次提交之后别人进行了提交),两份提交记录将被合并。
  
 
It is not a bad idea to get into the practice of using these two commands instead of {{ic|git pull}}. This way you can
 
It is not a bad idea to get into the practice of using these two commands instead of {{ic|git pull}}. This way you can
 
check to make sure that the server contains what you would expect before merging.
 
check to make sure that the server contains what you would expect before merging.
 +
 +
分步执行两个命令而非 {{ic|git pull}} 并不是坏事,这样可以确保合并之前服务器的文件与你期望的相同。
  
 
===查看历史记录===
 
===查看历史记录===
  
The command {{ic|git log}} shows the history of your current branch. Note that each commit is identified by a SHA-1 hash.
+
{{ic|git log}} 命令可以显示当前分支的历史记录。注意每一次提交(commit)会以一个 SHA-1 标记区分,接下来是提交者、提交日期以及提交信息。更实用的命令:
The author, commit date, and commit message follow. A more useful command is
+
  
 
  git log --graph --oneline --decorate
 
  git log --graph --oneline --decorate
  
which provides a display similar to TortoiseGit's log window. It shows the following:
+
可以显示与 TortoiseGit 的提交记录类似的窗口,这一窗口包含了如下内容:
  
* The first 7 digits of each commit's SHA-1 hash (enough to be unique)
+
* 每次提交的 SHA-1 标记的前七位(足以区分不同的提交)
* The {{ic|--graph}} option shows how any branches (if there are others) fork off from the current branch.
+
* {{ic|--graph}} 选项可以显示从当前分支 fork 的分支数目(如果有的话)
* The {{ic|--oneline}} option shows only the first line of each commit message
+
* {{ic|--oneline}} 选项可以在一行内显示每次提交的信息
* The {{ic|--decorate}} option shows all commit labels (branches and tags)
+
* {{ic|--decorate}} 选项可以显示所有的提交信息(包括分支与标签)
  
It may be convenient to alias this command as {{ic|git graph}} by doing the following:
+
可以通过如下命令将这一命令以 {{ic|git graph}} 的别名保存:
  
 
  git config --global alias.graph 'log --graph --oneline --decorate'
 
  git config --global alias.graph 'log --graph --oneline --decorate'
  
Now typing {{ic|git graph}} will run {{ic|git log --graph --oneline --decorate}}.
+
现在执行 {{ic|git graph}} 将等价于执行 {{ic|git log --graph --oneline --decorate}}
{{ic|git graph}} and {{ic|git log}} may be given the {{ic|--all}} flag in order to view all branches instead of just the current one.
+
 
Adding {{ic|--stat}} to one of these commands is also useful -
+
{{ic|git graph}} {{ic|git log}} 命令也可以带 {{ic|--all}} 的参数执行,这将显示所有的分支信息,而不止当前的分支。
it shows which files each commit changed and how many lines were changed in each file.
+
 
 +
也可以带 {{ic|--stat}} 参数执行,它可以显示每次提交时哪些文件有修改、修改了多少行。
 +
 
 +
===处理合并(merge)===
 +
 
 +
当你执行 pull、进行复原操作,或者将一个分支与另一个进行合并时会需要处理合并。与其它 VCS 类似,当 Git 无法自动处理合并时,就需要使用者进行处理。
  
===处理合并===
+
可以查看 Git Book 的[http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging#Basic-Merge-Conflicts 这一部分]讲解如何处理冲突合并。
  
Merges happen when you pull, as a result of a rebase operation, and when you merge one branch into another.
+
如果你需要通过合并来还原的话,可以带 {{ic|--abort}} 参数运行合并相关的命令,例如 {{ic|git merge --abort}}{{ic|git pull --abort}}{{ic|git rebase --abort}})
Like other version control tools, when Git cannot automatically merge a commit, it turns to you.
+
See [http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging#Basic-Merge-Conflicts this section]
+
of the Git Book for an explanation on how to resolve merge conflicts.
+
If you screw up and would like to back out of the merge, you can usually abort the merge using the {{ic|--abort}} flag
+
with whatever command started the merge (e.g. {{ic|git merge --abort}}, {{ic|git pull --abort}}, {{ic|git rebase --abort}}).
+
  
 
==使用分布式版本控制系统==
 
==使用分布式版本控制系统==
Line 265: Line 268:
 
===Git SSH===
 
===Git SSH===
 
You first need to have a public SSH key. For that follow the guide at [[Using SSH Keys]]. To set up SSH itself, you need to follow the [[SSH]] guide. This assumes you have a public SSH key now and that your SSH is working.
 
You first need to have a public SSH key. For that follow the guide at [[Using SSH Keys]]. To set up SSH itself, you need to follow the [[SSH]] guide. This assumes you have a public SSH key now and that your SSH is working.
Open your SSH key in your favorite editor (default public key name is {{ic|~/.ssh/id_rsa.pub}}), and copy its content ({{keypress|Ctrl+c}}).
+
Open your SSH key in your favorite editor (default public key name is {{ic|~/.ssh/id_rsa.pub}}), and copy its content ({{ic|Ctrl+c}}).
 
Now go to your user where you have made your Git repository, since we now need to allow that SSH key to log in on that user to access the Git repository.
 
Now go to your user where you have made your Git repository, since we now need to allow that SSH key to log in on that user to access the Git repository.
 
Open {{ic|~/.ssh/authorized_keys}} in your favorite editor, and paste the contents of id_rsa.pub in it. Be sure it is all on one line! That is important! It should look somewhat like this:
 
Open {{ic|~/.ssh/authorized_keys}} in your favorite editor, and paste the contents of id_rsa.pub in it. Be sure it is all on one line! That is important! It should look somewhat like this:
Line 274: Line 277:
 
Now you can checkout your Git repository this way (change where needed. Here it is using the git username and localhost):
 
Now you can checkout your Git repository this way (change where needed. Here it is using the git username and localhost):
 
  git clone git@localhost:my_repository.git
 
  git clone git@localhost:my_repository.git
You should now get an SSH yes/no question. Type {{ic|yes}} followed by {{keypress|Enter}}. Then you should have your repository checked out. Because this is with SSH, you also do have commit rights now. For that look at [[Git]] and [[Super Quick Git Guide]].
+
You should now get an SSH yes/no question. Type {{ic|yes}} followed by {{ic|Enter}}. Then you should have your repository checked out. Because this is with SSH, you also do have commit rights now. For that look at [[Git]] and [[Super Quick Git Guide]].
  
 
====特定非标准端口====
 
====特定非标准端口====

Latest revision as of 14:03, 19 July 2015

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

Git 是一个由 Linux 内核作者 Linus Torvalds 编写的版本控制系统(VCS),现在被用来维护 Linux 内核以及数以千计的其他项目,包括 Arch 的软件包管理器 Pacman

在官方网站可以获得一份完整的、包含参考与教程的 文档

安装

git 可以通过 Pacman 从 [extra] 官方源安装。如果你希望搭配其它 VCS、邮件服务器或 Git 的图形界面使用 git,请注意安装时提示的可选依赖。

如果需要 Bash 命令补完(也即按下 Tab 来完成你正在键入的命令),请在~/.bashrc文件中添加如下内容:

source /usr/share/git/completion/git-completion.bash

你也可以安装 bash-completion 来自动为 shell 提供命令补完。

如果你想使用 Git 内建的图形界面(例如 gitk 或者 git gui),你需要安装 tk 软件包,否则你会遇到一个隐晦的错误信息:

/usr/bin/gitk: line 3: exec: wish: not found.

配置

Git 从若干 INI 格式的配置文件中读取配置信息。在每一个 git 版本库中,.git/config 用于指定与该版本库有关的配置选项。在 $HOME/.gitconfig 中的用户 ("global") 的配置文件将被用作仓库配置的备用配置。你可以直接编辑配置文件,但是更推荐的方法是使用 git-config 工具。例如,

$ git config --global core.editor "nano -w"

会在 ~/.gitconfig 文件的 [core] 部分中添加 editor = nano -w

git-config 工具的 man page 提供了完整的选项列表。

这是一些你可能用到的常见的配置:

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@youremail.com"

在 Git 命令行下启用彩色输出

配置 color.ui 选项可以令 Git 以彩色输出信息。

$ git config --global color.ui true

解决 Git 在命令行下中文文件名显示为数字的问题

$ git config --global core.quotepath false

基本用法

克隆一个版本库

以下命令可以将一个 Git 版本库克隆至本地目录的新文件夹中:

git clone <repo location> <dir>

如果留空 <dir> 字段,就会以 Git 版本库的名称命名新文件夹,例如:

git clone git@github.com:torvalds/linux.git

可以将 GitHub 上 Linux 内核的镜像克隆至名为「linux」的文件夹中。

提交(commit)文件到版本库

Git 的提交过程分为两步:

  1. 添加新文件、修改现有的文件(均可通过 git add <files> 完成), 或者删除文件(通过 git rm 完成)。这些修改将被存入名叫 index 的文件中。
  2. 使用 git commit 提交修改。

Git 提交时会打开文本编辑器,填写提交信息。你可以通过 git config 命令修改 core.editor 来选择编辑器。

此外,你也可以直接用 git commit -m <message> 命令在提交时填写提交信息,这样就不会打开编辑器。

其它有用的技巧:

git commit -a lets you commit changes you have made to files already under Git control without having to take the step of adding the changes to the index. You still have to add new files with git add.

git commit -a 命令可以跳过添加修改的部分,但是如果创建新文件依然需要 git add

git add -p 命令可以提交修改文件的特定部分。如果你进行了许多修改而且希望将其分多次提交的话,这一选项非常有用。

将改动提交(push)到公共版本库

以下命令可以将修改提交至服务器(例如 Github):

git push <server name> <branch>

添加 -u 参数可以将该服务器设为当前分支(branch)提交时的默认服务器。如果你是通过上文的方法克隆的版本库,默认服务器将是你克隆的来源(别名「origin」),默认分支将是 master。也就是说如果你按照上文的方法克隆的话,提交时只要执行 git push 即可。如果需要的话,可以令 Git 提交至多个服务器,不过这比较复杂。下文将讲解分支(branch)。

从服务器公共版本库下载修改

如果你在多台电脑上工作,并且需要将本地版本库与服务器更新,可以执行:

git pull <server name> <branch>

与 push 类似,server name 与 branch 都可以根据默认来,所以只需执行 git pull

Git pull 实际上是如下两个命令的简写:

  1. git fetch,将服务器文件复制至本地,这一分支被称作「remote」也即它是远程服务器的镜像。
  2. git merge,将「remote」分支的文件与本地文件合并。如果你的本地提交记录与服务器的提交记录相同,就可以直接得到服务器的最新版本。如果你的提交记录与服务器的记录不符(例如在你最后一次提交之后别人进行了提交),两份提交记录将被合并。

It is not a bad idea to get into the practice of using these two commands instead of git pull. This way you can check to make sure that the server contains what you would expect before merging.

分步执行两个命令而非 git pull 并不是坏事,这样可以确保合并之前服务器的文件与你期望的相同。

查看历史记录

git log 命令可以显示当前分支的历史记录。注意每一次提交(commit)会以一个 SHA-1 标记区分,接下来是提交者、提交日期以及提交信息。更实用的命令:

git log --graph --oneline --decorate

可以显示与 TortoiseGit 的提交记录类似的窗口,这一窗口包含了如下内容:

  • 每次提交的 SHA-1 标记的前七位(足以区分不同的提交)
  • --graph 选项可以显示从当前分支 fork 的分支数目(如果有的话)
  • --oneline 选项可以在一行内显示每次提交的信息
  • --decorate 选项可以显示所有的提交信息(包括分支与标签)

可以通过如下命令将这一命令以 git graph 的别名保存:

git config --global alias.graph 'log --graph --oneline --decorate'

现在执行 git graph 将等价于执行 git log --graph --oneline --decorate

git graphgit log 命令也可以带 --all 的参数执行,这将显示所有的分支信息,而不止当前的分支。

也可以带 --stat 参数执行,它可以显示每次提交时哪些文件有修改、修改了多少行。

处理合并(merge)

当你执行 pull、进行复原操作,或者将一个分支与另一个进行合并时会需要处理合并。与其它 VCS 类似,当 Git 无法自动处理合并时,就需要使用者进行处理。

可以查看 Git Book 的这一部分讲解如何处理冲突合并。

如果你需要通过合并来还原的话,可以带 --abort 参数运行合并相关的命令,例如 git merge --abortgit pull --abortgit rebase --abort)。

使用分布式版本控制系统

The above commands only provide the basics. The real power and convenience in Git (and other distributed version control systems) come from leveraging its local commits and fast branching. A typical Git workflow looks like this:

  1. Create and check out a branch to add a feature.
  2. Make as many commits as you would like on that branch while developing that feature.
  3. Squash, rearrange, and edit your commits until you are satisfied with the commits enough to push them to the central server and make them public.
  4. Merge your branch back into the main branch.
  5. Delete your branch, if you desire.
  6. Push your changes to the central server.

创建一个分支

git branch <branch name>

can be used to create a branch that will branch off the current commit. After it has been created, you should switch to it using

git checkout <branch name>

A simpler method is to do both in one step with

git checkout -b <branch name>

To see a list of branches, and which branch is currently checked out, use

git branch

A word on commits

Many of the following commands take commits as arguments. A commit can be identified by any of the following:

  • Its 40-digit SHA-1 hash (the first 7 digits are usually sufficient to identify it uniquely)
  • Any commit label such as a branch or tag name
  • The label HEAD always refers to the currently checked-out commit (usually the head of the branch, unless you used git checkout to jump back in history to an old commit)
  • Any of the above plus ~ to refer to previous commits. For example, HEAD~ refers to one commit before HEAD and HEAD~5 refers to five commits before HEAD.

提交为检查点

In Subversion and other older, centralized version control systems, commits are permanent - once you make them, they are there on the server for everyone to see. In Git, your commits are local and you can combine, rearrange, and edit them before pushing them to the server. This gives you more flexibility and lets you use commits as checkpoints. Commit early and commit often.

编辑之前的提交

git commit --amend

allows you to modify the previous commit. The contents of the index will be applied to it, allowing you to add more files or changes you forgot to put in. You can also use it to edit the commit message, if you would like.

插入、重新排序和更改历史记录

git rebase -i <commit>

will bring up a list of all commits between <commit> and the present, including HEAD but excluding <commit>. This command allows you rewrite history. To the left of each commit, a command is specified. Your options are as follows:

  • The "pick" command (the default) uses that commit in the rewritten history.
  • The "reword" command lets you change a commit message without changing the commit's contents.
  • The "edit" command will cause Git to pause during the history rewrite at this commit. You can then modify it with git commit --amend or insert new commits.
  • The "squash" command will cause a commit to be folded into the previous one. You will be prompted to enter a message for the combined commit.
  • The "fixup" command works like squash, but discards the message of the commit being squashed instead of prompting for a new message.
  • Commits can be erased from history by deleting them from the list of commits
  • Commits can be re-ordered by re-ordering them in the list. When you are done modifying the list, Git will prompt you to resolve any resulting merge problems (after doing so, continue rebasing with git rebase --continue)

When you are done modifying the list, Git will perform the desired actions. If Git stops at a commit (due to merge conflicts caused by re-ordering the commits or due to the "edit" command), use git rebase --continue to resume. You can always back out of the rebase operation with git rebase --abort.

Warning: Only use git rebase -i on local commits that have not yet been pushed to anybody else. Modifying commits that are on the central server will cause merge problems for obvious reasons.
Note: Vim makes these rebase operations very simple since lines can be cut and pasted with few keystrokes.

Git提示符

The Git package comes with a prompt script. To enable the prompt addition you will need to source the git-prompt.sh script and add $(__git_ps1 " (%s)") to you PS1 variable.

  • Copy /usr/share/git/completion/git-prompt.sh to your home directory (e.g. ~/.git-prompt.sh).
  • Add the following line to your .bashrc/.zshrc:
source ~/.git-prompt.sh
  • For Bash:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
Note: For information about coloring your bash prompt see Color_Bash_Prompt
  • For zsh:
PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

The %s is replaced by the current branch name. The git information is displayed only if you are navigating in a git repository. You can enable extra information by setting and exporting certain variables to a non-empty value as shown in the following table:

Variable Information
GIT_PS1_SHOWDIRTYSTATE * for unstaged and + for staged changes
GIT_PS1_SHOWSTASHSTATE $ if something is stashed
GIT_PS1_SHOWUNTRACKEDFILES % if there are untracked files

传输协议

智能HTTP

Since version 1.6.6 git is able to use the HTTP(S) protocol as efficiently as SSH or Git by utilizing the git-http-backend. Furthermore it is not only possible to clone or pull from repositories, but also to push into repositories over HTTP(S).

The setup for this is rather simple as all you need to have installed is the Apache web server (with mod_cgi, mod_alias, and mod_env enabled) and of course, git:

# pacman -S apache git

Once you have your basic setup up and running, add the following to your Apache's config usually located at /etc/httpd/conf/httpd.conf:

<Directory "/usr/lib/git-core*">
    Order allow,deny
    Allow from all
</Directory>

SetEnv GIT_PROJECT_ROOT /srv/git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/

The above example config assumes that your git repositories are located at /srv/git and that you want to access them via something like http(s)://your_address.tld/git/your_repo.git. Feel free to customize this to your needs.

Note: Of course you have to make sure that your Apache can read and write (if you want to enable push access) on your git repositories.

For more detailed documentation, visit the following links:

Git SSH

You first need to have a public SSH key. For that follow the guide at Using SSH Keys. To set up SSH itself, you need to follow the SSH guide. This assumes you have a public SSH key now and that your SSH is working. Open your SSH key in your favorite editor (default public key name is ~/.ssh/id_rsa.pub), and copy its content (Ctrl+c). Now go to your user where you have made your Git repository, since we now need to allow that SSH key to log in on that user to access the Git repository. Open ~/.ssh/authorized_keys in your favorite editor, and paste the contents of id_rsa.pub in it. Be sure it is all on one line! That is important! It should look somewhat like this:

Warning: Do not copy the line below! It is an example! It will not work if you use that line!
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCboOH6AotCh4OcwJgsB4AtXzDo9Gzhl+BAHuEvnDRHNSYIURqGN4CrP+b5Bx/iLrRFOBv58TcZz1jyJ2PaGwT74kvVOe9JCCdgw4nSMBV44cy+6cTJiv6f1tw8pHRS2H6nHC9SCSAWkMX4rpiSQ0wkhjug+GtBWOXDaotIzrFwLw== username@hostname

Now you can checkout your Git repository this way (change where needed. Here it is using the git username and localhost):

git clone git@localhost:my_repository.git

You should now get an SSH yes/no question. Type yes followed by Enter. Then you should have your repository checked out. Because this is with SSH, you also do have commit rights now. For that look at Git and Super Quick Git Guide.

特定非标准端口

Connecting on a port other than 22 can be configured on a per-host basis in /etc/ssh/ssh_config or ~/.ssh/config. To set up ports for a repository, specify the path in .git/config using the port number N and the absolute path /PATH/TO/REPO:

ssh://user@example.org:N/PATH/TO/REPO

Typically the repository resides in the home directory of the user which allows you to use tilde-expansion. Thus to connect on port N=443,

url = git@example.org:repo.git

becomes:

url = ssh://git@example.org:443/~git/repo.git

Git守护进程

Note: The git daemon only allows read access. For write access see #Git SSH.

This will allow URLs like "git clone git://localhost/my_repository.git".

Edit the configuration file for git-daemon /etc/conf.d/git-daemon.conf (GIT_REPO is a place with your git projects), then start git-daemon with root privileges:

# systemctl start git-daemon@

To run the git-daemon every time at boot, enable the service:

# systemctl enable git-daemon@

Clients can now simply use:

git clone git://localhost/my_repository.git

Git版本库权限

To restrict read/write access, you can simply use Unix rights, see http://sitaramc.github.com/gitolite/doc/overkill.html

For a fine-grained rights access, see gitolite and gitosis

参见