When I was using Mercurial, creating a remote repository is one command line:
hg clone local remote
Where remote is something accessible via ssh. For instance:
hg clone /path/to/local/repo.hg ssh://host.name/path/to/new/clone.hg
I don't know if it is a functionality in Mercurial or we have installed something to make that possible.
Is there any way to do that with git? Or install something that will allow me to do the same like with Mercurial?
NOTE
My question is not 'How to clone a remote repository?'
No, you can't do that with a single command, and you can't do that using Git alone either.
You roll something like this:
Initialize a repository on the server:
ssh user#server git init --bare /path/to/the/new/repo
Push your local repository there:
git push --all --tags ssh://user#server/path/to/the/new/repo
If you intend to work with that new remote repository (push/fetch) afterwards, it makes sense to add it as a named remote, so your step #2 becomes:
git remote add foo ssh://user#server/path/to/the/new/repo
git push --all --tags foo
Related
I'm working on linux server (non-desktop) and I have two repositories. One is for development (github), the other is a local repository of my lab (gitlab).
What I need is to keep the two repositories always exactly the same, and I can push and pull on my dev repository, whereas only push on my local repository so that my colleagues can git pull from my lab repository. My lab-repository is used only for sharing my project with my colleagues: I do nothing but git push and they do nothing but git pull. That's all.
Well, you may ask why don't use the dev repository. That's because they have no right to connect to the Internet.
I don't like to execute many commands for each action. For example, for now I have to execute the commands below to git push to the two repositories:
cd devRepo
# coding here
# git add & git commit & git push here
cd ../myLabRepo
cp -r ../devRepo/* ./
# git add & git commit & git push here
This is kind of boring. Ofc, I know I can use bash script but I'm wondering if there are some specialized tool to do so or maybe Git has the capacity to do so.
You can specify more than one push location for the same remote.
git remote set-url --add --push <remote name> <push url>
Check this with git remote -v
You should see the original push and fetch URLs, along with the extra push URL you just added.
You can add the whole stuff that you have into a bash script and run that. Otherwise, you can add a remote to your first repository and do a push there as well, instead of copying your whole project.
git add & git commit & git pull origin master & git push origin master & git push local master
So I think I am confused - but for some reason I cannot push or pull to my git repository.
I have a linux web server and have a web folder in /var/www/bcs.net.nz/
I did a git init bcs.git in this folder (I have also tried .git)
and then I thought I could do git clone git#bcs.net.nz:bcs.git to clone it on the local machine or git push on the remote machine.
I have also added a git remote add origin git#bcs.net.nz:bcs.git on the remote machine.
After all of that I still cannot push and pull anything.
I am a bit stuck.
git add . & git commit -m "initial commit" work fine.
git clone git#bcs.net.nz:bcs.git would mean to clone a (preferably bare repo) which is in /home/git/bcs.git
You could initialize one there, (git init --bare /home/git/bcs.git), and add a post-receive hook (chmod 755 /home/git/bcs.git/hooks/post-receive) which will checkout the code in the live server folder
#!/bin/sh
git --work-tree=/var/www/bcs.net.nz --git-dir=/home/git/bcs.git checkout -f
I have a node.js project local in my computer, right now, I want to push this project to remote Ubuntu server. I have create a project.git on Ubuntu server, and pushed to it. Right now, I want to run this project on the server, but how do I access this folder, it's just a git directory. The process I am following is like below:
Ubuntu server:
mkdir /home/git/project
cd /home/git/project
git init
Local computer:
git remote add origin root#someserver.com:/home/git/project
git add .
git commit -am "Initial Commit"
git push origin master
It push succeed. Right now, I want to execute this node.js project and run it on the server, but how to turn this git repository to a folder, so I can cd into it and then node app.js after?
If you want to see the code on the server side, you should init the repository without --bare option or clone it somewhere else on the server.
Bare repositories store only git specific files.
UPDATE:
If you create a repository without --bare option, you can't push anything to that branch where your server is staying. Better solution is if you create a bare repository and clone it somewhere else on the server. Notice that, you should use there git pull command if you want to see your fresh code.
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.
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.