move local repo to remote single file [duplicate] - linux

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

Related

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

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.

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...

git post-receive checkout to remote machine?

If you add the following script as a hooks/post-receive hook to a bare git repository foo.git:
#!/bin/sh
GIT_WORK_TREE=/bar git checkout -f
then whenever someone pushes to the repository the current state will be updated in directory bar.
This requires bar and foo.git to be on the same machine.
What's the easiest way to modify it so that the checkout is made on a remote machine (say baz:/bar) ?
One way would be to:
#!/bin/sh
GIT_WORK_TREE=/tmp/bar git checkout -f
rsync ... /tmp/bar baz:/bar
Is there a better way? Perhaps not requiring an intermediate temp directory? (If not what are the correct options to pass to rsync such that the resulting directory is indentical to being checked out directly?)
GIT_WORK_TREE=/bar means that bar is a git repo.
If bar is a git repo on the remote side, then it can pull from a bare repo bare_bar.git (also on the remote side), to which you can push to.
In other words, your post-receive hook would push to bare_bar repo through ssh, and a post-receive hook on that bare repo would trigger the pull from the actual repo bar: see "Creating a git repository from a production folder".
I'd prefer a solution that keeps the remote server free of any .git dirs (and even the git package itself ideally)
In that case, your current post-receive hook and its rsync command seems to be the only way to incrementally copy new data to a remote working tree.
git archive would archive everything each time.
git bundle would require git on the other side.

Git - Syncing a Github repo with a local one?

First off, forgive me if this is a duplicate question. I don't know anything but the basic terminology, and it's difficult to find an answer just using laymen's terms.
I made a project, and I made a repository on Github. I've been able to work with that and upload stuff to it for some time, on Windows. The Github Windows application is nice, but I wish there was a GUI for the Linux git.
I want to be able to download the source for this project, and be able to edit it on my Linux machine, and be able to do git commit -m 'durrhurr' and have it upload it to the master repository.
Forgive me if you've already done most of this:
The first step is to set up your ssh keys if you are trying to go through ssh, if you are going through https you can skip this step. Detailed instructions are provided at https://help.github.com/articles/generating-ssh-keys
The next step is to make a local clone of the repository. Using the command line it will be git clone <url> The url you should be able to find on your github page.
After that you should be able to commit and push over the command line using git commit -am "commit message" and git push
You can use SmartGit for a GUI for git on Linux: http://www.syntevo.com/smartgit/index.html
But learning git first on the command line is generally a good idea:
Below are some basic examples assuming you are only working from the master branch:
Example for starting a local repo based on what you have from github:
git clone https://github.com/sampson-chen/sack.git
To see the status of the repo, do:
git status
Example for syncing your local repo to more recent changes on github:
git pull
Example for adding new or modified files to a "stage" for commit
git add /path/file1 /path/file2
Think of the stage as the files that you explicitly tell git to keep track of for revision control. git will see the all the files in the repo (and changes to tracked files), but it will only do work on the files that you add to a stage to be committed.
Example for committing the files in your "stage"
git commit
Example for pushing your local repo (whatever you have committed to your local repo) to github
git push
What you need to do is clone your git repository. From terminal cd to the directory you want the project in and do
git clone https://github.com/[username]/[repository].git
Remember not to use sudo as you will mess up the remote permissions.
You then need to commit any changes locally, i.e your git commit -m and then you can do.
git push
This will update the remote repository.
Lastly if you need to update your local project cd to the required directory and then:
git pull
To start working on the project in linux, clone the repo to linux machine. Add the ssh public key to github. Add your username and email to git-config.
For GUI you can use gitg.
PS : Get used to git cli, It is worth to spend time on it.

Resources