简体   繁体   中英

How do I recover deleted branches after git clone?

I have a local git repository (which was cloned from a remote origin branch):

~/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. I did the following, hoping branch3 will be added to my local repository:

~/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:

~/foo$ git branch
*branch3

Where are my branch1 , branch2 , and how can I recover them?

I don't have copies of branch1 , branch2 in the remote repository.

If a remote branch3 already existed, it should have been enough to do a

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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