环境
Linux Mint 17.3
git version 2.7.4
设置:
1.生成密钥对
penn@Linux-Mint18 ~ $ ssh-keygen -t rsa -C "xxxx@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/penn/.ssh/id_rsa):
Created directory '/home/penn/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/penn/.ssh/id_rsa.
Your public key has been saved in /home/penn/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxx xxxx@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
| ooo|
| . . . ..|
| . . . o . o .|
|E . .. o . + |
| . ...S o . |
| . =.+. o.|
| + @.X . *|
| . =.= +.B.o.|
| . . ..++o |
+----[SHA256
penn@Linux-Mint18 ~ $ cd .ssh
penn@Linux-Mint18 ~/.ssh $ ls
id_rsa id_rsa.pub
penn@Linux-Mint18 ~/.ssh $ cat id_rsa.pub
ssh-rsa xxxxxxxxxxxxxxxxxxxxxx xxxx@gmail.com
2.将公钥添加到github
3.检查配置结果
penn@Linux-Mint18 ~/.ssh $ ssh git@github.com
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxx
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
4.git全局配置
penn@Linux-Mint18 ~/.ssh $ git config --global user.name "xxxx"
penn@Linux-Mint18 ~/.ssh $ git config --global user.email "xxxx@gmail.com"
5.创建本地git目录
penn@Linux-Mint18 ~/.ssh $ cd
penn@Linux-Mint18 ~ $ cd Desktop/
penn@Linux-Mint18 ~/Desktop $ cd github_projects/
penn@Linux-Mint18 ~/Desktop/github_projects $ ls
penn@Linux-Mint18 ~/Desktop/github_projects $ mkdir github_hexo
penn@Linux-Mint18 ~/Desktop/github_projects $ ls
github_hexo
penn@Linux-Mint18 ~/Desktop/github_projects $ cd github_hexo/
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ ls
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git init
初始化空的 Git 仓库于 /home/penn/Desktop/github_projects/github_hexo/.git/
6.添加github远程仓库
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git remote add github.io https://github.com/xxxx/xxxx.github.io.git
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git remote
github.io
7.拉取github项目到本地git目录
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git fetch github.io
来自 https://github.com/xxxx/xxxx.github.io
* [新分支] master -> github.io/master
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git status
位于分支 master
8.提交新文件到remote
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ touch test.md
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git status
位于分支 master
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
test.md
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git add .
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git commit -m "test"
[master 0fe3106] test
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.md
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git status
位于分支 master
无文件要提交,干净的工作区
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git push -u github.io master
Username for 'https://github.com': xxxx
Password for 'https://xxxx@github.com':
To https://github.com/xxxx/xxxx.github.io.git
! [rejected] master -> master (non-fast-forward)
error: 无法推送一些引用到 'https://github.com/xxxx/xxxx.github.io.git'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。
9.解决冲突
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git merge github.io/master -m "merge local to remote"
Merge made by the 'recursive' strategy.
2016/06/12/hugo 1 use it in win7/index.html | 753 ++++++++++++++++++
2016/06/14/hugo 2 update sidebar/index.html | 782 ++++++++++++++++++
...
...
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ ls
2016 archives categories commonweal favicon_32X32.ico images js README.md sitemap.xml test.md
404.html atom.xml CNAME css google6f9ffd2313a65622.html index.html page robots.txt tags vendors
10.再次push到remote,成功
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $ git push -u github.io master
Username for 'https://github.com': xxxx
Password for 'https://xxxx@github.com':
对象计数中: 10, 完成.
Delta compression using up to 8 threads.
压缩对象中: 100% (5/5), 完成.
写入对象中: 100% (10/10), 1.05 KiB | 0 bytes/s, 完成.
Total 10 (delta 1), reused 2 (delta 0)
To https://github.com/xxxx/xxxx.github.io.git
8d2d9fa..accb66c master -> master
分支 master 设置为跟踪来自 github.io 的远程分支 master。
penn@Linux-Mint18 ~/Desktop/github_projects/github_hexo $
11.git log
可以看当前git仓库的修改记录
git log 文件名 可以看指定文件或者文件夹的log
也可以在指定的文件夹内使用
git log ./ 也可以看执行此命令的文件夹的修改记录
git show 查看某次commit的修改内容
git log -p 查看某个文件的修改历史
git log -p -2查看最近2次的更新内容
git log –stat 查阅最近的提交修改
12.在某些情况下我们可能希望查看目标文件两个版本之间的差异。 查看这个文件任意两个版本的差异:
git diff <hashcode-before-right> <hashcode> <filename>
13.团队开发或者做Code Review的时你可能需要用到:
查看某个文件的包含提交人员,日期、版本号等记录信息,不包括修改详情:
git whatchanged <filename>
14.在review时查看某次提交修改详情:
git show <hashcode>
15.我们打算查看某个文件在某次提交中的修改:
git show <hashcode> <filename>
我们分析后发现不是这几次的修改造成的,所以我们希望查看这个文件所有的提交记录。
查看仅这个文件的所有历史记录:
git log --pretty=oneline <filename>
16.gitignore的使用
S1: touch .gitignore #创建gitignore隱藏文件
S2: vim .gitignore #编辑文件,加入指定文件
#下面是我的gitignore文件的内容
#忽略gitignore文件
.gitignore
#忽略后缀名为.o和.a的文件
*.[oa]
#显示指定忽略名称为main的文件
main
文件.gitignore的格式规范:
A:#为注释
B:可以使用shell所使用的正则表达式来进行模式匹配
C:匹配模式最后跟"/"说明要忽略的是目录
D:使用!取反(例如目录中包含 test.a,并且gitignore文件中包含 *.[oa],如果在文件中加入 !test.a 表明忽略除test.a文件以外的后缀名为.a或者.o的文件)