windows/centos系统将已经存在的项目提交到gitlab的新分支中
将已经存在的项目提交到gitlab中
在gitlab中新增用户jack
登录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
# 可以从界面中看到新的分支
centos系统示例:
apollo相关的代码有点问题,需要把https://gitee.com/reblue520/dubbo-demo-service-and-web/tree/master/dubbo-demo-web-apollo 作为单独的仓库存在
1.在gitee.com中新建dubbo-demo-web-apollo01的仓库
2.下载https://gitee.com/reblue520/dubbo-demo-service-and-web/tree/master/dubbo-demo-web-apollo代码到本地的linux服务器中,放在 /data/dubbo-demo-web-apollo 目录下
3.设置ssh的代码(网络环境原因,需要设置ssh通过web代理能连接到gitee.com)
# 安装代理工具
# yum install -y connect-proxy
# 配置代理
[root@server01 ~]# vim ~/.ssh/config
Host gitee.com *.gitee.com
ProxyCommand connect-proxy -H 10.11.0.148:808 %h %p
4.将本地centos系统server01的公钥拷贝到gitee.com中
# 生成公钥
[root@server01 dubbo-demo-web-apollo]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:lfVwX75NlIU0qe3LjcuyUtC25V+eI431I8iu2jecHKo root@server01
The key's randomart image is:
+---[RSA 2048]----+
| o.+o*|
| o +o*.|
| o. o. +|
| .. + o.o|
| S o = ..|
| .o o..|
| =.+.+*+|
| ...O.=+==|
| Eoo+ooo=.o|
+----[SHA256]-----+
# 拷贝公钥到gitee.com
# cat ~/.ssh/id_rsa.pub
5.进行配置的初始化和代码推送
git config --global user.name "reblue520"
git config --global user.email "reblue520@163.com"
git init
git remote add origin git@gitee.com:reblue520/dubbo-demo-web-apollo01.git
git add .
git config --global core.autocrlf false
git branch
[root@server01 dubbo-demo-web-apollo]# git commit -m "update readme"
[master (root-commit) fead324] update readme
13 files changed, 407 insertions(+)
create mode 100644 README.md
create mode 100644 dubbo-api/pom.xml
create mode 100644 dubbo-api/src/main/java/com/od/dubbotest/api/HelloService.java
create mode 100644 dubbo-client/pom.xml
create mode 100644 dubbo-client/src/main/java/application.properties
create mode 100644 dubbo-client/src/main/java/com/od/dubbotest/Application.java
create mode 100644 dubbo-client/src/main/java/com/od/dubbotest/action/BjAction.java
create mode 100644 dubbo-client/src/main/java/com/od/dubbotest/action/HelloAction.java
create mode 100644 dubbo-client/src/main/java/com/od/dubbotest/action/ShAction.java
create mode 100644 dubbo-client/src/main/resources/META-INF/app.properties
create mode 100644 dubbo-client/src/main/resources/config.properties
create mode 100644 dubbo-client/src/main/resources/spring-config.xml
create mode 100644 pom.xml
[root@server01 dubbo-demo-web-apollo]# git -c http.sslVerify=false push origin master
Counting objects: 33, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (33/33), 5.55 KiB | 0 bytes/s, done.
Total 33 (delta 4), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
To git@gitee.com:reblue520/dubbo-demo-web-apollo01.git
* [new branch] master -> master
# 把master拉取新分支成master,并且提交为apollo
[root@server01 dubbo-demo-web-apollo]# git checkout -b apollo
Switched to a new branch 'apollo'
[root@server01 dubbo-demo-web-apollo]# git branch
* apollo
[root@server01 dubbo-demo-web-apollo]# git push -u origin apollo
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-5.0]
remote: Create a pull request for 'apollo' on Gitee by visiting:
remote: https://gitee.com/reblue520/dubbo-demo-web-apollo01/pull/new/reblue520:apollo...reblue520:master
To git@gitee.com:reblue520/dubbo-demo-web-apollo01.git
* [new branch] apollo -> apollo
Branch apollo set up to track remote branch apollo from origin.
windows中git-cmd的使用
# 步骤
# 1.创建一个目录
mkdir muke_vedio_test
# 2.进入目录将当前目录变为git管理仓库
git init
# 3.将文件添加到版本库,这里将目录下的所有文件都添加进去了
git add .
# 4.告诉git将文件提交到仓库
git commit -m "first-commit"
# 5.将当前仓库与远程仓库关联
git remote add orign 远程仓库的https地址 # eg: git remote add https://github.com/reblue520/muke_vedio_test.git
# 6.将仓库内master分支的所有内容推送到远程仓库,这里会使用到Github的账号密码
# 6.将仓库内master分支的所有内容推送到远程仓库,这里会使用到Github的账号密码
删除远程 Git 仓库 main
# git remote rm origin main
添加远程 Git 仓库
# git remote add origin git@github.com:reblue520/muke_vedio_test
# 修改 gitconfig文件
D:\python\django_imooc_xiaobai\muke_vedio_test\.git\config
https://用户名:密码@github.com/[username]/project.git
配置示例:
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] # 当配置了账号密码后,使用 git push -u orgin main 推送代码时就不需要输入账号密码了 url = https://reblue520:pass@github.com/reblue520/muke_vedio_test fetch = +refs/heads/*:refs/remotes/origin/* [remote "origin"] url = git@github.com:reblue520/muke_vedio_test fetch = +refs/heads/*:refs/remotes/origin/* [branch "main"] remote = origin merge = refs/heads/main [branch "develop"] remote = origin merge = refs/heads/develop
1.上传代码
1.1 创建自己的远程Repository, github或者gitee
1.2 创建本地git仓库
$ git init
Note:此指令本地源码根目录执行,执行成功后,会在当前目录生成一个名为“.git”的目录。所有对本地仓库的操作信息都在此目录下存储。
1.3 将项目的所有文件添加到本地仓库中,再执行添加命令:
$ git add .
Note:此处的“.”表示当前目录,如果只添加某些目录,则使用:git add [目录1] [目录2]。
1.4 提交对本地仓库的修改
$ git commit -m "注释语句"
1.5 先与远程的库做一个链接,这里以video库为例:
$ git remote add origin https://github.com/reblue520/video.git
1.6 上传github之前,pull一下
$ git pull origin master 或者 git pull --rebase origin master 否则会报错(没有readme文件)
Note:此处的使用origin是因为第三步在建立本地仓库时git init 指令后面没有给本地仓库命名,因此origin为缺省的名字。如果在执行git init时添加了自定义名字,请使用自定义名字。本地仓库的名字没有要求非得和远程仓库名字相同。
此处的使用master是因为远程仓库的master分支,如果想上传到其它分支,此处改为指定分支名称即可。
1.7 上传代码到远程仓库
$ git push -u origin master(远程仓库的名字在哪一个分支或者主线上)
Note:如果没有保存github(码云)的登录账号和密码, 再执行此指令后会提示输入Username和Password,输入正确的github(码云)的登录账号和密码之后就会上传之前提交的代码。
2. 更新代码
2.1 查看本地代码和本地仓库中代码的区别
$ git status
2.2 确认无误后更新全部修改到本地仓库
$ git add *
2.3 提交修改
$ git commit -m "说明信息"
2.4 获取远程仓库指定分支的最新代码
$ git pull origin master
2.5 上传到远程仓库指定分支上
$ git push origin master
3. 下载代码
3.1 使用git下载github或者gitee上的代码,这里以video为例
$ git init (可有可无)
$ cd to/path/video
$ git clone https://github.com/video/video.git
创建新的分支
1.提交当前分支代码
# 查看当前分支为 develop ,提交develop 分支代码
D:\python\django_imooc_xiaobai\muke_vedio_test>git branch * develop main D:\python\django_imooc_xiaobai\muke_vedio_test>git add * D:\python\django_imooc_xiaobai\muke_vedio_test>git commit -m "readme 20210129" [develop 2790fd3] readme 20210129 7 files changed, 437 insertions(+), 1 deletion(-) create mode 100644 .idea/deployment.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 video/app/libs/__init__.py create mode 100644 video/app/libs/base_render.py D:\python\django_imooc_xiaobai\muke_vedio_test>git branch * develop main D:\python\django_imooc_xiaobai\muke_vedio_test>git push -u origin develop Enumerating objects: 18, done. 2.切换分支 D:\python\django_imooc_xiaobai\muke_vedio_test>git checkout main Switched to branch 'main' Your branch is based on 'origin/main', but the upstream is gone. (use "git branch --unset-upstream" to fixup) D:\python\django_imooc_xiaobai\muke_vedio_test>git branch develop * main 3.创建本地分支uat D:\python\django_imooc_xiaobai\muke_vedio_test>git branch uat # 切换到 uat 分支 D:\python\django_imooc_xiaobai\muke_vedio_test>git checkout uat Switched to branch 'uat' D:\python\django_imooc_xiaobai\muke_vedio_test>git status On branch uat nothing to commit, working tree clean # 推送代码到github的uat分支中 D:\python\django_imooc_xiaobai\muke_vedio_test>git push -u origin uat Total 0 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'uat' on GitHub by visiting: remote: https://github.com/reblue520/muke_vedio_test/pull/new/uat remote: To https://github.com/reblue520/muke_vedio_test * [new branch] uat -> uat Branch 'uat' set up to track remote branch 'uat' from 'origin'. Connection reset by 52.74.223.119 port 22 fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. D:\python\django_imooc_xiaobai\muke_vedio_test>git branch develop main * uat
.git/config 配置变更为如下
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] url = https://reblue520:pass@github.com/reblue520/muke_vedio_test fetch = +refs/heads/*:refs/remotes/origin/* [remote "origin"] url = git@github.com:reblue520/muke_vedio_test fetch = +refs/heads/*:refs/remotes/origin/* [branch "main"] remote = origin merge = refs/heads/main [branch "develop"] remote = origin merge = refs/heads/develop [branch "uat"] remote = origin merge = refs/heads/uat
# git remote rm origin main