Setting up a new remote git Repository

Source: Internet
Author: User
Tags using git

To collaborate in a distributed development process you'll need to push code to remotely accessible repositories.

This is somewhat of a follow-up to the previous article setting up a new rails app with git.

It's good to know how this stuff works, but this is definitely the hard way. For no fuss git repository setup (especially if you want to collaborate with others) Check out GitHub.

For the impatient

Set up the new bare Repo On the server:

$ ssh myserver.com
Welcome to myserver.com!
$ mkdir /var/git/myapp.git && cd /var/git/myapp.git
$ git --bare init
Initialized empty Git repository in /var/git/myapp.git
$ exit
Bye!

Add the remote repository to your existing local git repo and push:

$ cd ~/Sites/myapp
$ git remote add origin ssh://myserver.com/var/git/myapp.git
$ git push origin master

Set the local master branch to track the remote branch.

Read further for a step-by-step explanation of what's going on.

Pre-flight sanity check

Setting up a remote repository is fairly simple but somewhat confusing at first. Firstly, let's check out what remote repositories are being tracked in our git repo:

$ cd ~/Sites/myapp
$ git remote

None. Looking good. Now let's list all the branches:

$ git branch -a
* master

Just one branch, the master branch. Let's have a look.git/config:

$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true

A pretty bare-Minimum config file.

Creating the bare remote Repository

Before we can push our local master Branch to a remote repository we need to create the remote repository. To do this we'll SSH in and create it on the server:


$ ssh myserver.com
Welcome to myserver.com!
$ cd /var/git
$ mkdir myapp.git
$ cd myapp.git
$ git --bare init
Initialized empty Git repository in /var/git/myapp.git
$ exit
Bye!

A short aside about what git meansBare: A default git repository assumes that you'll be using it as your working directory, So git stores the actual bare repository files in.gitDirectory alongside all the project files. remote repositories don't need copies of the files on the filesystem unlike Working Copies, all they need are the deltas and binary what-nots of the Repository itself. this is what "bare" means to git. just the repository itself.

Adding the remote repository to our local git repository Configuration

Now that we're 've created the remote repository we'll add it to our local repository as a remote server called "Origin" usinggit remote add, Which is just a nicer way of updating our config file for us:


$ git remote add origin ssh://myserver.com/var/git/myapp.git

Let's see what it added to the config file:

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = ssh://myserver.com/var/git/myapp.git
fetch = +refs/heads/*:refs/remotes/origin/*

We now have a remote repository "Origin" that will fetch all of it'srefs/heads/*Branches and store them in our local repo inrefs/remotes/origin/*Whengit fetchIs already med.

Pushing to the remote Repository

The time has come to push our local master branch to the origin's master branch. We do that usinggit push <target> <local>Command.


$ git push origin master
updating 'refs/heads/master'
from 0000000000000000000000000000000000000000
to b379203bc187c2926f44a71eca3f901321ea42c6
Also local refs/remotes/origin/master
Generating pack...
Done counting 1374 objects.
Deltifying 1374 objects...
100% (1374/1374) done
Writing 1374 objects...
100% (1374/1374) done
Total 1374 (delta 89), reused 0 (delta 0)
refs/heads/master: 0000000000000000000000000000000000000000 -> b379203bc187c2926f44a71eca3f901321ea42c6

And that's all, folks. Further pushes can be done by repeatinggit pushCommand.

Now you can tell your co-conspirators:

$ git clone ssh://myserver.com/var/git/myapp.git

And push and pull to your heart's content.

Track the remote Branch

You can specify the default remote repository for pushing and pulling using Git-branch's track option. You 'd normally do this by specifying--trackOption when creating your local master branch, but as it already exists we'll just update the config manually like so:

[branch "master"]
remote = origin
merge = refs/heads/master

Now you can simplygit pushAndgit pull.

Sharing the remote repository with the world

If you want to set it up as a public repository be sure to check out the GIT Manual's chapter on public git repositories.

Working with remote repository branches

git remote showIs used to inspect a remote repository. It goes and checks the remote repository to see what branches have been added and removed since the lastgit fetch.

Doinggit remote showAt the moment only shows us the remote repo's master branch which we pushed earlier:

$ git remote show origin
* remote origin
URL: ssh://myserver.com/var/git/myapp.git
Tracked remote branches
master

Let's create a new local git repository and push to a new branch on the remote repository. We can then usegit remote showTo see the new remote branch,git fetchTo mirror it into our local repo andgit checkout --track -bTo create a local branch to do some work on it.

We'll start by creating a new local repo and pushing some code to a new branch in the remote repository.

$ mkdir /tmp/other-git
$ cd /tmp/other-git
$ git init
Initialized empty Git repository in /tmp/other-git
$ git remote add origin ssh://myserver.com/var/git/myapp.git
$ echo "Rails 2... woo" > afile
$ git add afile
$ git commit -m "Added afile"
Created initial commit 0ac9a74: Added afile
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 something
$ git push origin master:rails-2
updating 'refs/heads/rails-2' using 'refs/heads/master'
from 0000000000000000000000000000000000000000
to 0ac9a7457f4b21c9e058d4c54d262584bf35e528
Also local refs/remotes/origin/rails-2
Generating pack...
Done counting 3 objects.
Deltifying 3 objects...
100% (3/3) done
Writing 3 objects...
100% (3/3) done
Total 3 (delta 0), reused 0 (delta 0)
Unpacking 3 objects...
100% (3/3) done
refs/heads/rails-2: 0000000000000000000000000000000000000000 -> 0ac9a7457f4b21c9e058d4c54d262584bf35e528

Now let's switch back to our old git repository and see if it detects the new branch on the remote Repository:

$ git remote show origin
* remote origin
URL: ssh://myserver.com/var/git/myapp.git
New remote branches (next fetch will store in remotes/origin)
dev/rails-2
Tracked remote branches
master

Let's update our mirror of the remote repository by doinggit fetch:

$ git fetch
* refs/remotes/origin/master: storing branch 'dev/rails-2' of ssh://myserver.com/var/git/myapp.git
commit: b379203
$ git remote show origin
* remote origin
URL: ssh://myserver.com/var/git/myapp.git
Tracked remote branches
master
dev/rails-2

We shoshould now be able to see this in a our list of remote branches:

$ git branch -a
* master
origin/dev/rails-2
origin/master

If we then wanted to do some work on this remotedev/rails-2Branch we create a new local tracking branch like so:

$ git checkout --track -b dev/rails-2 origin/dev/rails-2
Branch dev/rails-2 set up to track remote branch refs/remotes/origin/dev/rails-2.
Switched to a new branch "dev/rails-2"

To keep up-to-date and push new changesets we simply usegit pushAndgit pullWhen working in the localdev/rails-2Branch.

Also notice, like we manually changed for Master,.git/configHas a new entry for this new tracking Branch:

[branch "dev/rails-2"]
remote = origin
merge = refs/heads/dev/rails-2


From http://toolmantim.com/articles/setting_up_a_new_remote_git_repository

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.