Git (简体中文)

From ArchWiki
Revision as of 04:12, 13 April 2013 by Zechen11 (Talk | contribs) (备忘单)

Jump to: navigation, search


附注: please use the first argument of the template to provide more detailed indications.
摘要 help replacing me
安装和使用 Git 版本管理系统
Super Quick Git Guide: Generally about contributing to pacman, although it still serves as a practical Git tutorial
Concurrent Versions System

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 从几个 INI 格式的配置文件中读取配置信息。在每一个 git 仓库中 .git/config 被用来指定与本仓库有关的配置选项。每一个用户 ("global") 的配置文件在 $HOME/.gitconfig 被用来作为仓库配置的备用配置。你也可以直接编辑文件,但是更推荐的方法是使用 git-config 工具。例如,

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

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

git-config 工具的 man page 有一个很长的参数列表,列出所以可以设置的变量。


$ git config --global "Firstname Lastname"
$ git config --global ""
$ git config --global color.ui true
$ git config --global --list



git clone <repo location> <dir>

will clone a Git repository in a new directory inside your current directory. Leaving out <dir> will cause it to name the folder after the Git repository. For example,

git clone

clones Github's mirror of the Linux kernel into a directory named "linux".


Git's commit process involves two steps:

  1. Add new files, add changes for existing files (both with git add <files>), and/or remove files (with git rm). These changes are put in a staging area called the index.
  2. Call git commit to commit the changes.

Git commit will open up a text editor to provide a commit message. You can set this editor to whatever you want by changing the core.editor option with git config.

Alternatively, you can use git commit -m <message> to supply the commit message without opening the text editor.

Other useful commit tricks:

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 add -p lets you commit specific parts of files you have changed. This is useful if you have made a bunch of changes that you think would be best split into several commits.


To push your changes up to a server (such as Github), use

git push <server name> <branch>

Adding -u will make this server the default one to push to for this 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, 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>

Similarly to push, the server name and branch should have sane defaults, so git pull should suffice. Git pull is actually shorthand for doing two things:

  1. Calling git fetch, which updates the local copy of what the server has. Such branches are called "remote" branches because they are mirroring remote servers.
  2. Calling 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.

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.


The command git log shows the history of your current branch. Note that each commit is identified by a SHA-1 hash. The author, commit date, and commit message follow. A more useful command is

git log --graph --oneline --decorate

which provides a display similar to TortoiseGit's log window. It shows the following:

  • The first 7 digits of each commit's SHA-1 hash (enough to be unique)
  • The --graph option shows how any branches (if there are others) fork off from the current branch.
  • The --oneline option shows only the first line of each commit message
  • The --decorate option shows all commit labels (branches and tags)

It may be convenient to alias this command as git graph by doing the following:

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

Now typing git graph will run git log --graph --oneline --decorate. git graph and git log may be given the --all flag in order to view all branches instead of just the current one. Adding --stat to one of these commands is also useful - it shows which files each commit changed and how many lines were changed in each file.


Merges happen when you pull, as a result of a rebase operation, and when you merge one branch into another. Like other version control tools, when Git cannot automatically merge a commit, it turns to you. See 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 --abort flag with whatever command started the merge (e.g. git merge --abort, git pull --abort, git rebase --abort).

Git Prompt

The 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 do not 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 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 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 =


url = ssh://

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:

# rc.d start git-daemon

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

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