简体   繁体   English

如何在git clone之后恢复已删除的分支?

[英]How do I recover deleted branches after git clone?

I have a local git repository (which was cloned from a remote origin branch): 我有一个本地git存储库(从远程origin分支克隆):

~/foo

I had some branches in the local repository: 我在本地存储库中有一些分支:

~/foo$ git branch
*master
branch1
branch2

The origin repository has a branch branch3 that my local does not have, and I wanted to add it to my local. origin存储库有一个我本地没有的分支branch3 ,我想将它添加到我的本地。 I did the following, hoping branch3 will be added to my local repository: 我做了以下,希望branch3将被添加到我的本地存储库:

~/foo/..$ git clone -b branch3 --single-branch git@github.com:<localrespository_name>.git

After that, I see that there is only branch3 in the local respository: 之后,我看到本地branch3中只有branch3

~/foo$ git branch
*branch3

Where are my branch1 , branch2 , and how can I recover them? 我的branch1branch2在哪里,我branch1如何恢复它们?

I don't have copies of branch1 , branch2 in the remote repository. 我在远程存储库中没有branch1branch2副本。

If a remote branch3 already existed, it should have been enough to do a 如果已经存在远程分支3,那应该足够了

git checkout -b branch3 origin/branch3

instead of the clone command you used. 而不是您使用的克隆命令。

You may see all branches (including remotes) with 您可能会看到所有分支(包括遥控器)

git branch --all

in example, on a linux-stable tree: 例如,在linux-stable树上:

andi@SHARK:~/working_git/linux-stable$ git branch
  master
andi@SHARK:~/working_git/linux-stable$ git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/linux-2.6.11.y
  remotes/origin/linux-2.6.12.y
  remotes/origin/linux-2.6.13.y
  remotes/origin/linux-2.6.14.y
  <SNIP>
  remotes/origin/linux-4.9.y
  remotes/origin/master
  andi@SHARK:~/working_git/linux-stable$ git checkout -b v4.9 origin/linux-4.9.y
  Checking out files: 100% (11425/11425), done.
  Branch v4.9 set up to track remote branch linux-4.9.y from origin.
  Switched to a new branch 'v4.9'
  andi@SHARK:~/working_git/linux-stable$ git branch
     master
   * v4.9
  andi@SHARK:~/working_git/linux-stable$ git log -1
  commit 75353ac8ff437322ca5520b28d9f9b4b41b39bd6
  Author: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  Date:   Sun Jan 15 13:43:07 2017 +0100

      Linux 4.9.4

Now we got the local v4.9 branch on the remote/origin/linux-v4.9.y branch. 现在我们在remote / origin / linux-v4.9.y分支上获得了本地v4.9分支。

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

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