git提交本地代码到远程新分支

有时候我们回滚项目代码后,常常将本地回滚的分支提交到远程分支,命令如下:
1.建立本地仓库
查看当前项目根目录中有没有 .git文件(隐藏文件),如果没有,右键->Git bash here ,然后输入命令git init建立本地仓库

git init
1
2.将代码提交到本地仓库

git add .
git commit -m "new branch commit"
1
2
3.在本地仓库中建立一个与远程仓库的别名,以便之后提交代码而不是每次都要输入远程仓库地址。指令结尾是git的仓库地址,我使用的是SSH连接方式

git remote add origin git@XX.XX.XX.12:gyjia/hotcodeserver.git

1
2
使用SSH的话,提交会出现以下问题:

git@gitee.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

1
2
3
4
5
6
所以需要声明公钥:
1)、重新生成ssh
ssh-keygen -t rsa -C “xxxxx@xxxxx.com” (你的账号)
2)、查看你的 public key
cat ~/.ssh/id_rsa.pub
(以ssh-rsa 开头,以账号的注册邮箱结尾的)
3)、将它添加到码云,添加地址 https://gitee.com/profile/sshkeys
4)、终端输入
ssh -T git@gitee.com
5)、完成

当然,如果不想验证公钥,则git remote add origin 时直接使用http链接即可

4.本地的代码提交的远程仓库上,步骤如下:

1)首先要建立本地的分支,并切换到该分支上(本地建立完分支,默认是在master分支上)

git branch hello_git_branch
1
git checkout hello_git_branch
1
2)push到远程仓库上面

git push origin hello_git_branch
1
这里的含义是将hello_git_branch这个分支提交到远程仓库上面。如果远程仓库没有这个分支,那么也会新建一个该分支。 还有一种方法,可以指定提交到远程仓库的某个分支上。如下,是将hello_git_branch分支提交到远程仓库的master上面

git push origin hello_git_branch:master
1
拓展:

如果本地当前是在hello_git_branch分支上面,此时想把远程仓库的master与我的hello_git_branch分支合并(merge),可以使用如下命令:
---------------------

posted @ 2019-06-27 15:32  水至清明  阅读(12696)  评论(0编辑  收藏  举报