pushing and pulling from remote git repo works but it is empty on remote server [duplicate] - linux

This question already has answers here:
What's the -practical- difference between a Bare and non-Bare repository?
(11 answers)
Closed 2 years ago.
I have a linux server with ssh and git installed.
I created a folder on the root directory and changed its owner to mike to avoid giving out root passwords to anyone.
sudo mkdir /GitRepos
sudo chown mike:mike /GitRepos
Then I created an empty repo, and set it to bare, otherwise pushing to this repo does not work (I tried!!)
mkdir test.git
cd test.git
git init
git config core.bare true
I then used another machine (Windows) to clone the newly created repo:
git clone ssh://mike#myLinuxServer/GitRepos/test.git
and that worked fine, I got an empty repo as expected.
I added some files and committed the change.
git add .
git commit -m "Initial commit"
git push origin master
Pushing works fine. I even deleted the entire repo from the local machine, and tried to clone it again.
cd ..
rd test /s
git clone ssh://mike#myLinuxServer/GitRepos/test.git
It worked fine and I got all of the files.
When I went to the remote Linux machine, I listed the files inside test.git and found it empty.
Where did the files go?
How can the repo receive pushes and give out files for pulls without storing the files in the repo?

The files are stored in the remote repository's database. You cannot expect that the files themselves appear in the remote repository, in particular not when you declare it as bare.
The way you initialized the bare remote repository is very unusual. You should have done it simply like this:
cd /GitRepos
git init --bare test.git

The repository on the server is considered as a bare repo as you ran git config core.bare true. A more common way to initialize a bare repo is git init --bare test.git or mkdir test.git;cd test.git;git init --bare. The difference between git init test.git;cd test.git;git config core.bare true and git init --bare test.git lies in the directory structure. The former creates the worktree(test.git) and the database(test.git/.git) but forbids the worktree then. The latter creates only the database(test.git), without the worktree from the beginning.
A bare repository does not have an active worktree by default, which means you can't check out files from any revision in a bare repository. Although you can create extra worktrees with git worktree add, it's quite rare and may behave not so well as a non-bare repository with extra worktrees. All the revisions are stored as compressed data in the database. You can use commands to read the data, like git show master or git ls-tree -r master.
otherwise pushing to this repo does not work (I tried!!)
It's partially true. You can't push to a non-bare repository when you try to update a branch which has been checked out in the remote repository. If the branch you try to update is not checked out or does not exist yet, you can push to update or create it. With git config receive.denyCurrentBranch ignore or git config receive.denyCurrentBranch warn, you can even update such branch. But in practice, we always use bare repositories to preserve revision data.

Related

move local repo to remote single file [duplicate]

This question already has answers here:
How do I move my local Git repository to a remote Git repository
(9 answers)
Closed 6 years ago.
I apologize if this has been asked somewhere else and I missed it. I've found many questions and answers that are close to what I'm trying to do, but none that fit exactly.
I have local files and projects in a local git repository. I created the repository a few months back as a way to safeguard and track my own work. The rest of our company uses SVN so I am the first to use git here. I set it up in my home directory on a Linux server. This repo contains many commits, branches, and tags that span my work over the past few months.
I want to move that repo to a more accessible location within our company's Linux server (not in my home directory) so others can clone and work with the entire repo (including all the tags, branches, history, etc.) as they see fit.
I've thought about copying or moving the entire file structure, but I want it to be a single file - i.e. myRepo.git.
How do I do this so that myRepo.git contains all the information I've committed such that when someone clones this repo and runs 'gitk --all', they will see all the branches, tags, etc.?
Thanks for your help.
You can git clone --bare your repo from somewhere other people can access it.
The most basic remote Git repo is nothing but a bare clone. A bare clone means it doesn't have a checkout, it's just the contents of the .git directory. Having a checkout would mean people might try to use that checkout as their own git working directory and make changes and that would get confusing with other people pushing changes to the repository.
For example, I'll make a new repository in foo/ and commit a file.
$ git init foo
Reinitialized existing Git repository in /Users/schwern/tmp/foo/.git/
$ rm -rf foo
$ git init foo
Initialized empty Git repository in /Users/schwern/tmp/foo/.git/
$ cd foo
$ touch this
$ git add this
$ git ci -m 'First commit'
[master (root-commit) 1a0bddf] First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 this
Then I'll make a bare clone of it into bar/ (cloning doesn't have to happen over a network).
$ git clone --bare foo bar
$ ls
HEAD config description hooks info objects packed-refs refs
Instead of the checkout, it has the contents of the .git directory.
Now I can clone from that.
$ git clone bar baz
Cloning into 'baz'...
done.
$ cd baz/
$ git log
commit 1a0bddf415f48d71fa3aacc79c07c233d91ed4a9 (HEAD -> master, origin/master, origin/HEAD)
Author: Michael G. Schwern <schwern#pobox.com>
Date: Tue Feb 14 12:01:13 2017 -0800
First commit
That's cloning on a local filesystem. Git will, of course, clone over a network. Typically it uses ssh or https. git clone user#host/path is really sshing into host as user and copying what's in path.
So you can put your repo anywhere the other folks have ssh access, like a dev server, or the same host that has the SVN server.
That's the most bare bones way to share your Git repository. There are plenty of more sophisticated Git servers out there that provide web access and more protocols. Everything from the very sparse GitWeb to Gitlab, a Github clone you can host locally for free.
You can read more about this in Pro Git's chapter on Git Servers.
A fully functional git repo is not a single file, and there is no way to make it so. That said, copying your working repo isn't the best thing to do either.
If having a single file is more important that having the repo be fully functional - i.e. you don't care if your coworkers can push to it - then you can create a bundle file.
git bundle create /path/to/my/repo.bundle --all
Your coworkers can clone from this, add it as a remote, pull or fetch from it... but they can't push to it (and neither can you; to add to it you'd have to recreate it).
A shared repo to be cloned from should normally be a bare repo. This is roughly equivalent to a copy of the .git directory from your working repo. If your existing repo is at /path/to/my/repo, you can create a suitable bare repo at /path/to/shared/repo by saying
cd /path/to/shared
git clone --mirror /path/to/my/repo
Note that I used --mirror instead of --bare; this has to do with how refs are copied and will work better if you intend this to be treated as the origin henceforth.
And yet at this point /path/to/my/repo is still considered the upstream. To fix that
cd /path/to/shared/repo
git remote remove origin
cd /path/to/my/repo
git remote add origin /path/to/shared/repo

Cannot clone git repo inside existing checked out git repository

I am trying to clone my a git repository inside an existing checked out git repository and getting this failure. I've done this workflow before so I cannot figure out why it would fail now. Does anyone have any suggestions?
The git repository does exist and I can clone outside of the checked out repository in a different location
Let's say I do the following
1. cd <existing git repo clone folder>
2. git clone https://github.com/apache/cassandra
Cloning Git Repository of cassandra
Cloning into 'cassandra'...
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
What could be causing this error?
PLEASE NOTE - I DO NOT WANT TO ADD THIS REPOSITORY AS A SUBMODULE TO MY PARENT GIT REPO. I simply want to figure out how to clone the repository in an existing working folder checked out from git.
Have a look at git submodules This was designed exactly for this. You can find information and examples here: https://git-scm.com/book/en/v2/Git-Tools-Submodules
In your case, this might work:
git submodule add https://github.com/apache/cassandra
In order to change the submodule to track a particular commit change directory to the submodule folder and switch branches as normal.
cd submodule
git checkout -b some_branch origin/some_branch
or for some particular tag
git checkout <version_tag>
You will need to commit this change to update.
Your question sort of seems to be in conflict. You can't have a git repo within another git repo without using git submodules AFAIK.
What you could do is have a dir that is not a git repo, and clone both the repos into that dir (so don't put the repos inside each other).
You could also add the repository as a remote (using git remote add name_of_remote http://your/remote/here). Then you can checkout any branch from either repo in the same repository.
I usually do not like to use submodules. for this case I would do the following:
1) in the main repo .gitignore the folder path where you want to store the repo the path-of-cassandra-repo/* (to ignore it)
2) in the terminal execute git clone https://github.com/apache/cassandra.git path-of-cassandra-repo/ where "path-of-cassandra-repo/" the name of the folder you want git to store the repo.
3) you are ready to go... Happy coding.
Let me know if this works for you...

How can I verify git clone is working correctly?

I'm following the documentation provided here by git to setup a bare git repository in a folder called root.
I started in the root directory where I ran
git init
git -A *
git commit -m "test"
I then ran git status and all appears good.
Next I ran the line from the documentation at a directory one level above the repo I created above.
git clone --bare root root.git
This created root.git but I cannot see any evidence that anything was cloned I just see a set of files and directories when I cd root.git.
I don't know how to verify it was actually cloned, and if it was why can't I see the original files?
When you do --bare --- you are telling git to clone just the git portion -
This is the option you use when you want to have a remote repository that does not include a workspace.
If you want to verify that it actually cloned your changes, you'll want to clone it again in a different directory - without the --bare flag
I would recommend using the full path to do this:
cd /path/to/some/workspace
git clone /path/to/your/root.git successful-git-clone #that last bit is optional
This will put the workspace contents of root.git into a folder named successful-git-clone/ - without that last bit, it will default to root/ -
Even if you are in a bare repository, some git commands works and you could do a git branch to see if you have all your branches or git log to look at your commits...

Added BitBucket repo as remote on GitHub

I've just installed git on Ubuntu, now I want to work on my repo in BitBucket. I'm a little confused on how to do so. I can't do the following:
git remote add BitBucketRepo git#bitbucket.org:dir/file.git
As it returns the following error:
fatal: Not a git repository (or any of the parent directories): .git
It clearly is pointing to a git repo, so why is it lying to me?
Also, it is worth noting I am using SSH and I have successfully paired my GitHub account to my computer.
You need to run this command from a local git repository (a directory in which you have run git init or git clone) - otherwise git remote doesn't know which local repo you want to add the remote for.
It should be as simple as cd my-local-dir, where my-local-dir is the directory containing your local (cloned) git repository.
If you don't yet have the repo available locally:
git clone git#github.com:...etc... my-local-dir
cd my-local-dir
git remote add ButbucketRepo git#bitbucket.org...
git push -u ButbucketRepo master
This will clone your code from Github into the my-local-dir directory, add your BitBucket repo as a remote repository, push your code up to Bitbucket and set the local master branch to track the BitBucket remote's master branch.
Tracking means that commands that involve a remote like git push will automatically use the BitBucket remote's master branch. If you don't want that behaviour, skip the -u option.

Unable to create a bare git repository

My git version is 1.5.0.6.
I want to create a bare git repository called sample.git.
I tried all sorts, but it failed:
Try 1:
Gives a usage statement but no git created
git init --bare sample.git
usage: git-init [--template=<template-directory>] [--shared]
Try 2: (using git-init not git init)
$ git-init --bare sample.git
usage: git-init [--template=<template-directory>] [--shared]
Try 3: Create a directory sample and then run same command, but still same output and git repository not created.
Please help me.
Firstly, upgrade your git. Version 1.5 is positively ancient.
If you can't upgrade, I'd just create a normal git repository, then manually convert it to a bare repo. An ordinary git repo will contain a '.git' directory. This can be your "bare" repo. Rename this directory to whatever you like. (You'll probably want to mv .git ../myrepo.git).
Then you have to tell git that it is a bare repo. Do this by adding bare = true to the config file in the [core] configuration section.

Resources