09 Git 自定义Git
By Kevin Song
- 09-01 忽略特殊文件
- 09-02 配置别名
- 09-03 搭建Git服务器
让Git显示颜色:
$ git config --global color.ui true
09-01 忽略特殊文件
在工作区根目录下创建.gitignore文件,把需要忽略的文件名填进去,Git会自动忽略这些文件
不需要从头写.gitignore文件,GitHub已经为我们准备了各种配置文件,只需要组合一下就可以使用了。所有配置文件可以直接在线浏览
忽略文件的原则:
- 忽略操作系统自动生成的文件
- 忽略编译生成的中间文件、可执行文件等
- 一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要放进版本库(比如Java编译产生的.class文件)
- 忽略带有敏感信息的配置文件,比如存放口令的配置文件
09-02 配置别名
把 git status 改成 git st
$ git config --global alias.st status
用co表示checkout,ci表示commit,br表示branch:
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch
配置文件
Git配置文件都放在.git/config文件中:
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@github.com:michaelliao/learngit.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[alias]
last = log -1
当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中:
$ cat .gitconfig
[alias]
co = checkout
ci = commit
br = branch
st = status
[user]
name = Your Name
email = your@email.com
配置别名也可以直接修改这个文件,如果改错了,可以删掉文件重新通过命令配置。
09-03 搭建Git服务器
不想开源又不想给GitHub付钱则自己搭建Git服务器
必须是Linux系统
1 . 安装Git
$ sudo apt-get install git
2 . 创建一个git用户,用来运行git服务:
$ sudo adduser git
3 . 创建证书登录:
- 收集所有需要登录的用户的公钥(每个用户的id_rsa.pub文件)
- 把所有公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个
4 . 初始化Git仓库
- 选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令:
$ sudo git init --bare sample.git
Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,把owner改为git:
$ sudo chown -R git:git sample.git
5 . 禁用shell登录:
出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。找到类似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
改为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
这样,git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。
6 . 克隆远程仓库:
- 通过 git clone 克隆远程仓库,在各自的电脑上运行:
$ git clone git@server:/srv/sample.git
Cloning into 'sample'...
warning: You appear to have cloned an empty repository.