Git 向两个远程仓库提交代码

创建两个远程仓库

在 gitlab 上创建远程仓库 test-commit-2-2-repository
在 github 上创建远程仓库 test-commit-2-2-repository

创建本地仓库并初始化

本地电脑创建test-commit-2-2-repository目录
进入到test-commit-2-2-repository目录下

初始化.git目录

git init --initial-branch=main

Git 独立设置

git config user.name "uwoerla"
git config user.email "uwoerla@qq.com"

提交到 gitlab 仓库

# 添加 gitlab 远程仓库 origin
git remote add origin命名为 git@gitlab.com:uwoerla/test-commit-2-2-repository.git
touch README.md
git add README.md
git commit -m "add README"
git push --set-upstream origin main

提交到 github 仓库

# 添加 github 远程仓库命名为 github-origin
git remote add github-origin git@github.com:uwoerla/test-commit-2-2-repository.git
# -u 参数可以在推送的同时,将 github-origin 仓库的 main 分支设置为本地仓库当前分支的 upstream(上游),添加了这个参数,将来运行 git pull 命令从远程仓库获取内容时,本地仓库的这个分支就可以直接从 github-origin 的 main 分支获取内容,省去了添加参数的麻烦
git push -u github-origin main

一些常见的操作

查看远程仓库信息

git remote -v
github-origin	git@github.com:uwoerla/test-commit-2-2-repository.git (fetch)
github-origin	git@github.com:uwoerla/test-commit-2-2-repository.git (push)
origin	git@gitlab.com:uwoerla/test-commit-2-2-repository.git (fetch)
origin	git@gitlab.com:uwoerla/test-commit-2-2-repository.git (push)

指定源定分支拉取代码

git pull origin main

指定源 指定分支提交代码

git push github-origin main

创建分支

以某个分支为基础,创建 develop 分支 并切换 develop 分支为当前分支

git checkout -b develop

显示本地仓库分支一览表

git branch
* develop
  main

同时显示本地仓库和远程仓库的信息

git branch -a
* develop
  main
  remotes/github-origin/main
  remotes/origin/main

切换分支

git checkout main
# 等同于 git checkout main
git branch main

获取远程仓库的最新数据

# 获取 origin 远程仓库上的最新数据(分支和代码)
git fetch 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 499 bytes | 249.00 KiB/s, done.
From gitlab.com:uwoerla/test-commit-2-2-repository
   7f82641..71d8a3b  main       -> origin/main
From gitlab.com:uwoerla/test-commit-2-2-repository
 * [new branch]      test       -> origin/test

git branch -a
* develop
  main
  remotes/github-origin/main
  remotes/origin/main
  remotes/origin/test

# 获取 github-origin 远程仓库上的最新数据(分支和代码)
git fetch github-origin
From github.com:uwoerla/test-commit-2-2-repository
 * [new branch]      develop    -> github-origin/develop
git branch -a
* develop
  main
  remotes/github-origin/develop
  remotes/github-origin/main
  remotes/origin/main
  remotes/origin/test

基于远程分支创建分支

获取远程的 test 分支到本地、同样命名为 test 分支、并切换到本地 test 分支

git checkout -b test origin/test

git branch -a
  develop
  main
* test
  remotes/github-origin/develop
  remotes/github-origin/main
  remotes/origin/main
  remotes/origin/test

获取远程仓库的最新数据后 merge 代码

git branch -a
  develop
  main
* test
  remotes/github-origin/develop
  remotes/github-origin/main
  remotes/origin/main
  remotes/origin/test

git merge origin/main
Updating 7f82641..71d8a3b
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

git push origin test
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for test, visit:
remote:   https://gitlab.com/uwoerla/test-commit-2-2-repository/-/merge_requests/new?merge_request%5Bsource_branch%5D=test
remote:
To gitlab.com:uwoerla/test-commit-2-2-repository.git
   7f82641..71d8a3b  test -> test

本地到远程

git push origin-name local-baranch-name:remote-baranch-name

merge 远程的分支

git merge origin-name/remote-baranch-name 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值