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

From ArchWiki
Jump to: navigation, search
(update templates, see Help:Style)
(安装: 用 bc 缩减行长度)
Line 22: Line 22:
Bash 自动完成 (例如,按 tab 来完成你正在键入的命令),只需要:
Bash 自动完成 (例如,按 tab 来完成你正在键入的命令),只需要:
source /usr/share/git/completion/git-completion.bash
{{bc|source /usr/share/git/completion/git-completion.bash}}
另外,你可以安装 {{Pkg|bash-completion}} 软件包来自动为新的外壳加载自动完成。
另外,你可以安装 {{Pkg|bash-completion}} 软件包来自动为新的外壳加载自动完成。
如果你想使用 Git 内建的图形界面 (例如 {{Ic|gitk}} 或者 {{Ic|git gui}}) 你需要安装 {{Pkg|tk}} 软件包,否则你会遇到一个隐晦的错误信息:
如果你想使用 Git 内建的图形界面 (例如 {{Ic|gitk}} 或者 {{Ic|git gui}}) 你需要安装 {{Pkg|tk}} 软件包,否则你会遇到一个隐晦的错误信息:
/usr/bin/gitk: line 3: exec: wish: not found.
{{bc|/usr/bin/gitk: line 3: exec: wish: not found.}}
== 配置 ==
== 配置 ==

Revision as of 08:45, 13 February 2012

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Tango-preferences-desktop-locale.pngThis article or section needs to be translated.Tango-preferences-desktop-locale.png

Notes: please use the first argument of the template to provide more detailed indications. (Discuss in Talk:Git (简体中文)#)
Template:Article summary start

Template:Article summary text Template:Article summary heading Template:Article summary wiki: Generally about contributing to pacman, although it still serves as a practical Git tutorial Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary link Template:Article summary end

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

有一个很全面的文档,包含了手册和教程,可以从 官方网站 获得。


git 可以用 Pacman 从 [extra] 仓库安装。如果你关心使用 Git 配合其他的 VCS 软件,邮件服务器,或者使用 Git 的图形界面,注意看可选的依赖。

Bash 自动完成 (例如,按 tab 来完成你正在键入的命令),只需要:

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

另外,你可以安装 bash-completion 软件包来自动为新的外壳加载自动完成。

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

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


Git reads its configuration from a few INI type configuration files. In each git repository .git/config is used for configuration options specific to that repository. Per-user ("global") configuration in $HOME/.gitconfig is used as a fall-back from the repository configuration. You can edit the files directly but the preferred method is to use the git-config utility. For example,

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

adds editor = nano -w to the [core] section of your ~/.gitconfig file.

The man page for the git-config utility has a fairly long list of variables which can be set.

Here are some more basic configurations you will probably want:

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


Parts from everywhere, much from the wonderful tutorial here: http://schacon.github.com/git/gittutorial.html

Additionally see Super Quick Git Guide.

Pull the network scripts with

git clone http://archlinux.org/~james/projects/network.git

Update an existing clone

git pull origin

Commit changes

git commit -a -m "changelog message"

To create a new branch

git branch somebranch

Change to a different branch

git checkout differentbranch

Merge a branch to current active branch

git merge somebranch

Delete a branch

git branch -d somebranch

Push a local branch or tag to a remote repository


Delete a branch or tag in a remote repository


Diff between two branches

git diff master..somebranch

Diff between two commit IDs (found in git log)

git diff e9780c7cba2855350e914fde227a79bb63c1351d..8b014e40346b38b3b9bfc41359b4e8a68e804c0d

Diff between the last two commits

git diff HEAD^ HEAD

Patchset between two branches (follows same syntax as git diff afaik)

git format-patch master..somebranch

Or better: http://wiki.winehq.org/GitWine#head-f7a29e7ed999b5924748a60c5a1cd4a019032d26

git format-patch -o out origin

Set nano as default editor

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

Git Prompt

Git package comes with a bash completion file. This file, also contains the necessary functions to provide git information on your bash or zsh shell prompt. To enable it add $(__git_ps1 " (%s)") to you PS1 variable.

  • For bash:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  • 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

In addition you can set the GIT_PS1_SHOWUPSTREAM variable to "auto" in order to see < if you are behind upstream, > if you are ahead and <> if you have diverged.

Note: If you don't use bash completion consider sourcing /usr/share/git/completion/git-completion.bash in your .bashrc/.zshrc

Transfer Protocols

Smart 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 webserver (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

SetEnv GIT_PROJECT_ROOT /srv/git
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:


You first need to have a public SSH key. For that follow the guide at Using SSH Keys. To setup SSH itself you need to follow the SSH guide. I assume you have a public SSH key now and your SSH is working. Open your SSH key in your favorite editor (default public key name is id_rsa.pub and is located in ~/.ssh) 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 login on that user to access the GIT repository. Open this file in your favorite editor (i use nano)

nano ~/.ssh/authorized_keys

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 repo 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. Since this is with SSH you also do have commit rights now. For that look at Git and Super Quick Git Guide.

Specifying a non-standard port

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:


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


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

GIT Daemon

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 configuration file for git-dameon /etc/conf.d/git-daemon.conf (GIT_REPO is a place with your git projects), then start git-daemon with root privileges:

# /etc/rc.d/git-daemon start

To run the git-daemon every time at boot, just append git-daemon to DAEMONS line in rc.conf.

Clients can now simply use:

git clone git://localhost/my_repository.git

GIT repositories rights

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