将已经存在的项目提交到gitlab的新分支中

将已经存在的项目提交到gitlab中

在gitlab中新增用户jack

将已经存在的项目提交到gitlab的新分支中

登录jack这个git用户,然后创建仓库 mxonline

已经写好了部分功能的项目存放在 D:\>cd D:\python\mxonline\
需要推送到gitlab中
运行git_cmd.exe

# 切换到项目目录下
D:\>cd D:\python\mxonline\

# 初始化
D:\python\mxonline>git init
Initialized empty Git repository in D:/python/mxonline/.git/

# 加入主分支
D:\python\mxonline>git remote add origin https://gitlab.example.com/jack/mxonline.git

D:\python\mxonline>git add .
warning: LF will be replaced by CRLF in .idea/misc.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .idea/modules.xml.
The file will have its original line endings in your working directory

# 提交代码失败
D:\python\mxonline>git push -u origin master
To https://gitlab.example.com/jack/mxonline.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitlab.example.com/jack/mxonline.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.

# 使用账号密码的方式,照样失败
D:\python\mxonline>git remote set-url origin https://jack:jack2019@gitlab.example.com/jack/mxonline.git

# 最终的解决办法:
通过查看提示信息,我发现,是因为本地仓库和远程仓库的文件不一致所致,也就是说,github允许你本地仓库有的东西,远程仓库里没有,但不允许远程仓库有的东西,你本地仓库没有。问题找到了,解决办法就很简单了,那就是在push之前先同步一下本地仓库与远程仓库的文件。使用以下命令

D:\python\mxonline>git pull --rebase origin master
From https://gitlab.example.com/jack/mxonline
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: 'init'

D:\python\mxonline>git push -f origin master
Enumerating objects: 1552, done.
Counting objects: 100% (1552/1552), done.
Delta compression using up to 8 threads
Compressing objects: 100% (1491/1491), done.
Writing objects: 100% (1551/1551), 9.44 MiB | 3.46 MiB/s, done.
Total 1551 (delta 350), reused 0 (delta 0)
remote: Resolving deltas: 100% (350/350), done.
To https://gitlab.example.com/jack/mxonline.git
   8c83ef5..5509745  master -> master

至此,本地代码就推送到了远程仓库的master中

# 提示warning: LF will be replaced by CRLF in .idea/misc.xml.

D:\python\mxonline>git add .
warning: LF will be replaced by CRLF in .idea/misc.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .idea/workspace.xml.
The file will have its original line endings in your working directory
# 解决
D:\python\mxonline>git config --global core.autocrlf false


# 修改代码后的提交

D:\python\mxonline>git add .
D:\python\mxonline>git commit -m "update readme"
D:\python\mxonline>git -c http.sslVerify=false push origin master


# 标签

# 查看标签
D:\python\mxonline>git tag
# 命名标签
D:\python\mxonline>git tag mxonline20191016 -m "拉分支前的备份"
# 推送标签
D:\python\mxonline>git push origin mxonline20191016

# 分支的操作

# 查看分支
D:\python\mxonline>git branch
* master

# 创建名为 dns_manage的分支
D:\python\mxonline>git branch dns_manage

D:\python\mxonline>git branch
  dns_manage
* master

# 切换到 dns_manage 分支
D:\python\mxonline>git checkout dns_manage
Switched to branch 'dns_manage'
M       .idea/workspace.xml

# 修改代码,然后提交到分支中,可以看到git的web界面中就有分支了

D:\python\mxonline>git add .

D:\python\mxonline>git commit -m "add dns_manage readme"
[dns_manage a7efacc] add dns_manage readme
 2 files changed, 21 insertions(+), 14 deletions(-)
 create mode 100644 apps/dns_manage/dns_readme.md

D:\python\mxonline>git push origin dns_manage
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 782 bytes | 78.00 KiB/s, done.
Total 7 (delta 5), reused 0 (delta 0)
remote:
remote: To create a merge request for dns_manage, visit:
remote:   https://gitlab.example.com/jack/mxonline/merge_requests/new?merge_request%5Bsource_branch%5D=dns_manage
remote:
To https://gitlab.example.com/jack/mxonline.git
 * [new branch]      dns_manage -> dns_manage

# 可以从界面中看到新的分支

将已经存在的项目提交到gitlab的新分支中

上一篇:面试题3


下一篇:【hbase】检察HBase集群的一致性并修复