Git记录一次在本地创建仓库后关联远程仓库并成功推送

本文讲述了在本地代码有改动但未关联远程仓库时,如何处理远程仓库覆盖的情况,包括添加远程地址、合并分支、配置gitpull/rebase策略并最终成功推送的过程。
摘要由CSDN通过智能技术生成

首先说明,这个操作是把远程仓库覆盖掉了。

条件:

1、有远程仓库 remote_url

2、本地代码有修改,但是没有关联远程仓库

下面是我的操作

1、在本地项目初始化 

git init

2、添加远程地址

git remote add origin remote_url

3、将文件添加

git add .

4、提交

git commit -m "初始化"

5、此时执行 git push origin master 会报错

To https://gitee.com/12345/t.git  ! [rejected]        master -> master (fetch first) error: failed to push some ref  to 'https://gitee.com/12345/t.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.

 然后我执行了 

git pull origin master

 然后出现下面这个

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.72 KiB | 251.00 KiB/s, done.
From https://gitee.com/12345/t
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

然后,从网上找到了下面这句 去执行

git pull origin master --allow-unrelated-histories

就出现了下面这段

From https://gitee.com/123456/t
 * branch            master     -> FETCH_HEAD
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

我选择了 :git config pull.rebase true

git config pull.rebase true

到这里再重新进行3、4步操作

最后执行

git push origin master

成功推送

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值