Home > database >  How correctly create remote git reposotory
How correctly create remote git reposotory

Time:07-30

I've created on remote server bare git repo:

mkdir test-repo.git
chown git:git test-repo.git
cd test-repo.git
su git
git init --bare

After that, I cloned it to my local PC, committed and pushed some data to main branch.

And main problem, that after commit and push, if I cloned it again, I have empty folder:

git clone [email protected]:test-repo.git 
cd test-repo
ls
{empty here}

It seams, that after cloning my repo do not track remote branch automatically:

git status:
On branch main
No commits yet

As you can see, there is no origin/main.

For tracking remote branch I execute:

git pull origin main
From 1.1.1.1:test-repo
 * branch            main       -> FETCH_HEAD
git branch --set-upstream-to=origin/main main

So, it seems, that my "bare" repository is mot correctly setup. Because project data, should be present after cloning, without any additional steps.

CodePudding user response:

I did like here in Docs: enter link description here

With clone --bare and pushing it to remote server.

In my case there was also needed additional right:

chown -R git:git test-repo.git

CodePudding user response:

An empty repository, as created by git init when it says that it created a new repository, has no commits.

A branch name in any Git repository is required to contain the hash ID of a valid, existing commit.1 So this empty repository, which has no commits, cannot have any branch names. That's why cloning it doesn't create any commits or branches.

In other words, you have not displayed any actual problem. Everything you show is normal.

Before you can use the repository normally, you must create at least one commit. This will be an initial, or "root", commit. It will contain whatever files you have it contain, and this will cause the initial branch name that you chose, whether that's master or main or whatever, to spring into existence. This need for an initial commit to get a repository to behave the way people usually expect Git repositories to behave is why sites like GitHub offer you the option—and default it to enabled!—to create the repository and put in an initial commit. As long as you're expecting "empty repository" behavior, there's no actual requirement for an initial commit, but most people don't expect this behavior.

Once you have an initial commit, everything behaves the way people expect. Just don't expect an empty repository to behave the way other repositories behave, because it won't. Handle that however you like, whether that's by training your people to understand this, or by creating an initial commit so that they can remain ignorant.2


1This phrase is a bit redundant: an existing commit is valid, and a valid commit is existing. The point is to emphasize that you can't project what a future commit's hash ID will be and thereby stick that into a branch name: you have to make the commit first, to find out its hash ID.

2Ignorance should not be considered shameful here: most people don't need to know this, just as most people don't need to know that e's approximation is not just 2.7182818 but in fact more like 2.7182818284590452353602874713526624977572470936999595749669676. (I don't have that much memorized myself, I always have to look it up. The more important thing here is to know that they are irrational, so whatever rational base we use to express e or π, they are still approximations.)

  •  Tags:  
  • git
  • Related