Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名

1.gitlab基本概念

GitLab是一个基于 Web 的 Git 仓库管理工具,且具有wiki 和 issue 跟踪功能。GitLab 由 GitLab Inc. 开发,使用开源许可证。
GitLab 由乌克兰程序员 Dmitriy Zaporozhets 和 Valery Sizov 开发。它由 Ruby 写成。后来,一些部分用Go语言重写。截止2016年12月,该公司有150名团队成员,以及1400多名开源贡献者。 GitLab被 IBM,Sony,Jülich Research Center,NASA,Alibaba,Invincea,O’Reilly Media,Leibniz-Rechenzentrum (LRZ),CERN,SpaceX 等组织使用。

2.gitlab库创建

Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名

点击“New project”

Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名

按照上面步骤,最后点击“Create project”

3.文件提交
客户端上(mac本的远程终端上)进行如下操作
Git global setup
git config –global user.name “wtf”
git config –global user.email “wtf@linuxmi.com”
git clone ssh://git@gitlab.linuxmicom:19234/linux/linux_datagrand.git
cd linux_datagrand
echo “this is a test file.”  >  wtf.txt
cat wtf.txt
this is a test file !
git add .
git commit -m “add a file named wtf.txt”
git push -u origin master
这样就可以将客户端文件上传到gitlab的仓库里了。

4.文件抓取
如果我在gitlab上linux_datagrand.git这个仓库里进行如下操作:
添加shiyan.txt和目录test这两个文件,那么我在客户端应该怎么操作才能将新增的文件“拉取”到本地呢?
cd linux_datagrand
git pull 或 git pull –rebase origin master
说明:建议使用git pull,原因我会在下文说明。
cat linux_datagrand
wtf.txt  shiyan.txt  test

5.在存在的文件下进行文件递交
如果我在终端上有个目录文件existing_folder,里面的文件需要递交至git库,而你又不想cp到目标库再push,那么你可以这样做:
cd existing_folder
git init
git remote add origin ssh://git@gitlab.linuxmi.com:19234/linux/linux_datagrand.git
git add .
git commit -m “Initial commit”
在push之前先进性pull操作:
git pull –rebase origin master
如果不进行pull这一步,会报如下错误
error: failed to push some refs to git。
然后:
git push -u origin master

6.库的迁移或备份
##如我想把linux_datagrand.git这个库里的文件迁移至linuxmi.git这个新库中,要求如下:
(1)迁移的时候就要考虑到已有的分支;
(2)保留以前的提交记录;
(3)不要把本地的代码直接提交到gitLab,这样以前提交的记录和分支就都没有了。
##操作如下:
git clone ssh://git@gitlab.linuxmicom:19234/linux/linux_datagrand.git
cd linux_datagrand
ls
name.txt  shiyan.txt
##查看当前的远程仓库:
git remote -v
origin  ssh://git@gitlab.linuxmicom:19234/linux/linux_datagrand.git (fetch)
origin  ssh://git@gitlab.linuxmicom:19234/linux/linux_datagrand.git (push)
##添加linuxmi.git这个远程仓库
git remote add test ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git
说明:添加远程仓库的格式
git remote add  仓库名字  [仓库地址]
##查看当前的远程仓库:
git remote -v
origin  ssh://git@gitlab.linuxmicom:19234/linux/linux_datagrand.git (fetch)
origin  ssh://git@gitlab.linuxmicom:19234/linux/linux_datagrand.git (push)
test    ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git (fetch)
test    ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git (push)
##把本地的分支push到远程仓库
git push -u test master
这个时候有报错,内容如下:
error: failed to push some refs to ‘ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.
##解决方法
(1)由于GitLab一些分支默认被保护,仅仅拥有master级别的用户才能提交到保护分支,而且master分支默认是保护分支,其他用户需要通过合并issue请求来提交上去。所以我们先关闭这个master分支保护: Project: “Settings” -> “Repository” -> “Protected Branches(Expand)”  -> “Unprotect”。
(2)使用命令:git push -f test master
所以把本地的分支push到远程仓库命令:
git push -f test master
##这个时候,我们已经把linux_datagrand.git这个库里的文件迁移至linuxmi.git这个新库中了

7.库的回收和重命名
有的时候库迁移完成之后,老的库就不需要了,这个时候就需要我们去回收或重名了,接着上面的实例说明:
要求如下:
(1)我已经把linux_datagrand.git这个库里的文件迁移至linuxmi.git这个新库中了,那么我不想再使用git clone ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git 建立本地库;
(2)我想把linux_datagrand.git这个本地库更改为linuxmi.git
操作如下:
cd linux_datagrand
##先删除原先的origin
git remote remove origin
##查看当前远程仓库
git remote -v
test    ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git (fetch)
test    ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git (push)
##我们一般都习惯使用origin,所以更改一下test这个名称
命令格式:
git remote rename <old> <new>
git remote rename test origin
##再查看当前远程仓库
git remote -v
origin    ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git (fetch)
origin    ssh://git@gitlab.linuxmicom:19234/linux/linuxmi.git (push)

8.git pull 和 git pull –rebase
##说明:
Git 作为分布式版本控制系统,所有修改操作都是基于本地的,在团队协作过程中,假设你和你的同伴在本地中分别有各自的新提交,而你的同伴先于你 push 了代码到远程分支上,所以你必须先执行 git pull 来获取同伴的提交,然后才能 push 自己的提交到远程分支。而按照 Git 的默认策略,如果远程分支和本地分支之间的提交线图有分叉的话(即不是 fast-forwarded),Git 会执行一次 merge 操作,因此产生一次没意义的提交记录,从而造成了递交图像的混乱。
##解决:
其实在 pull 操作的时候,,使用 git pull –rebase 选项即可很好地解决上述问题。 加上 –rebase 参数作用是,提交线图有分叉的话,Git 会 rebase 策略来代替默认的 merge 策略。 使用 rebase 策略有什么好处呢?借用一下 man git-merge 中的图就可以很好地说明清楚了。
假设提交线图在执行 pull 前是这样的:

Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名

如果是执行 git pull 后,提交线图会变成这样:

Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名

结果多出了 H 这个没必要的提交记录。如果是执行 git pull –rebase 的话,提交线图就会变成这样:

Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名

F G 两个提交通过 rebase 方式重新拼接在 C 之后,多余的分叉去掉了目的达到。

##结论:
大多数时候,使用 git pull –rebase 是为了使提交线图更好看,从而方便 code review。
不过,如果你对使用 git 还不是十分熟练的话,我的建议是 git pull –rebase 多练习几次之后再使用,因为 rebase 在 git 中,算得上是『危险行为』。

另外,还需注意的是,使用 git pull –rebase 比直接 pull 容易导致冲突的产生,如果预期冲突比较多的话,建议还是直接 pull。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 软件 Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名 https://www.lizi.tw/soft/10728.html

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Git/Gitlab抓取,提交,库的迁移/备份及回收/重命名-海报

分享本文封面