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

时间:2018-06-27 04:42:37

标签: git github git-branch

我是git的新手,真的很挣扎。最近,我创建了一个新分支,做了一些工作,并将其推送到GitHub,创建了拉取请求。那里没有问题。

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

通过在线阅读,我尝试过:

重新创建分支:

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

从远程拉入分支

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'

但是在每种情况下我都会得到错误:

fatal: Couldn't find remote ref name-of-branch-I-had-been-working-on

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

2 个答案:

答案 0 :(得分:1)

我建议您执行以下操作:
1.在所需位置打开git bash
2.将远程存储库克隆到本地计算机:git clone <repo_url>
3.为了列出所有远程分支:git branch -a
   绿线表示您所在的分支。以remotes /开头的红色可能是要移动到的远程分支。
4.检出到远程分支:git checkout <branch_name>

就这样:)现在可以在分支上工作...提交更改,一旦您要推送更改,请运行:git push origin <branch_name>

答案 1 :(得分:1)

来自评论: 我:执行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

用于本地实例。

相关问题