本地项目添加远程GIT管理

本文介绍了解决Git中遇到的致命错误:当前分支master没有上游分支的问题,并提供了详细的步骤来同步远程master分支,包括如何处理未关联的历史记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. git init

  2. git add .

  3. git commit -m “init”

  4. git remote add origin 你的github仓库地址

  5. git push

但是,以上步骤,可能会出错,如下:

$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

执行红色部分:

$ git push --set-upstream origin master
To gitee.com:xxxxx/yyyyy.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:xxxxx/yyyyy.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.

还是报错

在push远程服务器的时候发现出现此错误;原因是没有同步远程的master

所以我们需要先同步一下,使用git pull origin master

$ git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitee.com:xxxxx/yyyyy.git
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

依然有错误,接下来只要如下设置一下就OK了

$ git pull origin master --allow-unrelated-histories
From gitee.com:xxxxx/yyyyy.git
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

然后,继续同步操作:

$ git pull origin master
From gitee.com:xxxxx/yyyyy.git
 * branch            master     -> FETCH_HEAD
Already up-to-date.

再来设置一下upstream

$ git push --set-upstream origin master
Counting objects: 44, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (35/35), done.
Writing objects: 100% (44/44), 53.92 KiB | 0 bytes/s, done.
Total 44 (delta 2), reused 0 (delta 0)
To gitee.com:xxxxx/yyyyy.git
   1e4dc19..8d8694c  master -> master
Branch master set up to track remote branch master from origin.
如上操作,就可以了










评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值