如何将本地 Git 存储库移动到远程 Git 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8978650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:49:08  来源:igfitidea点击:

How do I move my local Git repository to a remote Git repository

git

提问by Mausimo

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?

我的本地机器上有各种 Git 项目。我有一台服务器,我想将其用作我的远程 Git 存储库。如何将我的本地 Git 存储库(项目)移动到我的服务器,同时保持历史记录完整?

Thanks!

谢谢!

EDIT: Thanks for all the great answers. The response I choose makes sense to my limited GIT knowledge.

编辑:感谢所有伟大的答案。我选择的响应对我有限的 GIT 知识很有意义。

EDIT #2: I noticed my original answer selection did not copy my tags. git push --mirror <path>does copy tags.

编辑 #2:我注意到我的原始答案选择没有复制我的标签。git push --mirror <path>复制标签。

采纳答案by knittl

On your server create the git repositories as bare repository

在您的服务器上创建 git 存储库作为裸存储库

git init --bare repo.git

then, push the commits of your local repository

然后,推送本地存储库的提交

git push --mirror ssh://yourserver/~/repo.git

回答by Dor Shemer

First, create a git repo on your server

首先,在您的服务器上创建一个 git repo

git init --bare /path/to/repo

Then add the remote repo to your local one (ssh:// or https://)

然后将远程仓库添加到您的本地仓库(ssh:// 或 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

回答by iwein

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:

Ralf Wernders 博客上有一个很好的教程。假设您知道如何在服务器上创建存储库,或者已经完成了:

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

将远程添加到本地存储库。<remote>是遥控器的名称(通常是“原点”)。<url>是具有写访问权限的存储库的 URL(如 git@...)

git push <remote> <branch>

To move the commits over to the origin. <branch>is the branch you're pushing (often "master").

将提交移至原点。<branch>是您要推送的分支(通常是“master”)。

回答by Femaref

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 存储库(您可以使用 gitolite/gitosis 或只是一个普通用户帐户 + pubkey ssh auth),使用将服务器添加到您的本地 git 存储库

git remote add name url

and use git push -u name master(-umarks the current branch as tracking so you can just git pullinstead git pull name master).

并使用git push -u name master(-u将当前分支标记为跟踪,以便您可以git pull改为git pull name master)。

On the server side (debian based system):

在服务器端(基于 debian 的系统):

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.

之后,网址将是git@yourserver:projectName.

回答by balazer

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:

如果您有一个独立的本地工作树存储库(一个包含“.git”文件夹的文件夹),您想要将远程添加到:

  1. Create a new empty repository in the remote.
  2. In the local repository, set the new remote as the origin:

    cd localrepo

    git remote add origin REMOTEURL #(verify with git remote -v)

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

  4. Push all local tags to the remote:

    git push --tags origin

  1. 在远程创建一个新的空存储库。
  2. 在本地存储库中,将新的远程设置为原点:

    cd 本地仓库

    git remote add origin REMOTEURL #(用 git remote -v 验证)

  3. 将所有本地分支推送到远程,并设置每个本地分支跟踪对应的远程分支:

    git push --all --set-upstream origin #(用git branch -vv验证)

  4. 将所有本地标签推送到远程:

    git push --tags 原点

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:

如果您有一个想要复制到远程的裸本地存储库(名称以 .git 结尾的文件夹):

  1. Create a new empty repository in the remote.
  2. In the local repository, push all of its branches to the remote

    cd localrepo.git

    git push --all REMOTEURL

  3. Push all local tags to the remote:

    git push --tags REMOTEURL

  1. 在远程创建一个新的空存储库。
  2. 在本地存储库中,将其所有分支推送到远程

    cd localrepo.git

    git push --all REMOTEURL

  3. 将所有本地标签推送到远程:

    git push --tags REMOTEURL

回答by Daniel Pittman

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 --mirrorwill get everything through.)

如果您想在服务器上使用裸存储库,只需在服务器上初始化它,将其作为远程添加到“本地”副本上,然后推送到它。(git push --mirror将通过一切。)

回答by furicle

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

证明新的 repo 很好,使用 git status/log 等让我感觉更好

move any files not under version control from -prev to the new localrepo

将任何不受版本控制的文件从 -prev 移动到新的 localrepo

rm -rf localrepo.git localrepo-prev

回答by Tony

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:

我有一个带有提交日志的本地存储库。我想将它添加到一个新的 github 远程存储库,并保留所有提交日志。方法如下:

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

  2. If the local repo was attached to an old orgin. remove it first

    git remote remove origin

  3. Add the existing repository from the command line

    git remote add origin https://github.com/mhisoft/eVault.git

    git push -u origin master

  1. 在 github 上创建远程仓库。并从“Clone or Download”绿色按钮获取repo URL,例如 https://github.com/mhisoft/eVault.git

  2. 如果本地存储库附加到旧源。首先删除它

    git远程删除原点

  3. 从命令行添加现有存储库

    git 远程添加源https://github.com/mhisoft/eVault.git

    git push -u origin master