How to push a local Git repository to another computer?

GitGit Push

Git Problem Overview


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

How can I do that?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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/

Solution 3 - Git

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnubelaView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitkubiView Answer on Stackoverflow
Solution 3 - GittakeshinView Answer on Stackoverflow