简体   繁体   English

Git checkout 远程分支在本地删除

[英]Git checkout remote branch deleted locally

In one of my systems while working on a project, I made changes and pushed it.在处理项目时,我在我的一个系统中进行了更改并推送了它。 Now I have a new system with all the same ssh/user details, I cloned that project and wanted to continue my unfinished change by checking out my branch.现在我有了一个具有所有相同 ssh/用户详细信息的新系统,我克隆了那个项目并想通过检查我的分支来继续我未完成的更改。 Here I did one mistake: I made checkout with -b option ie在这里我犯了一个错误:我用 -b 选项结账,即

git checkout -b mybranch
//instead of 
git checkout mybranch

So a blank branch was presented to me instead of cloned remote branch.因此,向我呈现了一个空白分支,而不是克隆的远程分支。

So I deleted locally created branch using:所以我使用以下方法删除了本地创建的分支:

git branch -d mybranch

I tried below things(by googling) without any success:我尝试了以下事情(通过谷歌搜索)但没有成功:

git checkout --track origin/mybranch
git pull origin origin/mybranch

How can I pull and checkout to mybranch?我怎样才能提取并结帐到我的分支机构?

If there's no work laying around there, make the branch look like it was started from the remote branch.如果那里没有工作,让分支看起来像是从远程分支开始的。

# dangerous, that's why i said _if_ there's no work laying around
git reset --hard origin/mybranch # place the local branch where the remote branch is (local branch and worktree content)
git branch --set-upstream origin/mybranch

That should be good enough.那应该足够好了。

But given that you deleted the local branch already但是鉴于您已经删除了本地分支

git checkout mybranch

Should be good enough for git to create the local branch from the remote应该足以让 git 从远程创建本地分支

Steps:脚步:

  1. Asked a colleague to switch to myBranch.要求同事切换到 myBranch。
  2. Replace contents of my project/.git/config file to his contents for the same file.将我的 project/.git/config 文件的内容替换为同一文件的内容。
  3. Checkout myBranch.结帐 myBranch。 Works!作品!

This trick worked for me when just commands didn't helped.当命令没有帮助时,这个技巧对我有用。

Once deleted locally, you cannot recover it but you can recreate it with an other name with the following command:一旦在本地删除,您将无法恢复它,但您可以使用以下命令使用其他名称重新创建它:

git checkout -b name_of_the_branch origin/name_of_the_branch_on_github

git checkout -b name_of_the_branch allows you to create a branch starting from the one where you are, if you add an argument, the new branch will start from this argument origin/name_of_the_branch_on_github in this example. git checkout -b name_of_the_branch允许你从你所在的分支开始创建一个分支,如果你添加一个参数,新分支将从这个参数origin/name_of_the_branch_on_github在这个例子中开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM