Git:无法将分支 pull 回本地(找不到远程引用)

标签 git github git-branch

我是 git 的新手,我真的很挣扎。最近我创建了一个新分支,做了一些工作,然后将它推送到 GitHub,创建了一个 pull request。没有问题。

但是,我的本地工作开发环境得到了更新,我丢失了对我的分支机构的所有本地引用。我需要在本地恢复我的工作,所以我继续。

从网上看,我试过:

重新创建我的分支:

git checkout -b 'name-of-branch-I-had-been-working-on'

从远程 pull 入分支

git pull origin 'name-of-branch-I-had-been-working-on'

然后我尝试删除我在上面创建的本地分支 (git branch -d) 并尝试:

git fetch origin 'name-of-branch-I-had-been-working-on'

但每次我都会得到错误:

严重:无法找到远程引用 name-of-branch-I-had-been-working-on

有人知道这样做的正确方法吗?

最佳答案

来自评论: 我:当你执行 git branch -r 时,你想要从远程恢复的分支是否出现在列表中? 你:是的。

从这一点上我会: (如果需要,首先存储所有未提交的更改)

# first delete the failed new branch
git branch -D local-branch-you-just-created-when-trying-to-solve-the-problem
# get all up-to-date remote references into your local repo
git fetch
# (optionnally) list all branches to be sure
git branch -a
# to avoid typos, copy-paste the line with your branch in your next checkout
# which will recreate a local version of said remote branch
git checkout name-of-the-branch-you-try-to-recover-from-remote

注意:当列出所有分支时(假设您的分支名为“feature_A”),不要

git checkout origin/feature_A

(检查 remote-tracking 分支)但只是

git checkout feature_A

对于本地实例。

关于Git:无法将分支 pull 回本地(找不到远程引用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51054662/

相关文章:

git - 在 'git merge' 之后保留提交历史

git - z/OS 有 GIT 客户端吗?

git - 在 Git 中找出您从中克隆的原始存储库的名称

git - 我应该如何构建我的 Git 存储库中的文件/目录?

git - Squash 直接在功能上提交,无需 rebase 或 merge

git - 使用 Git 和 Netbeans 从单独的分支中提取更改

git - "svn status -u"的 git 等价物(重新审视)

android - Git 推送失败 : fatal: unable to access Unknown SSL protocol error in connection to github. com:-9805

git - 在 Git 中 merge 两个分支?

github - 我可以在Gist之外的网页上嵌入来自GitHub的源文件吗?