68

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

How can I do that?

5
  • 4
    git should be in superusers.com
    – vodkhang
    Commented May 22, 2010 at 12:18
  • 1
    Have you read the man page? typed git help push?
    – crazyscot
    Commented May 22, 2010 at 12:28
  • 15
    @vodkhang: no it should not be on superuser. It is an important part of a developer toolbox and his/her programming environment.
    – VonC
    Commented May 22, 2010 at 12:47
  • uhm, that can be right. I just thought because it relates to some administration stuff so it should be on that page:)
    – vodkhang
    Commented May 22, 2010 at 13:00
  • 6
    @vodkhang I asked some questions about git setup on serverfault.com too, but I think the real git users with best git knowledge are here.
    – takeshin
    Commented May 22, 2010 at 20:56

3 Answers 3

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.

9
  • git push desktop means push from laptop to desktop. A bare repo is definitely not needed for this.
    – Timo
    Commented Nov 15, 2017 at 10:23
  • @Timo Yes, since 2015, a non-bare repo is possible: stackoverflow.com/a/30030236/6309
    – VonC
    Commented Nov 15, 2017 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
    Commented Dec 14, 2018 at 14:45
  • @AstroFloyd 8+ years later, you can use SSH, considering Windows 10 comes with a OpenSSH server (bleepingcomputer.com/news/microsoft/…, bleepingcomputer.com/news/microsoft/…)
    – VonC
    Commented Dec 14, 2018 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
    Commented Dec 14, 2018 at 16:07
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
# [email protected]
# 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/
1
  • 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 (blog.stackoverflow.com/2009/06/…), you are sure this script will always be available. Otherwise, +1
    – VonC
    Commented May 22, 2010 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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.