设为首页 收藏本站
查看: 799|回复: 0

[经验分享] git简单使用

[复制链接]

尚未签到

发表于 2018-1-12 14:19:36 | 显示全部楼层 |阅读模式
  公司内部代码管理使用git,简单使用一下,还蛮好的。
  我简单的理解是:在本地建立文件夹,可以放project,里面什么file、dir都可以放,放完了之后,提交到git里,可以供展示,或者是别人下载更新之类。高级的用法目前没用到。
  1.全局设置
  

Git global setup  

  
git config
--global user.name "xuecheng.hu"  
git config
--global user.email "hxc@aispeech.com"  

  2.建仓提交
  

Create a new repository  

  
git clone https:
//gitlab.spetechcular.com/xuecheng.hu/test2.git //当前git上的地址  
cd test2    //已经克隆到本地目录了,直接进去
  
touch README.md  //各种花里胡哨操作,可以把自己的代码移进来,只要不提交,这就相当于本地文件夹
  
git add README.md     //提交
  
git commit -m "add README"  // 提交时的评论,方便看,这提交的是个啥东西
  
git push -u origin master   // 推送到master的这个分支,一般情况下都是这个,可以换别的
  

  以上我的事就完成了,下面的我都没怎么看,用到再说~~

常用操作
  这里只是流水账似的列出常用的几个操作, 要想真正用得熟练, 还得多看文档掌握重要的git命令如branch, checkout, push, pull, merge, fetch, reset等

创建新分支
  

$ git branch <branch-name>  

  

签出分支
  也叫切换分支
  

$ git checkout <branch-name>  

  

列出所有branch
  标星号的是当前branch
  

$ git branch -a  

  

删除本地分支
  

$ git branch -d <local-branch-name>  

  

删除远程分支
  

$ git push origin --delete <remote-branch-name>  

  

clone一个分支
  

$ # 为了规避502错误或跟踪其它分支, 若出现502再使用--depth 1  
$ git clone https://gitlab.example.com/shun.zhang/aispeech-git-help.git -b <branch-name> [--depth 1]
  

  
$ # 再增加跟踪一个分支
  
$ git pull origin <branch-name>:<branch-name>
  
$ echo <branch-lastest-commit> > .git/refs/remotes/origin/<branch-name>
  

  

查看本地修改
  

$ git diff  
$ git difftool  # diff和difftool都可以
  

  

提交本地修改
  

$ git commit -m "commit log" <file-pattern> # 提交部分  
$ git commit -m "commit log" -a             # 提交所有
  

  

推送到远程仓库
  

$ git push origin <branch-name>  

  

回滚
  恢复到某个文件到最近一次提交的版本/取消某个文件的本地修改
  

$ git checkout <file-name>  

  

  恢复某个文件到历史版本
  

$ git log <file-name>                   # 查看日志中的commit-number  
$ git reset <commit-number> <file-name>
  
$ git checkout <file-name>
  

  

  撤消本地commit(本地commit了, 但未push到服务端)
  

git reset --hard <commit-number>  

  

同步当前分支的更新
  例如当前feature分支还在开发中, 其它小伙伴向当前提交了代码\ 不建议使用pull命令, 因为它隐藏了一些细节, 这里用fetch & merge命令替代, 每一步心里有数
  

$ git fetch origin <branch-name>              # 下载最新的代码到远程跟踪分支, 即origin/<branch-name>  
$ git difftool <branch-name> origin/<branch-name> # 查看更新内容, 看看而已, 不要在这一步执行手工merge
  
$ git merge origin/<branch-name>              # 尝试合并远程跟踪分支的代码到本地分支
  
$ git mergetool                               # 借助mergetool解决冲突
  

  

同步其它分支的更新
  例如当前feature分支还在开发中, master上已经有了更新的代码, 和上例同步当前分支更新的步骤类似
  

$ git fetch origin master  
$ git difftool <branch-name> origin/master
  
$ git merge origin/master
  
$ git mergetool
  

  
$ # git merge --abort # 回到merge前的状态
  

  

fork后同步上游仓库的更新
  如下示例里假设你fork了shun.zhang/aispeech-git-help, 你想享受到上游的更新
  

$ git remote -v # 查看远程仓库  
origin https://gitlab.example.com/li.li/aispeech-git-help.git (push)
  
origin https://gitlab.example.com/li.li/aispeech-git-help.git (fetch)
  

  
$ git remote add upstream https://gitlab.example.com/shun.zhang/aispeech-git-help.git # 第一次需要添加上游仓库
  
$ git remote -v
  
origin  https://gitlab.example.com/li.li/aispeech-git-help.git (push)
  
origin  https://gitlab.example.com/li.li/aispeech-git-help.git (fetch)
  
upstream    https://gitlab.example.com/shun.zhang/aispeech-git-help.git (push)
  
upstream    https://gitlab.example.com/shun.zhang/aispeech-git-help.git (fetch)
  

  

  
$ git fetch upstream # 和上例同步当前分支更新的步骤类似了
  
$ git difftool <branch-name> upstream/master
  
$ git merge upstream/master
  
$ git mergetool
  

  

引用公共代码
  代码引用在git上有两种方式: submodule和subtree, 推荐使用subtree方式, submodule需要每个人都有公共项目的权限, 而subtree只需要一个有权限访问公共库的人来维护就行了, 对团队其它成员是透明的\ 虽然可以直接引用代码库, 但是基础项目尽量对外发布二进制库, 而不是开放源代码给其它项目直接引用
  

$ # 第一次初始化  
$ git remote add -f <remote-subtree-repository> <remote-subtree-repository-url>
  
$ git subtree add --prefix=<local-directory> <remote-subtree-repository> <remote-subtree-branch-name> --squash
  

  
$ # 同步subtree的更新
  
$ git fetch <remote-subtree-repository> <remote-subtree-branch-name>
  
$ git subtree pull --prefix=<local-directory> <remote-subtree-repository> <remote-subtree-branch-name>
  

  

project管理

create
  在网页上创建, 默认限制一个用户最多创建3个项目, 如需创建更多项目请与管理员联系

fork
  只有当你有某个项目的读权限但是没有写权限时才需要fork, 这在开源界用的多, 但在公司内部基本上都是private项目, 所以使用不多

permission
  所有项目默认是private, 即只对该项目组内的成员开放权限, 项目的owner(默认是创建者)负责项目的访问权限设置, 并且可以对每一个成员设置权限

group
  有时一个项目比较大, 会有很多子项目, 这时可以专门为这个大项目创建一个分组, 例如aios分组下有aios-core, aios-for-car, 语音内核core分组下有asr-cloud, asr-local等
  如需创建group请与管理员联系

transfer
  可以将一个项目过户给另一个用户, 比如将 shun.zhang 创建的 aispeech-git-help 过户给 li.li, 过户后的地址就变成了 https://gitlab.example.com/li.li/aispeech-git-help

常见问题


  • 用什么编辑markdown文本 -- dillinger 是优秀的markdown在线编辑器
  • gitlab clone时报502错误 -- 加参数--depth 1
  • Your branch and 'origin/master' have diverged -- 执行git pull --rebase
  • 支持TOC -- README.MD第一行使用标签[[_TOC_]], gitlab支持TOC

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434267-1-1.html 上篇帖子: DotNetCore跨平台~xUnit生成xml报告 下篇帖子: 跪着行走的boy
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表