1

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.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 2
    I believe [this](http://stackoverflow.com/questions/3640764/can-i-recover-branch-after-its-deletion-in-git) question may help you. If it doesn't (e.g. `.git` directory was already cleaned), I doubt you can do something. – yeputons Feb 03 '17 at 06:48
  • The linked answers didn't help me, but I found a way to recover. Even though I could not find a way to see my previous branches on the local, I was somehow able to push them to a remote repository of mine: `git push git@myremote_repository.git branch1`, and the same for `branch2`. – sawa Feb 03 '17 at 07:05
  • 1
    Next time, just do `git checkout branch3`. Git will automatically set up the remote tracking for you. – rlee827 Feb 03 '17 at 07:06

1 Answers1

0

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.

andipla
  • 363
  • 4
  • 9