Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic set-up of remote git repository on a standard server

Set-up remote git repository on a standard server

The first thing to do is to install Git on the remote server.

Once you do that the rest of the process is split into three sections:

  1. Server set-up
  2. Local set-up (push commits)
  3. Server (pull commits)

Server set-up

  • ssh -pxxxx username@xxx.xxx.xxx.xxx (this is you connecting to your remote server)
  • cd ../ (this gets you to the 'absolute root' of the server)
  • cd www/..../ (navigate to the directory one level above your website directory - e.g. your website directory being where you would upload your HTML files etc)

Note: if (for example) your web directory is httpdocs then move up one level from there.

The following example assumes httpdocs is your web directory...

  • rm -rf httpdocs (remove the web directory - you'll recreate it again in a minute)
  • mkdir httpdocs && cd httpdocs (create new web directory folder and move inside it)
  • git init (initiate new git repo)
  • cd ../ (jump back up a directory level)

The following three commands are the black magic for getting a remote git repo setup:

  • git clone --bare httpdocs httpdocs.git
  • mv httpdocs httpdocs.backup
  • git clone httpdocs.git

Local set-up (push commits)

  • cd ~/Desktop/Sites/myWebsite
  • git init
  • git add *
  • git commit -m 'Start of new project'
  • git remote add origin ssh://username@xxx.xxx.xxx.xxx:xxxx/www/.../httpdocs.git
  • git push origin master

Server (pull commits)

  • cd ../
  • cd www/..../httpdocs/
  • git fetch
  • git diff origin/master
  • git merge origin/master
@miknonny

This comment has been minimized.

Copy link

commented May 14, 2013

I am new to git and have been looking for this thanks!!

@liaoclarke

This comment has been minimized.

Copy link

commented May 18, 2013

Good job, thanks

@tyleretters

This comment has been minimized.

Copy link

commented Jul 3, 2013

Dat black magic.

@sunrum

This comment has been minimized.

Copy link

commented Sep 3, 2013

Awesome, thanks!

@osadan

This comment has been minimized.

Copy link

commented Feb 13, 2014

worked like a charm

@jamiehollern

This comment has been minimized.

Copy link

commented Apr 6, 2014

If anyone is using an IP address as the host and gets the error "fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists."

Ensure that you use a port number for the remote (22 is the standard ssh port). For example:
git remote add origin ssh://username@127.0.0.1:22/www/.../httpdocs.git

@zrls41

This comment has been minimized.

Copy link

commented Aug 10, 2014

Not sure what elements in your instruction are actual keywords, or to be typed in literally... and which terms in the instructions are to be replaced with specific detail code for a given site. could you clarify which part of the instructions have to be typed in exactly as shown, and which ones are replaced with specific code and pathing.

@juliardi

This comment has been minimized.

Copy link

commented Sep 25, 2014

Awesome!! but, could you make it automatically fetch and merge, so we don't need to fetch it from the server every time we make a push?
sorry for bad english

@jajourda

This comment has been minimized.

Copy link

commented Oct 30, 2014

thanks so much; +1 for juliardi's question :)

@rwolst

This comment has been minimized.

Copy link

commented Mar 11, 2015

Really useful, thanks

@JoshMcCullough

This comment has been minimized.

Copy link

commented Apr 17, 2015

cd ../ doesn't get you to the "absolute root" of the server, just the parent directory. cd / is the absolute root.

@Fuentealba

This comment has been minimized.

Copy link

commented Dec 29, 2015

this is good stuff, thanks!

@muyaedward

This comment has been minimized.

Copy link

commented Nov 30, 2017

Very helpful

@johnchrishays

This comment has been minimized.

Copy link

commented Aug 19, 2018

Worked great! Would be helpful to have explanation for key commands (e.g. the black magic)

@adamkendall1

This comment has been minimized.

Copy link

commented Oct 8, 2018

Thank you so much! This is so simple to follow your instructions and get up and running with git repos.

This was super helpful. Thanks!

@simo97

This comment has been minimized.

Copy link

commented May 24, 2019

thanks for this man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.