How do I move my local Git repository to a remote Git repository
GitGit Problem Overview
I have various Git projects that are on my local machine. I have a server that I would like to use as my remote Git Repository. How do I move my local Git Repositories (Projects) to my server while keeping the history intact?
Thanks!
EDIT: Thanks for all the great answers. The response I choose makes sense to my limited GIT knowledge.
EDIT #2:
I noticed my original answer selection did not copy my tags. git push --mirror <path>
does copy tags.
Git Solutions
Solution 1 - Git
On your server create the git repositories as bare repository
git init --bare repo.git
then, push the commits of your local repository
git push --mirror ssh://yourserver/~/repo.git
Solution 2 - Git
First, create a git repo on your server
git init --bare /path/to/repo
Then add the remote repo to your local one (ssh:// or https://)
git remote add origin ssh://server/path/to/repo
And, push files/commits
git push origin master
And finally, push tags
git push origin --tags
Solution 3 - Git
There is a good tutorial on Ralf Wernders blog. Assuming you know how to create a repository on the server, or that has already been done:
git remote add <remote> <url>
To add a remote to your local repository. <remote>
is the name of the remote (often "origin"). <url>
is the url to your repository with write access (like git@...)
git push <remote> <branch>
To move the commits over to the origin. <branch>
is the branch you're pushing (often "master").
Solution 4 - Git
Create a git repository on the server (you can use gitolite/gitosis or just a normal user account + pubkey ssh auth), add the server to your local git repository using
git remote add name url
and use git push -u name master
(-u
marks the current branch as tracking so you can just git pull
instead git pull name master
).
On the server side (debian based system):
adduser --system --home /home/git --bash /bin/bash git
su - git
mkdir .ssh
cat yourkey.pub > .ssh/authorized_keys
Now, create a new bare repository for each local repository using
mkdir projectName
cd projectName
git init --bare
After that, the url would be git@yourserver:projectName
.
Solution 5 - Git
If you have a stand-alone local working tree repository (a folder with a ".git" folder inside) that you want to add a remote to:
-
Create a new empty repository in the remote.
-
In the local repository, set the new remote as the origin:
> cd localrepo
> git remote add origin REMOTEURL #(verify with git remote -v)
-
Push all local branches to the remote, and set each local branch to track the corresponding remote branch:
> git push --all --set-upstream origin #(verify with git branch -vv)
-
Push all local tags to the remote:
> git push --tags origin
At this point the local repository will act just like it had been cloned from the remote.
If you have a bare local repository (a folder with a name ending in .git) that you just want to copy to a remote:
-
Create a new empty repository in the remote.
-
In the local repository, push all of its branches to the remote
> cd localrepo.git
> git push --all REMOTEURL
-
Push all local tags to the remote:
> git push --tags REMOTEURL
Solution 6 - Git
If you want a normal (eg: not bare) repository, just copy it. There is nothing special that needs to be done.
If you want to use a bare repository on the server, just initialize it on the server, add it as a remote on the "local" copy, then push to it. (git push --mirror
will get everything through.)
Solution 7 - Git
I have a local repo with commit logs. I wanted to add it a a new github remote repository with all the commit logs preserved. Here is how:
-
create the remote repo on the github. And get the the repo URL from the "Clone or Download" green button, such as https://github.com/mhisoft/eVault.git
-
If the local repo was attached to an old orgin. remove it first
> git remote remove origin
-
Add the existing repository from the command line
> git remote add origin https://github.com/mhisoft/eVault.git
> git push -u origin master
Solution 8 - Git
Perhaps this is "backwards", but I've always done
git clone --bare localrepo localrepo.git
scp -r localrepo.git remoteserver:/pathTo
mv localrepo localrepo-prev
git clone remoteserver:/pathTo/localrepo
prove out the new repo is fine, with git status/log etc to make me feel better
move any files not under version control from -prev to the new localrepo
rm -rf localrepo.git localrepo-prev
Solution 9 - Git
I have tried the first two most upvoted answers. But it throws all sort of errors. Maybe because things have been updated in the 'git'. This is what I did. If you have tried any other methods, delete the '.git' folder by 'rm -rf ./.git' also If you have created a remote repo delete it to do a fresh start.
#. In your local repo initiate the git.
git init
#. Add the file (you can create a .gitignore file if you want to ignore certain file or file type or folders, this is optional).
git add .
#. make the first commit
git commit -m "First commit"
#. Now create an empty repository in bitbucket/github log in to the bitbucket/github and create a new repository with no readme or .gitignore. If there is any file tnere or commit, it will create problem. There are other procedures for that.
#. Add this repository in the BitBucket/Github as 'remote' for the local repository. Go to the root of the local repository directory.
git remote add origin Remote_URL_Of_the_Repo
#. The Remote_URL looks like https://[email protected]/UserName/RepositoryName.git
#. Push the local files to the remote repository
git push origin master
#. Once complete you will see the local files, master branch, commits in the remote.