git常见问题记录:【git clone后报错】 warning: remote HEAD refers to nonexistent ref, unable to checkout.

文章讲述了在从服务器获取DPDK代码时遇到的gitclone警告,原因是默认拉取的master分支与本地分支名称不符。解决方法是通过gitbranch-a切换到正确的分支(如origin/20.11),并创建新的分支以保留代码。

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


1、概述

比如我从服务器上获取 DPDK 的代码:

git clone 【地址】

在这里插入图片描述
发现看似代码下载下来了,但是 ls 命令后项目文件夹里的内容都是空的!!
可以看到最后有个 warning :warning: remote HEAD refers to nonexistent ref, unable to checkout.

2、原因

git clone 代码时会默认拉取远程服务器 master 主分支下的代码,可是我创建的主分支没有用 master 命名,里面是 origin/20.11 分支,目录下 .git/refs/heads 不存在 HEAD 指向的文件,因此产生了警告。

remotes/m/master -> origin/master //其余工程代码有master分支

上下对比:

remotes/m/master -> origin/20.11 //本工程代码分支没有master分支导致警告

如下图显示:

ls .git/refs/heads/

在这里插入图片描述

3、解决方法

1、进入git clone 操作后,生成的项目文件夹内,使用 git branch -a 查看所有的分支

hwc@hwc:~/dpdk$ git branch -a
  remotes/origin/18.11.11
  remotes/origin/19.11.14
  remotes/origin/20.11
  remotes/origin/default

2、选择自己需要的分支,比如我需要获取 origin/20.11 分支上的代码,那么执行操作git checkout remotes/origin/20.11

hwc@hwc:~/dpdk$ git checkout origin/20.11
Note: checking out 'origin/20.11'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at d2d894a55f Add: supports the "set link-up/link-down" command.

3、这时候 ls 命令就能看到你想要的代码

hwc@hwc:~/dpdk$ ls
app         devtools  examples     lib          Makefile           mk      test
buildtools  doc       GNUmakefile  license      meson.build        pkg     usertools
config      drivers   kernel       MAINTAINERS  meson_options.txt  README

4、新建一个分支后,操作git log, git status等命令。

hwc@hwc:~/dpdk$ git checkout -b dev-20.11
Switched to a new branch 'dev-20.11'

到此,解决问题!


谢谢点赞关注哈!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啵啵学习

你的鼓励将是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值