Git关联/克隆远程仓库到本地,然后推送回远程仓库

列下重要命令格式

  • git push <远程主机名> <本地分支名>:<远程分支名>。如果加上参数"-u",那么提交的本地分支就会成为追踪分支。
  • git fetch <远程主机名> <远程分支名>:<本地分支名>
  • git pull <远程主机名> <远程分支名>:<本地分支名>
  • git checkout -b <本地分支名> <远程主机名>/<远程分支名>。该命令创建本地分支跟踪分支<本地分支名>,并从远程分支<远程主机名>/<远程分支名>拉取代码
# 如果在不想和远程有任何的瓜葛,则执行remote remove。
# 执行该命令前,git branch -a有本地分支master和远程分支master,git branch -vv显示这两者关联起来的。
# 执行该命令后,git branch -a只有本地分支,git branch -vv显示master这个本地分支,但它不再是远程分支。
git remote remove origin

# 最后检查一遍本地的branch,是否包含了所有远程的branch
git branch

从远程分支中检出本地分支会自动创建所谓的跟踪分支。跟踪分支是与远程分支有直接关系的本地分支。如果你在追踪分支并键入git push,Git会自动知道推送到哪个服务器和分支。同时,在其中一个分支上运行git pull会获取所有远程引用,然后自动合并到相应的本地分支中。
Git中的"追踪分支"的作用:简化推(git push)拉(git pull)命令。
在这里插入图片描述

一、已有本地项目,要把该项目推送到新建的远程仓库

情况:目前在gitee上新建了个名字是HelloWorld,地址是https://gitee.com/snow_night/hello-world的远程仓库,现在想把本地的SpringBoot项目推送到这个远程仓库。

方式一、关联远程库,然后推送

# 在要推送的项目的目录下,打开Git Bash Here
$ git init	
$ git remote add origin git@gitee.com:snow_night/hello-world.git
# git pull <远程主机名> <远程分支名>:<本地分支名>
$ git pull --rebase origin master	# 这句命令,会把远程库里的master分支的内容(不是hello-world文件夹)拉取到当前文件夹里,但master分支此时不是追踪分支。

$ git add .
$ git commit -m "提交个Springboot项目"
# git push <远程主机名> <本地分支名>:<远程分支名>。如果加上参数"-u",那么提交的本地分支就会成为追踪分支。
$ git push origin master

方式二、clone远程库,然后推送

# 这个命令,会克隆一个hello-world文件夹到当前文件夹里,文件夹内是远程库里的master分支内容
$ git clone git@gitee.com:snow_night/hello-world.git	
$ cd hello-world
1、从新建的远程仓库克隆下来的hello-world文件夹里的文件。
2、把这些文件复制到你要提交到远程仓库的项目中
# 在要推送的项目的目录下(上方的右侧图),打开Git Bash Here
$ git add .
$ git commit -m "提交个Springboot项目"
$ git push origin master

二、把远程仓库关联/克隆到本地,修改然后push回去

情况:目前在gitee上有个名字是HelloWorld,地址是https://gitee.com/snow_night/hello-world的远程仓库,现在想先拉取该仓库的所有分支,然后在本地修改该仓库的内容然后推送回去。
以下两种方式,都要在本地先新建个空文件夹,名字无所谓。然后在该文件夹中打开Git Bash Here,然后执行下方命令。

方式一、关联远程库,然后推送

$ git init	
$ git remote add --fetch --tags origin git@gitee.com:snow_night/hello-world2.git
# git checkout -b <branch> <remote>/<remote-branch>,该命令创建本地分支跟踪分支<branch>,并从远程分支<remote>/<remote-branch>拉取代码
$ git checkout -b master origin/master
$ git checkout -b develop origin/develop

# 这里修改一下pull下来的项目

$ git add .
$ git commit -m "修改README.md文件,增加“命令”两字"
$ git push origin master
演示图片

在这里插入图片描述

方式二、clone远程库(包括多个分支),然后推送

# 这个命令,会克隆一个hello-world文件夹到当前文件夹里,文件夹内是远程库里的master分支内容,即创建本地分支跟踪分支master,并从远程分支master拉取代码
$ git clone git@gitee.com:snow_night/hello-world.git	
$ cd hello-world
# git checkout -b <branch> <remote>/<remote-branch>,该命令创建本地分支跟踪分支<branch>,并从远程分支<remote>/<remote-branch>拉取代码
$ git checkout -b develop origin/develop

# 这里可以用git switch <branch>切换本地分支,工作区的内容也会对应更改
# 这里修改一下clone下来的项目

$ git add .
$ git commit -m "修改README.md文件,增加了‘的’字"
# git push <远程主机名> <本地分支名>:<远程分支名>
$ git push origin master
演示图片

在这里插入图片描述

常见错误

1、

dell@DESKTOP-4OG2TJ0 MINGW64 /d/Desktop/gitcode (master)
$ git commit
Aborting commit due to empty commit message.	#由于提交消息为空,正在中止提交。

解决:必须要提交注释信息,如git commit -m "update xxx文件"

2、

dell@DESKTOP-4OG2TJ0 MINGW64 /d/Desktop/hello-world (master)
$ git push origin master
To gitee.com:snow_night/hello-world.git
 ! [rejected]        master -> master (fetch first)
# 错误:无法将某些引用推送到'gitee.com:snow_night/hello-world.git'
error: failed to push some refs to 'gitee.com:snow_night/hello-world.git'
# 提示:更新被拒绝,因为远程包含本地没有的工作。这通常是由另一个存储库推送到同一个ref引起的。
# 您可能希望在再次推送之前先集成远程更改(例如,“git pull…”)。
# 有关详细信息,请参阅“git push--help”中的“关于fast-forwards的说明”。
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.

解决:用命令git pull --rebase origin master 将github修改的文件更新到本地,然后直接用git push提交。
不需要再次git addgit commit,因为git pull=git fetch + git mergegit merge会把本地库内容和下载的远程库内容合并后,提交到本地仓库。

3、

dell@DESKTOP-4OG2TJ0 MINGW64 /d/Desktop/hello (master)
$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.	# 致命:无法从远程存储库读取。

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

解决:用git remote add origin git@gitee.com:snow_night/hello-world.git关联远程库后,才能推送。

当没有修改东西就提交时的演示图片

在这里插入图片描述

参考

Git - 远程分支
在这里插入图片描述
如何优雅地克隆远程所有分支_Jackie Tan的专栏-CSDN博客_如何克隆远程分支

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值