我是 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/