赞
踩
本方法适用于Github远程空仓库
空仓库即在Github新建仓库时,没有选择添加README.md
、LICENSE
、.gitignore
其中任何文件,细分为以下几种情况:
echo "# 你的仓库名" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/xxx/xxx.git
git push -u origin main
echo "# 你的仓库名" >> README.md
git remote add origin https://github.com/xxx/xxx.git
git branch -M main
git push -u origin main
新建仓库后点击下面的import code
即可
这样在push的时候,会自动将本地的main分支(已经改名了),和远程的origin/main分支关联。
本方法适用于Github远程非空仓库
具体而言,就是在创建仓库的时候,选择了添加LICENSE
,.gitignore
以及 README.md
文件。如下:
其中,本地自动有main分支,追踪远端main
否则执行拉取会提示:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
解决办法:
a. `git pull oringin 远程分支名`
这里我指定了main,毕竟目前只有这一个;使用 branch 查看本地分支,依旧是默认的 master,且没有追踪远端,直接执行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
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
执行命令,追踪远端master分支,发现:
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
打开GitHub观察,发现远端出现了新的分支:
那么以后的情况就变成了,本地master追踪远端master,远端main没有拉取回来,本地没有main。
新的分支来都来了,索性走一下merge request 的流程:
- PR
- Merge PR
- (可选)删除master
注意这里都是我自己,但是进行分支merge的操作,依旧需要走这个流程;其次,如果不删除master分支,那么本地与远端的联系依旧存在,直接git pull,会将远端master拉取到本地master;如果删除,会提示:
Your configuration specifies to merge with the ref 'refs/heads/master'
from the remote, but no such ref was fetched.
即,还需要重复之前的步骤,拉取远端main;
拉取远端main,直接Fast-forward,本地master直接向前移动,比远端master提前了一次提交,以后的流程可能就需要多分支的处理了。
b. `git branch --set-upstream-to=origin/<branch> master`
这里直接本地分支与远程分支关联,但是执行会报错 fatal: No commit on branch 'master' yet.
看起来需要本地先有一个分支,不论什么名字,运行:
$ git checkout -b main
Switched to a new branch 'main'
但好像没有提交的话,不会真的创建一个分支,会提示fatal: No commit on branch 'main' yet.
,看起来必须得提交一次才行,才能有本地分支
这时候可以运行:
$ git branch --set-upstream-to=origin/main main
branch 'main' set up to track 'origin/main'.
当前的情况是:
$ git branch -vv
* main 2a92788 [origin/main: ahead 1, behind 1] test_4
不能直接pull,否则会出现fatal: refusing to merge unrelated histories
不能直接push,否则会出现:
To https://github.com/gaoyi3/test_4.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/gaoyi3/test_4.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
我的理解是,此时本地和远端没有共同的祖先提交,所以git将其视为了2个不相关的仓库,无法合并。 解决方式如下:
当你在合并两个不相关的 Git 仓库时,可能会遇到 “fatal: refusing to merge unrelated histories” 错误。这是因为 Git 默认不允许合并不同的历史记录,因为这可能会导致代码库的混乱。
要解决这个问题,可以使用 --allow-unrelated-histories
选项来强制合并不同的历史记录。具体步骤如下:
在你的本地仓库中,使用 git pull
命令拉取远程仓库的代码。
如果你遇到 “fatal: refusing to merge unrelated histories” 错误,可以使用以下命令来强制合并不同的历史记录:
git pull origin master --allow-unrelated-histories
这里假设你要将远程仓库的 master
分支合并到本地仓库的当前分支。
如果合并成功,你可以使用 git push
命令将本地仓库的代码推送到远程仓库。
请注意,强制合并不同的历史记录可能会导致代码库的混乱,因此在执行此操作之前,请确保你知道自己在做什么,并且备份你的代码库。
按照操作执行,可以合并,并且也可以push到远端,只不过此时的git graph变得很奇怪:
一般的流程是:
提交后直接点击VSCode插件的发布分支,则经过授权后,会发布一个共有的或者私有的仓库,名称和项目文件夹名称一样。这个还挺推荐的,比较适合新手;
添加远端,push,但是需要关联远端:
$ git push origin
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
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
直接push,或者按照上面的指定远端main分支后push,都会收到:
error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/xxx/test_5.git'
而执行pull,遇到熟悉的错误:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
这时候的情况和上面一点中是一样的,需要git pull origin main --allow-unrelated-histories
注意,即使这时候改名本地分支,或者设置远端分支为origin/main,都是不可以的,在git看来这就是2此不相关的提交。
也可以通过git branch --set-upstream-to=origin/main main
来设置远端分支,再git pull origin main --allow-unrelated-histories
再尝试一下另一种情况:初始化一个已有内容的仓库,结论是一样的
如果远端初始化的时候,添加了其他文件,那么本地初始化的仓库需要:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。