ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 书籍 & 教程推荐 > 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 ```