git
##startup
配置文件
- /etc/.gitconfig 最顶级
- /home/$USERNAME/.gitconfig 用户设定
- project/.gitconfig 当前项目
下面会把上面的设定覆盖.
git config --global user.name "XXX"
修改/home/username configgit config (--local) user.name "YY"
修改当前项目下的gitconfiggit config --system user.name "ZZZ"
系统通用配置文件
git config --global|system|local --get user.name -> XXX
git config user.name
简写
##2ndgit init
-> generate .git filegit add
-> 可以跟踪新文件,也可以将修改文件放入暂存区
gitignore遵循glob匹配
#
之后是注释- 名字后面是/表示目录
- !表示取反
- *匹配一个或多个字符
- [abc]匹配abc中任意一个字符
- ?表示任意一个字符
- [0-9], [a-z] 两个字符之间
git diff
比较暂存和未暂存的区别git diff --cached
比较暂存和上次提交的区别
git commit
提交暂存区git commit -a
跳过git add 步骤,跳过暂存区, 直接提交git rm
删除跟踪的某个文件,如果文件已在暂存区需要-f
,该命令同时删除文件git rm --cached
不删除文件,只删除暂存去或者跟踪git mv
重命名
git log
: -p
显示差异 -2
最近两次log --name-status
显示文件状态 --relative-data
显示诸如几周前这种相对时间 --pretty=oneline|short|full|fuller|format
定制显示格式git log --since=2.weeks
两周以内 --since="2015-08-01"
指定日期之后 --until="2015-08-01"
指定日期之前git log --author=XXX --grep= sometest --all-match(与关系)
###gitk
git log 图形化界面
git --amend
修改上一次提交,如果上一次commit落下了一些文件没有提交可以在commit之后 git add等操作之后amend,之产生一次提交
git reset HEAD <filename>
移除暂存区git checkout --<filename>
撤销为加入暂存区的文件的修改
git remote -v
显示远程仓库git tag (-l "0.4.*|[0-9]")
显示特定标签 glob匹配
分支的原理
pro git
建立分支步骤:git branch (-v)
列出所有分支git branch branchname
(建立) -> git checkout branchname
(跳到对应branch).
以上等同于git checkout -b branchname
git branch -d branchname
删除分支git branch --merged(--no-merged)
##搭建git服务器
以下例子是在我自己的linode的vps上搭的步骤
由于买的vps自带ssh-server. 不需要安装ssh-server
如果是自己的需要执行(ubuntu下)1
2
3sudo apt-get install openssh-server
sudo /etc/init.d/ssh start(sshd start)
sudo apt-get install git
centos用yum安装
安装ssh_server. 客户端默认安装了ssh_client.
安装完成后可以通过shell1
ssh username@ip
的方式登陆远程服务器
如果提示connect to host xxx.xxx.xxx.xxxport 22: Connection refused
说明openssh_server没安装成功或者没有start
1 | youthy@youthy:~$ ssh root@XXX.XXX.XXX.XXX |
输入exit退出ssh
接下来创建需要传到服务器上的文件(例子用是博客的源文件)1
2
3git clone --bare https://github.com/youthy/youthy.github.io test.git
youthy@youthy:~$ ls test.git/
branches config description HEAD hooks info objects packed-refs refs
--bare 用来创建裸版本库。git clone --bare REPOS NAME.git
REPOS 可以为本地文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16youthy@youthy:~$ scp -r test.git test@xxx.xxx.xxx.xxx:/home/test
test@xxx.xxx.xxx.xxx's password:
HEAD 100% 23 0.0KB/s 00:00
packed-refs 100% 98 0.1KB/s 00:00
prepare-commit-msg.sample 100% 1239 1.2KB/s 00:00
pre-rebase.sample 100% 4898 4.8KB/s 00:00
commit-msg.sample 100% 896 0.9KB/s 00:00
post-update.sample 100% 189 0.2KB/s 00:00
pre-applypatch.sample 100% 398 0.4KB/s 00:00
pre-push.sample 100% 1352 1.3KB/s 00:00
applypatch-msg.sample 100% 452 0.4KB/s 00:00
pre-commit.sample 100% 1642 1.6KB/s 00:00
update.sample 100% 3611 3.5KB/s 00:00
exclude 100% 240 0.2KB/s 00:00
description 100% 73 0.1KB/s 00:00
pack-b7cb7743297b04547b0401b940914333ed2d513b.pack 100% 3025KB 504.2KB/s 00:06
此时所有安装git的用户可以通过输入密码的方式git clone test@XXX.XXX.XXX.XXX:/home/test/test.git
克隆这个库和修改这个库,
####免密码
为了在不用每次提交都输入密码
需要将自己的.ssh/id_rsa.pub 公钥加入到test/.ssh/authorize_keys 中,如果没有需要自己建立。没有公钥的话,用ssh-kegen (-t rsa|dsa)
生成(默认时dsa加密方式)
但是需要注意以下两点
authorize_keys需要权限是600 chmod 600 test/.ssh/authorized_keys
将自己的私钥加入到ssh_agent中:在客户端执行ssh-add ~/.ssh/id_rsa
否则会出现Agent admitted failure to sign using the key.
无法免密码登陆
有时候github上面自己的项目虽然加入了公钥,但是每次push仍需要密码
那是因为这个项目clone时用的时https方式。1
2
3youthy@youthy:~/hexoblog$ git remote -v
origin https://github.com/youthy/blogfiles.git (fetch)
origin https://github.com/youthy/blogfiles.git (push)
通过git remote rm origin
的方式删掉在添加ssh方式1
2
3
4youthy@youthy:~/hexoblog$ git remote add origin git@github.com:youthy/blogfiles.git
youthy@youthy:~/hexoblog$ git remote -v
origin git@github.com:youthy/blogfiles.git (fetch)
origin git@github.com:youthy/blogfiles.git (push)
####安全措施:
以上完成之后参与者都可以通过ssh test@XXX.XXX.XXX.XXX
登陆我的vps,可以通过以下方式
1 | [root@li1166-59 ~]# which git-shell |
passwd文件内容如下1
2
3
4...
NTP:X:38:38::/ETC/NTP:/SBIN/NOLOGIN
test:x:500:500::/home/git:/bin/bash
...
将 test用户的bin/bash改为git-shell或者去掉1
test:x:500:500::/home/git:/usr/bin/git-shell
这样就无法通过test登陆服务器
更多参考
pro git