keybase / client

Keybase Go Library, Client, Service, OS X, iOS, Android, Electron
BSD 3-Clause "New" or "Revised" License
8.91k stars 1.23k forks source link

[git] Couldn't push non-master branch to new repository #22929

Open junderw opened 4 years ago

junderw commented 4 years ago

Repro:

  1. Create new repo on keybase
  2. Add as remote on existing local repo that doesn't have a master branch (default branch is develop, and almost no one checks out master on their PC)
  3. With develop checked out, try git push keybase develop
  4. git will show as if it succeeded.
  5. On another PC, try to clone keybase git repo.
  6. Run ls -a and see the folder is empty

Work around:

checking out master and pushing master first, then pushing develop etc. allows the data to be uploaded to keybase. Then cloning works and you can checkout develop and it will link to the remote keybase develop.

Perhaps this is spec for git... not too sure. Please close if so.

strib commented 4 years ago

I don't follow steps 5-6 -- if you just cloned the repo but didn't actually switch to the origin/develop branch on the new clone, wouldn't you expect the repo to be empty if there's nothing in master? Or is there some other step missing there?

junderw commented 4 years ago

is it git protocol to pull master if no branch is specified during clone?

And if origin/master doesn't exist it just doesn't clone anything? (no .git folder even)

the keybase repo should have one branch origin/develop.

junderw commented 4 years ago

it does create the folder though... but no .git folder until I push up devlop after renaming as master.

junderw commented 4 years ago

Here's some repro steps for bash

mkdir ws1 ws2

cd ws1
git clone https://github.com/junderw/testkeybasegitissue22929
cd testkeybasegitissue22929/

keybase git create testkeybasegitissue22929
MY_KB_USER=$(keybase whoami)
git remote add keybase keybase://private/$MY_KB_USER/testkeybasegitissue22929
git push keybase develop

cd ../../ws2

# errors
git clone keybase://private/$MY_KB_USER/testkeybasegitissue22929
cd testkeybasegitissue22929/
# "is not a git repository"
git checkout develop
# "is not a git repository"
git branch --all
# .git folder is there but not recognized
ls -a
junderw commented 4 years ago

Notice my github only has a develop branch. and it is default.

The workaround is to locally create a master branch off develop and push that to keybase... then the clone works fine.

strib commented 4 years ago

Ah I see. Thanks for the full repro, that was useful!

Github has a special notion of "default" branch that is not really related to git itself. See the top answer to this question: https://stackoverflow.com/questions/51274430/change-from-master-to-a-new-default-branch-git

Basically, it looks like github is setting the .git/HEAD file to point to your develop branch, while Keybase (and git in general) always initializes it to master. That only works because Github has a non-standard feature that lets you set it that way on the server. Keybase doesn't have that feature, so the "server" repository that you clone from (and that's stored in your private KBFS folder) always has it set to master.

If you'd like to clone a certain branch from the Keybase repo, you can use this command to set the branch:

git clone -b develop keybase://private/$MY_KB_USER/testkeybasegitissue22929

You can also muck around manually in your hidden .kbfs_git subfolder if you want to set the HEAD branch of the server repo (this is what Github does under the covers), but I don't really recommend it. If you mess up, you risk breaking the repository.

junderw commented 4 years ago

Ok sounds good.

So a task I might suggest branching from this:

I had no idea about the fact that Github / Gitlab are not adhering to the spec of git... I'm sure many people just assume github is the spec :-D

Thoughts?

caitlinelfring commented 4 years ago

It's worth noting that in Git 2.28 (not just GitHub/Gitlab), a new configuration option, init.defaultBranch has been introduced to replace the hard-coded term.

  • The name of the primary branch in existing repositories, and the default name used for the first branch in newly created repositories, is made configurable, so that we can eventually wean ourselves off of the hardcoded 'master'.

https://raw.githubusercontent.com/git/git/master/Documentation/RelNotes/2.28.0.txt. For background on this change, see https://sfconservancy.org/news/2020/jun/23/gitbranchname/

This seems like a good enough reason for Keybase to support non-master branches within the Keybase UI

junderw commented 4 years ago

Unfortunately, Keybase was bought by Zoom and suddenly commits became extremely sparse.

I doubt they will have the resources to fix any of these problems...

But that's the great thing about open source. Someone could do it for them.

Not sure if they'd have the resources to be able to merge and release it though...

bogosj commented 3 years ago

https://git-scm.com/book/en/v2/Git-Internals-Git-References#ref_the_ref seems to indicate it's safe to modify this file, but the only way to do it on the remote is to edit it in .kbfs_git

kg4zow commented 1 year ago

A few things to point out ...

For a long time I was in the habit of creating new repos with a master branch containing nothing but a README.md file explaining the actual branch names used in the repo, and creating the primary branch off of that. Later I figured out how to create the actual primary branch as an "orphan", so the master branch ended up not being part of the "real" commit history at all.

As @strib pointed out, you can use git clone -b xxx to manually check out a specific branch after cloning the repo, or as I explained in this comment you can manually edit the HEAD file in the "hidden" KBFS directory which acts as the "upstream" for the repo.

As for how to best "fix" the issue, my suggestions are ...