132

I'm just learning Git and there is something I can't work out. After creating and using a git repository locally on my Mac, can I push a copy to another server somewhere else? I am behind a firewall so unfortunately I can't run git clone from the other machine.

swimfar
  • 147
  • 6
Jay
  • 19,649
  • 38
  • 121
  • 184

5 Answers5

141
  1. git remote add name url
  2. git push name branch

Example:

git remote add origin git@github.com:foo/bar.git
git push origin master

See the docs for git push -- you can set a remote as the default remote for a given branch; if you don't, the name origin is special. Just git push alone will do the same as git push origin thisbranch (for whatever branch you're on).

August Lilleaas
  • 54,010
  • 13
  • 102
  • 111
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • 7
    For a closer effect as a **"push cloning"** rather than just *pushinh a _branch_*: [`git push -u --all`](http://linux.die.net/man/1/git-push) we can omit `origin` as it's the default assumed name for pushing and `-u` will also track the remote branches. – cregox Dec 02 '11 at 18:34
  • 14
    It should be enough to create an empty bare repository at the server using `git init --bare /foo/bar.git` and then push your local one there (like in this answer). You do not need to transfer anything with zipped/tared to the server. – OderWat Nov 20 '12 at 12:13
  • 1
    Seconding OderWat. For the example, do the 'git init --bare foo/bar.git' while logged into the remote (i.e. git@github.com) then the 'remote origin' will have a destination to push into. For the github example, I think you could use github's Repository/Add web interface and then follow the "Push an existing repository from the command line" advice it provides. Github advises a '-u' to cause a tracking reference. – Dave X Aug 08 '13 at 17:29
  • 1
    This method may be canonical, but it is obtuse and makes git less usable. I love me some git, but good heavens does it have some UI issues.. – Ross Rogers Nov 03 '13 at 22:54
  • I took the liberty to remove the "EDIT" paragraph that was added to the original question, that made it look like this procedure was questionable. I don't think it is, and it should probably have been a comment instead of a question edit anyway. – August Lilleaas Jun 02 '15 at 07:22
  • This is a wrong method. When I do this, git push responds: `fatal: '~/system.git': unable to chdir or not a git archive fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.` It seems you must first create a git repository on the remote! – Yaroslav Nikitenko Jan 07 '16 at 15:10
  • Do you have to have a bare repo at foo/bar.git for this to work? – faceclean May 07 '19 at 11:58
77

What you may want to do is first, on your local machine, make a bare clone of the repository

git clone --bare /path/to/repo /path/to/bare/repo.git  # don't forget the .git!

Now, archive up the new repo.git directory using tar/gzip or whatever your favorite archiving tool is and then copy the archive to the server.

Unarchive the repo on your server. You'll then need to set up a remote on your local repository:

git remote add repo-name user@host:/path/to/repo.git #this assumes you're using SSH

You will then be able to push to and pull from the remote repo with:

git push repo-name branch-name
git pull repo-name branch-name
Grant Limberg
  • 20,913
  • 11
  • 63
  • 84
  • 4
    Could you explain the comment "don't forget the .git!"? Where is the point in adding .git to end of the folder? When should I do this? A convention only? – lumbric Jul 23 '11 at 21:25
  • 5
    The ".git" is just the unofficial standard for identifying a bare git repo – Grant Limberg Jul 24 '11 at 02:17
  • 4
    I'm not following this idea... doesn't this break the whole purpose of setting up a git server and actually *using it* to "transfer / copy / keep things in sync / backup" rather than *zipping* and using some other method? – cregox Dec 02 '11 at 18:19
  • 1
    This is exactly what I needed, because the original repo was on a network that didn't have access to the network where the server I wanted to push to existed. This allowed me to put the repo on a box that did have access. – michaeltwofish Mar 15 '12 at 05:28
  • 4
    @Cawas you only have to manually zip/copy/extract the repo once. after that all you need is git push and pull. – sparebytes Oct 18 '12 at 04:18
  • Ah yes @sparebytes... 1 year later I'm not following my comment now!:P – cregox Oct 18 '12 at 10:18
  • This is not needed. You can create an empty bare repository and push to it. – OderWat Nov 20 '12 at 12:15
  • 1
    if I done with this, where can I find my version controled files in remote repository? the repo.git only contains `branches/` `hooks/` `info/` `objects/` `refs/` folders – laplasz Jan 23 '13 at 16:07
  • 1
    also when using `git status` I got tihs: `fatal: Not a git repository` – laplasz Jan 23 '13 at 16:13
  • 1
    @laplasz If I am not mistaken, bare repository just holds your repository, but not ht e current working copy. If you need a working copy just `git clone` from that bare repo - you will then be able to push/pull there if you set it as a remote. – kervich Feb 11 '13 at 08:09
  • @kervich Thank you, that was the last puzzle piece! – JamesB41 Jun 22 '13 at 03:00
9

There are many ways to move repositories around, git bundle is a nice way if you have insufficient network availability. Since a Git repository is really just a directory full of files, you can "clone" a repository by making a copy of the .git directory in whatever way suits you best.

The most efficient way is to use an external repository somewhere (use GitHub or set up Gitosis), and then git push.

slang
  • 626
  • 7
  • 26
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
8
remote server> cd /home/ec2-user
remote server> git init --bare --shared  test
add ssh pub key to remote server
local> git remote add aws ssh://ec2-user@<hostorip>:/home/ec2-user/dev/test
local> git push aws master
jazzed
  • 382
  • 3
  • 6
  • I like the simplicity of your answer, but I'm not sure whether it's what I need or not. I have an existing GIT repo on GitHub, and also now want to push to the live files our Ubuntu server (which has GIT installed). Can you add some comments to make the process clearer? – Dave Everitt Aug 10 '18 at 17:01
2

You can push a branch to a remote server, say github. You would first have to do the initial project setup, then clone your project and:

git push <remote repo> <your branch>
Cesar
  • 5,488
  • 2
  • 29
  • 36