Setup Git Local and Remote Repositories

If you are a remote worker or, like me get some work done on the train on the way to/from your office, you’ll appreciate the need for set up of a local and remote source code repository, This allows you to develop using your laptop/netbook, check in your changes locally and when you arrive at the mothership later, you can synchronise your local repository with the remote one ensuring all your changes are available for other developers. This article shows how to set up git for both a mothership type repository (let’s call it the remote repository) and a local version on your own laptop.

Configuring Git project on the mothership.

Prerequisites:

A Git server (the mothership) has been set up on a box called bluelight. This box is available to the network as git.petermac.com.

A git user has been created on the server called ‘git’. This user has access to the folder where the git repositories are stored.

The Steps:

1.On your dev machine create your code project using whatever tools you need.

2.Initialise this working project under the git version control system

$ cd ~/projects/[myprojectname]
$ git init
peter@peter-desktop:~/Projects/rentmanager$ git init
Initialized empty Git repository in /home/peter/Projects/rentmanager/.git/

3.Add whatever work you’ve done to the repository

$ git add app/
$ git add docs/

4.Check the files you want added have been added

$ git status
# On branch master
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#    new file: app/rentmanager/,
#

5.Open a SSH session to bluelight – (the central git repository server)

$ sudo mkdir /usr/local/share/gitrepos/[myprojectname].git

6.Obviously substitute your real projectname and don’t forget to leave the .git extenstion

7.Initialise the repository under the new folder

$cd /usr/local/share/gitrepos/[myprojectname].git

[peter@bluelight myprojectname.git]$ sudo git init
Initialized empty Git repository in /usr/local/share/gitrepos/myprojectname.git/.git/

8.Change ownership of the repository to the system git user

$cd ..; sudo chown git.git -R /usr/local/share/gitrepos/myprojectname.git

Configure git on your laptop to use the remote repository

1. Return back to the local machine and add reference to the new ‘remote’ repository from the base directory of the project.

$ git remote add remote ssh://git at git.petermac.com/usr/local/share/gitrepos/myprojectname].git

2. Here the ‘git remote add’ part says add a reference to a remote repository. The second ‘remote’ is the friendly name I want to use when referring to the repository on the git server

3. Now commit the local files to the local repository – Note: Step 3 was only an add, not a commit. When you commit you’ll be prompted (or you can enter it as a -m option) to enter a message to be used as a comment.

peter@peter-desktop:~/Projects/myprojectname$ git commit
Created initial commit 633fd3c: initial checkin of project core and data migration files

4. It’s time to test the new remote repository by ‘pushing your local repository info up to it. This is done using git push

peter@peter-desktop:~/Projects/myprojectname$ git push –dry-run –all --repo=remote
fatal: 'origin': unable to chdir or not a git archive
fatal: The remote end hung up unexpectedly

Didn’t quite go to plan – so let’s see what’s wrong

peter@peter-desktop:~/Projects/myprojectname$ git remote show remote
The authenticity of host 'git.petermac.com (192.168.0.15)' can't be established.
RSA key fingerprint is 5a:ce:6e:a4:78:d5:01:50:36:2b:bb:12:67:e1:be:53.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '
git.petermac.com' (RSA) to the list of known hosts.
git at git.petermac.com'
s password:
* remote remote
URL: ssh://git at git.petermac.com/usr/local/share/gitrepos/myprojectname.git

let’s try again

$ git push –dry-run –all –repo=remote
git at git.petermac.com's password:
To ssh://git at git.petermac.com/usr/local/share/gitrepos/myprojectname.git
[new branch]      master -> master

looks like it will work so remove the dry-run parameter

$ git push –all –repo=remote

That’s all folks!

Share

No Responses to “Setup Git Local and Remote Repositories”

Leave a Reply



*

Spam protection by WP Captcha-Free