新手Git实用指导

Abstract: This introduction is a tip post for the coding beginners. You may not use GitHub as the server but it is always being nice to have your codes titled with a source control. Not only for somebody else but also yourself in the feature work. html

若是你第一次接触 Git, 那么这是我见过的最好的 Git 初学者指导. 只有一页并且是中文的.
http://rogerdudler.github.io/git-guide/index.zh.htmlgit

初次运行 Git 前的配置

通常在新的系统上,咱们都须要先配置下本身的 Git 工做环境。配置工做只需一次,之后升级时还会沿用如今的配置。固然,若是须要,你随时能够用相同的命令修改已有的配置。github

Git 提供了一个叫作 git config 的工具(译注:实际是 git-config 命令,只不过能够经过 git 加一个名字来呼叫此命令。),专门用来配置或读取相应的工做环境变量。而正是由这些环境变量,决定了 Git 在各个环节的具体工做方式和行为。这些变量能够存放在如下三个不一样的地方:ide

/etc/gitconfig 文件:系统中对全部用户都广泛适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。工具

~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。post

当前项目的 Git 目录中的配置文件(也就是工做目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每个级别的配置都会覆盖上层的相同配置,因此 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。
在 Windows 系统上,Git 会找寻用户主目录下的 .gitconfig 文件。主目录即 $HOME 变量指定的目录,通常都是 C:\Documents and Settings\$USER。此外,Git 还会尝试找寻 /etc/gitconfig 文件,只不过看当初 Git 装在什么目录,就以此做为根目录来定位。fetch

用户信息

第一个要配置的是你我的的用户名称和电子邮件地址。这两条配置很重要,==每次 Git 提交时都会引用这两条信息==,说明是谁提交了更新,因此会随更新内容一块儿被永久归入历史记录:ui

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

若是用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,之后你全部的项目都会默认使用这里配置的用户信息。若是要在某个特定的项目中使用其余名字或者电邮,只要去掉 --global 选项从新配置便可,新的设定保存在当前项目的 .git/config 文件里。url

查看配置信息
要检查已有的配置信息,可使用 git config --list 命令:code

$ git config --list
user.name=Scott Chacon
user.email=schacon@gmail.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto

有时候会看到重复的变量名,那就说明它们来自不一样的配置文件(好比 /etc/gitconfig 和 ~/.gitconfig),不过最终 Git 实际采用的是最后一个。

也能够直接查阅某个环境变量的设定,只要把特定的名字跟在后面便可,像这样:

$ git config user.name
Scott Chacon

Creat a local repo linked to a remote repo

$ rm -rf Matlab-Tests/
$ mkdir Matlab-Tests
$ cd Matlab-Tests/

$ git init
$ git remote add origin https://github.com/xia50/Matlab-Tests.git
$ git fetch

Git branch config

If you often merge with the same branch, you may want to
use something like the following in your configuration file:
    [branch "master"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

For example: do$ cd .git/ first, then $ subl config

[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "master_v5"]
    remote = origin
    merge = refs/heads/master_v5
相关文章
相关标签/搜索