67

I have a local Git repository setup on my laptop. I would like to push it to my desktop.

How can I do that?

nubela
  • 1
  • 24
  • 75
  • 123

3 Answers3

59

If you have access to a shared directory, you can (see git clone and git remote):

git clone --bare /path/to/your/laptop/repo /shared/path/to/desktop/repo.git
git remote add desktop  /shared/path/to/desktop/repo.git

That will create a bare repo, referenced in your local repo as "desktop".
Since it is bare, you can push to it (as well as pull from it if needed)

git push desktop

As the ProGit book mentions, git does support the file protocol:

The most basic is the Local protocol, in which the remote repository is in another directory on disk.
This is often used if everyone on your team has access to a shared filesystem such as an NFS mount, or in the less likely case that everyone logs in to the same computer.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `git push desktop` means push `from` laptop `to` desktop. A `bare` repo is definitely not needed for this. – Timo Nov 15 '17 at 10:23
  • @Timo Yes, since 2015, a non-bare repo is possible: https://stackoverflow.com/a/30030236/6309 – VonC Nov 15 '17 at 10:27
  • What if I don't have access to a shared directory? I'd like to do something like `git clone . ssh://user@desktop:git/remoteRepo.git` (from my local repo). Or can this only be done by logging into my desktop and creating a (bare) repo there? – AstroFloyd Dec 14 '18 at 14:45
  • @AstroFloyd 8+ years later, you can use SSH, considering Windows 10 comes with a OpenSSH server (https://www.bleepingcomputer.com/news/microsoft/heres-how-to-enable-the-built-in-windows-10-openssh-client/, https://www.bleepingcomputer.com/news/microsoft/how-to-install-the-built-in-windows-10-openssh-server/) – VonC Dec 14 '18 at 14:52
  • @VonC no need for Windows here; OpenSSH has been around on Linux for ages. However `git clone . ssh://user@desktop:git/remoteRepo.git` creates a local repo in a subdirectory called ssh:/user@desktop:git/remoteRepo.git whereas I'd like a *remote* repo - that's the whole point. So how do I connect to the remote machine over ssh rather than creating a directory called ssh. – AstroFloyd Dec 14 '18 at 16:07
  • @AstroFloyd You did not mentioned your OS. And could you try the URL: `ssh://user@desktop/full/path/to/git/remoteRepo.git` (so no '`:`') – VonC Dec 14 '18 at 16:14
  • @VonC Sorry, using Linux locally and remotely. That (full command being `git clone . ssh://user@desktop/home/user/path/to/git/remoteRepo.git`) creates a subdirectory tree `./ssh:/user@desktop/home/user/path/to/git/remoteRepo.git` in the current dir of my local box... :( – AstroFloyd Dec 14 '18 at 17:38
  • @AstroFloyd What version of git are you using? I assume that on the remote desktop, the path `/home/user/path/to/git/remoteRepo.git` does exist? – VonC Dec 14 '18 at 18:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185285/discussion-between-astrofloyd-and-vonc). – AstroFloyd Dec 15 '18 at 08:44
5

Here's a script that I wrote to do just this thing. The script handles all my usual initialization of new git repos

  1. creates .gitignore file
  2. initializes .git
  3. creates the bare git repo on the server
  4. sets up the local git repo to push to that remote repo

http://gist.github.com/410050

You'll definitely have to modify it to suit whatever setup you've got, especially if you're dealing with Windows laptop/desktop.

Here is the full script:

#!/bin/bash
# Create Git Repository
# created by Jim Kubicek, 2009
# jimkubicek@gmail.com
# http://jimkubicek.com

# DESCRIPTION
# Create remote git repository from existing project
# this script needs to be run from within the project directory

# This script has been created on OS X, so YMMV

#######
# Parameters
REPLOGIN=#Login name
REPADDRESS=#Repo address
REPLOCATION=/Users/Shared/Development #Repo location

# The repo name defaults to the name of the current directory.
# This regex will accept foldernames with letters and a period.
# You'll have to edit it if you've got anything else in your folder names.
REPNAME=`pwd | egrep -o "/[a-zA-Z]+$" | egrep -o "[a-zA-Z\.]+"`


# If you have standard files/directories to be ignored
# add them here
echo "Creating .gitignore"
echo 'build/' >> .gitignore # The build directory should be ignored for Xcode projs
echo '.DS_Store' >> .gitignore # A good idea on OS X

# Create the git repo
echo "Initializing the repo"
git init
git add .
git commit -m "Initial commit"

# Copy the repo to the server
echo "Copying the git repo to the server $REPADDRESS"
TEMPREP="$REPNAME.git"
git clone --bare .git $TEMPREP
scp -r $TEMPREP $REPLOGIN@$REPADDRESS:$REPLOCATION/
rm -rf $TEMPREP

# Set up the origin for the project
echo "Linking current repository to remote repository"
git remote add origin $REPLOGIN@$REPADDRESS:$REPLOCATION/$REPNAME.git/
rbaleksandar
  • 8,713
  • 7
  • 76
  • 161
kubi
  • 48,104
  • 19
  • 94
  • 118
  • 2
    I would recommend copying your script in full in your answer (since it is not too big). Once Stack Overflow release the next cc-wiki dump (http://blog.stackoverflow.com/2009/06/stack-overflow-creative-commons-data-dump/), you are sure this script will *always* be available. Otherwise, +1 – VonC May 22 '10 at 12:59
2

The easiest (not the best) way is to share the repository directory via LAN, and use git's file:// protocol (see man git).

For me, the best way is to use gitolite (see gitolite docs for detailed instructions).

takeshin
  • 49,108
  • 32
  • 120
  • 164