工作中,主要以svn为主,对svn也比较热衷。不过随着工作时间的推进,git和svn对比,越来越觉得git更舒服和方便,特别是它的分支管理。在此,详细整理一份git的知识汇总,方便以后查阅。

  • git安装
  • 以centOS为例,直接系统命令安装即可。

    root [~] yum install git

    安装好以后,输入git会显示简单的帮助提示。

    root [~] git
    usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
               [-p|--paginate|--no-pager] [--no-replace-objects]
               [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
               [--help] COMMAND [ARGS]

    The most commonly used git commands are:
       add        Add file contents to the index
       bisect     Find by binary search the change that introduced a bug
       branch     List, create, or delete branches
       checkout   Checkout a branch or paths to the working tree
       clone      Clone a repository into a new directory
       commit     Record changes to the repository
       diff       Show changes between commits, commit and working tree, etc
       fetch      Download objects and refs from another repository
       grep       Print lines matching a pattern
       init       Create an empty git repository or reinitialize an existing one
       log        Show commit logs
       merge      Join two or more development histories together
       mv         Move or rename a file, a directory, or a symlink
       pull       Fetch from and merge with another repository or a local branch
       push       Update remote refs along with associated objects
       rebase     Forward-port local commits to the updated upstream head
       reset      Reset current HEAD to the specified state
       rm         Remove files from the working tree and from the index
       show       Show various types of objects
       status     Show the working tree status
       tag        Create, list, delete or verify a tag object signed with GPG
                 
    See 'git help COMMAND' for more information on a specific command.

    客户端安装,推荐sourcetreeapp

  • config配置
  • git的配置存放在三个地方,按照优先级别的高低排序依次是仓库配置 > 全局配置 > 系统配置,因此仓库配置的优先级最高。

    仓库配置:每一个git项目的路径下都有一个.git/,此目录里面的config文件便是该仓库配置。
    全局配置:指linux用户级别配置,就像linux的.bashrc文件一样,全局配置存放在用户的home目录下面,~/.gitconfig便是全局配置。
    系统配置:系统配置的作用是,管理员统一设置,方便所有用户同时生效,配置文件在/etc/gitconfig。

    在使用git之前,我们需要设置用户的信息,这样每次提交代码时,都会记录下是哪个用户提交的。

    root [~] git config --global user.name "web"
    root [~] git config --global user.email "web@web.web"

    这个会被记录到全局配置中:

    root [~] cat ~/.gitconfig
    [user]
            name = web
            email = web@web.web

    此时,我们创建的git项目,都会继承这个用户信息,如果想对某个项目进行单独设置用户,则可以去掉--global,并在项目目录中进行操作:

    root [~] mkdir test
    root [~] cd test
    root [~] git init
    Initialized empty Git repository in /root/test/.git/
    root [~] cat .git/config
    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    root [~] git config user.name "web2"
    root [~] cat .git/config            
    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [user]
            name = web2

    有些配置希望可以对linux所有用户都生效,那么就可以修改系统配置进行完成:

    #设置vimdiff作为git的比较工具
    root [~] git config --system merge.tool vimdiff
    #模拟svn命令,st代表status
    root [~] git config --system alias.st status
    root [~] cat /etc/gitconfig
    [alias]
            st = status
    [merge]
            tool = vimdiff
  • 常用配置信息
  • 以global为例子,仓库、全局、系统因个人需求而定。

    设置git用户信息
    git config --global user.name [username]
    git config --global user.email [email]
    开启颜色
    git config --global color.ui true
    常用命令缩写
    git config --global alias.co checkout
    git config --global alias.ci commit
    git config --global alias.st status
    git config --global alias.br branch
    文本编辑器采用Emacs
    git config --global core.editor emacs
    设置vimdiff作为git的比较工具
    git config --global merge.tool vimdiff

    具体详细的config可以参考网址:https://git-scm.com/docs/git-config.html