## 書籍 & 教程推薦
> date: 2016-10-20 17:15
> svn 和 git 都用過,我只想說 —— 請使用 git
- [GitHub 入門與實踐](http://www.ituring.com.cn/book/1581)
- [git 常用操作腦圖](http://blog.csdn.net/kehyuanyu/article/details/41278797)
- [git 簡明指南](http://www.bootcss.com/p/git-guide/)
- [git tutorial](https://try.github.io)
- [廖雪峰的git教程](http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000)
- [git 協作流程](http://kb.cnblogs.com/page/535581/)
- [版本控制入門 – 搬進 Github](http://www.imooc.com/learn/390)
- [Git實戰_201703014](https://segmentfault.com/a/1190000008579494)
- [一張圖看明白Git的四個區五種狀態](http://imtuzi.com/post/git-four-areas-five-states.html)
- [回滾錯誤合并](https://segmentfault.com/a/1190000015792394)
- [git hook 代碼檢測](https://www.longlong.asia/2015/08/08/git-pre-receive-hook.html)
大部分時候都直接使用 git bash,輕量級使用 gitk,diff、log 時使用 tortoiseGit,當然 sourceTree 也可以玩一玩
子目錄中包括 `.git` 是只會提交一個空目錄, `git status` 時要注意
- gitlab 回滾 - MR revert
- github PR
## 基礎概念
git clone 有 2 種形式:
- ssh 方式需要提前配置私鑰
- https 需要賬號密碼認證,不過可以通過如下配置自動記錄
```
git help xxx # get help
git clone git@git.coding.net:daydaygo/project.git # ssh
git clone http://git.coding.net/daydaygo/project.git # http
git config -l
# my config
git config --global user.name "daydaygo" && \
git config --global user.email "1252409767@qq.com" && \
git config --global push.default simple && \
git config --global color.ui "always" && \
git config --global credential.helper "store" && \f
git config --global format.pretty "oneline" && \
git config --global core.quotepath false && \
git config --global core.autocrlf false # 自動轉換換行符
git config --global core.safecrlf true # 混合換行符警告
git config --global core.ignorecase false # git 忽略文件名大小寫,導致 window 下修改 linux 下報錯
git config --global core.filemode false
git config --global alias.s status
git config --global -- unset alias.s
git config --global gui.encoding utf-8 # gitk 中文亂碼
```
```
git commit # --amend 修改提交信息
git log --pretty=short --graph
git log --grep='xxx' # 查詢 commit msg
git rebase -i # 壓縮歷史,比如修正拼寫錯誤
# 獲取遠程分支
git fetch
git checkout xxx
# 新增遠程倉庫
git remote add url
git push -u origin mater
git push
# 修改遠程倉庫
git remote set-url origin git://new.url.here
# git pull 沖突
git stash
git pull
git stash pop
git checkout . # 取消所有臨時修改
# 增加 tag
git pull
git tag # 輕量級tag
git tag -a xxx -m 'xxx' # 含附注tag, 推薦
git push --tags
# 刪除 tag/branch
git push origin :<name>
git push origin --delete <name>
git branch -m old new # 分支重命名
gb --set-upstream-to=origin/dev-daydaygo-didi # 本地分支和遠程分支關聯
git show [十六進制碼] # 顯示某一個特定的提交的日志
git log --graph --pretty=oneline --abbrev-commit # 查看提交圖
git ls-files -u # 查看沖突未處理的文件列表
# 修正錯誤的 commit
git log # 查看 commit id
git reset --hard e377f60e28c8b84158 # 回滾到指定的版本
// change coder
git commit -am 'xxx' --amend # 修正錯誤的 commit
git pull
// merge
git push
git rm -r --cached .idea # 配合 .gitignore 修改
git fetch -p origin # 同步刪除本地分支
# stash
git stash
git stash list
git stash pop
git stash apply
# 子模塊 submodule -> 最好不用,別增加復雜度了
# http://www.jianshu.com/p/b49741cb1347
git submodule add https://github.com/chaconinc/DbConnector # add
git clone xxx # 原項目
git submodule init
git submodule update
git rebase master # 貢獻開源代碼特別有用,可以讓 commit 看起來像一條線
git clean -xdf # 小心這條命令,gitignore 里面的文件也會被刪除掉
# 多個遠程倉庫
git remote add origin https://url
git remote set-url --add origin https://url # 再添加一個
git push --all
# git hooks 實現不允許分支合并
# .git/hooks/commit-msg
#!/c/bin/php/php
<?php
var_dump($argv);
$str = file_get_contents($argv[1]);
var_dump($str);
if (strpos($str, "Merge branch 'rtest'") !== false) {
echo "can not merge rtest \n";
exit(1);
}
# 代碼自動發布
# 方式一: git hook post-receive https://www.jianshu.com/p/9c2fcd803877
# 方式二: .git/hooks/post-push
#!/bin/sh
ssh root@xyjf-dev 'cd /data/docker; git pull'
# 全新分支
git checkout --orphan test
git clean -df
# git gc
# 查找大文件
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
# 刪除文件
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch bin/nspatientList1.txt' --prune-empty --tag-name-filter cat -- --s
# 使用 gc 再次壓縮
git gc --prune=now
# https://rtyley.github.io/bfg-repo-cleaner/
git clone --mirror git://example.com/some-big-repo.git
bfg --strip-blobs-bigger-than 100M --replace-text banned.txt repo.git
cd some-big-repo.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push
# github pr 撤銷
git remote add upstream https://github.com/hyperf-cloud/hyperf
git fetch upstream
git reset upstream/master --hard
git push -f
```
`.gitignore`:需要忽略的文件,比如項目需要使用的配置、第三方依賴的文件夾
沖突: 將每次commit用線串起來, 不同分支上面的commit就可能將直線分為2個, 當合并這2個分支就可能遇到沖突; 解決沖突實際就是修改發生沖突的文件, 修改完之后再提交一次, 這次用來合并的分支和正在使用的分支, 都將指向新的commit
標簽: 對外是軟件發布的版本;對內實際是指向某個commit點的指針
pull request: 你自己的項目, 怎么折騰都行, 如果想貢獻代碼給他人的項目, 就需要先fork版本到自己的github, 折騰完了發起一個pull request 請求
issue: 大部分都是 bug,也有部分關于軟件的改進
## github
- [Trending](https://github.com/trending)
- [subscribe](https://github.com/explore/subscribe)
- 添加github小圖標: http://shields.io/
- github 漢化:52cik/github-hans
tasklist 語法
通過提交信息操作 issue
給特定 issue 提交代碼會轉化為 pull request
url 的用處:不同版本對比;同一分支不同時間對比;獲取 diff、patch 格式文件
評論:使用 R 鍵引用選中的評論;使用 : 來使用表情
大部分軟件使用 MIT 協議; MIT協議可以和其他許可協議共存 -- 基本就是在說使用 MIT 就沒錯,不行之后再多加協議it
### 快捷鍵
跳轉面板:gc goto-code
文件搜索(code 面板下):t,類似 sublime,可以模糊匹配
聚焦到搜索框:s
搜索過濾
引用 issue:#
代碼段選擇
顯示快鍵鍵:?
### github api
ok.sh: https://github.com/whiteinge/ok.sh
```
cp ok.sh /usr/bin/gh
gh delete_repo daydaygo test
gh fork_repo swoft-cloud swoft
```
## svn
> 這年頭還有人在用 svn, 但是也沒辦法呀
```
svn h/? # help
svn log # 看 log 還是圖形化客戶端更靠譜
svn di # diff
svn st # status
svn revert -R . # 取消未提交的修改
svn add
svn ci # commit
svn rm
svn merge
```