繁体   English   中英

从本地存储库直接推送到新计算机

[英]Push from a local repo directly to a new machine

我的机器上有一个git项目,我试图将其设置为直接(不涉及github,bitbucket等)推送到另一台机器上的仓库中。 我无法撤消该过程,只能从远程运行git clone ,因为我自己的计算机上没有sshd。

我已经将新遥控器告诉了我的本地仓库:

git remote add deploy ssh://different.machine//path/to/repo/.git

但是, 如何设置新的遥控器以接收更改?

  • 如果我使用git init --bare创建一个空的远程服务器,则可以推送所有更改,但git checkout master在远程计算机上失败,并显示消息“致命:此操作必须在工作树中运行”。

  • 如果我使用git init创建一个空遥控器,则命令git push --all deploy失败:

     Enumerating objects: 51, done. Counting objects: 100% (51/51), done. Delta compression using up to 4 threads Compressing objects: 100% (29/29), done. Writing objects: 100% (51/51), 73.03 KiB | 73.03 MiB/s, done. Total 51 (delta 21), reused 51 (delta 21) remote: error: refusing to update checked out branch: refs/heads/master remote: error: By default, updating the current branch in a non-bare repository remote: error: is denied, because it will make the index and work tree inconsistent remote: error: with what you pushed, and will require 'git reset --hard' to match remote: error: the work tree to HEAD. remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable t remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int remote: error: its current branch; however, this is not recommended unless ETC... 

那么, 推荐的方法是什么? 我不想调整我不了解的“不推荐”设置,这将给我带来更多麻烦。

如果我使用git init --bare创建一个空的远程服务器,则可以推送所有更改,但是git checkout master在远程计算机上失败,并显示fatal: This operation must be run in a work tree消息fatal: This operation must be run in a work tree

让我们将其分解成小块,并解释一件事正在进行中。

git init --bare

当您运行git init <optional folder name> git会创建一个新文件夹,并使用.git文件夹初始化该文件夹,而无需太多细节,该文件夹包含git可以操作的“元数据” ,并且文件夹本身是你是“工作树”。

在此处输入图片说明

当您运行git init --bare git不会创建.git文件夹,而不是将内容包含在.git文件夹中,而是该文件夹的内容位于您的当前文件夹中。 这就是bare的含义,您有一个没有工作树的裸存储库。


我可以全力以赴

裸仓库包含您所有的分支,标签,注释等。“数据”与数据包的文件一起存储在“对象”文件夹中。


git checkout master在远程计算机上失败,并显示fatal: This operation must be run in a work tree消息fatal: This operation must be run in a work tree

如上所述,在布雷亚仓库中,您没有工作树,因此无法签出内容。


你能做什么?

Git支持几种在存储库之间进行连接的传输协议。 因此,您可以使用file协议在远程计算机上创建一个有效的存储库。

# Clone the bare repo to a new folder
git clone /usr/folder/.../.git

Worktree

另一个解决方案是使用git worktree
git worktrtee将从裸仓库中创建一个工作树,这样您就可以工作了

###
### Creating new worktree
###

### create new branch inside the worktree folder 
git worktree -b <branch name> <path>

# Or (branch name can be exiting branch or new one)
git worktree add <path>/<branch name> 


您忘记了解释如何在其他计算机上使用存储库。

在没有更好地了解您的需求的情况下,通常建议的方法是使用中间的裸存储库和最终的裸存储库。 中间的裸仓库在哪里都无所谓-git托管还是另一台机器上的裸仓库。

您将推送到中间的裸存储库,然后从该存储库克隆/拉取到非裸存储库。 您可以通过向裸仓库添加post-receive / post-update挂钩来自动化该过程。

让我们在单个远程主机上创建两个存储库:

ssh user@remote-host "
cd /some/path/ &&
git init --bare myrepo.git &&
git clone myrepo.git myrepo &&
echo '#!/bin/sh

for ref in "$@"; do
   if [ "$ref" = refs/heads/master ]; then
      unset GIT_DIR
      cd ../myrepo &&
      exec git pull ../myrepo.git master
   fi
done' > myrepo.git/hooks/post-update &&
chmod a+x myrepo.git/hooks/post-update
"

cd /path/to/local-repo &&
git remote add remote-repo user@remote-repo:/some/path/myrepo.git
git push remote-repo master

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM