简体   繁体   English

如何将现有目录导入孤分支

[英]How to import an existing directory to an orphan branch

tl;dr tl; dr

What I want is a tidy way to push code from my web servers to orphan branches in a remote repository without leaving any Git repo's on the web server afterwards and without having to clone the entire remote repository onto the Web server first. 我想要的是一种整齐的方法,可以将代码从Web服务器推送到远程存储库中的孤立分支,而之后无需在Web服务器上保留任何Git存储库,而不必先将整个远程存储库克隆到Web服务器上。 I also need to leave the current, unversioned, files untouched on the web server. 我还需要在Web服务器上保留当前未版本控制的文件。

EDIT: To clarify, I am not asking for a way to setup deployment or anything of the sort, I already have that working. 编辑:为澄清起见,我不要求设置部署或任何种类的方法,我已经可以使用了。 I simply need a way to push the existing code from a non-versioned directory to a new orphan branch in a remote repository tidily without messing up and files on the webserver and without leaving any .git files behind. 我只需要一种方法就可以整齐地将现有代码从非版本目录推送到远程存储库中的新孤立分支,而不会弄乱Web服务器上的文件和文件,也不会留下任何.git文件。

Background Information 背景资料

I've recently gotten into the trend of using Git for versioning all my websites. 最近,我开始使用Git对所有网站进行版本控制的趋势。 It seems so much easier than FTPing things, and allows working in teams all with full history and revisions. 似乎比FTP事情要容易得多,并且允许具有完整历史记录和修订版本的团队一起工作。 I am going through the process of importing everything into Git repos, but I've hit a bit of an issue. 我正在经历将所有内容导入Git仓库的过程,但是我遇到了一个问题。

I have a project that will have multiple (orphan) branches that represent different deployment targets for code. 我有一个项目,该项目将具有代表不同代码部署目标的多个(孤立)分支。 For the purpose of this question this is what my setup looks like: 出于这个问题的目的,这就是我的设置:

I have 3 servers. 我有3台服务器。 Two are Web Servers and 1 is a hybrid Web-Git Server. 两个是Web服务器,一个是混合Web-Git服务器。

One of the Web Servers is a small VM that operates as a CDN backend for Cloudflare. Web服务器之一是一个小型VM,可以用作Cloudflare的CDN后端。 It runs an nginx server and simply serves and changed or new files once or twice to Cloudflare for caching (its small because Cloudflare only requests the file once, meaning it doesn't handle large amounts of requests). 它运行一台nginx服务器,只需向Cloudflare缓存一次或两次或更改或更改新文件即可(这很小,因为Cloudflare仅请求一次文件,这意味着它不处理大量请求)。

My second Web Server hosts the actual Website and contains the entire server backend for the app. 我的第二台Web服务器托管实际的网站,并包含应用程序的整个服务器后端。

Both of the Web Servers are, for illustrational purposes, separate machines. 出于说明目的,两个Web服务器都是单独的计算机。 The third machine is simply the Git server and hosts a bunch of Atlassian apps. 第三台机器只是Git服务器,并托管了许多Atlassian应用程序。

I also had a previous SVN repository that contained C# Source code for a server related to the project. 我以前也有一个SVN存储库,其中包含与该项目相关的服务器的C#源代码。

The Problem 问题

I have imported the existing SVN repository into the "master" of the Git repository. 我已经将现有的SVN存储库导入到Git存储库的“主服务器”中。 I now want to create 2 orphan branches, one for the CDN files and one for the website files. 我现在想创建2个孤立分支,一个用于CDN文件,一个用于网站文件。

After some research I have come across something like this: git checkout --orphan <branchname> . 经过一番研究,我发现了这样的事情: git checkout --orphan <branchname> This is where my problem lies. 这就是我的问题所在。 I would like to create a branch to push CDN etc. code to and then push the code from the current web servers without having to clone the entire repository. 我想创建一个分支来将CDN等代码推送到,然后从当前的Web服务器推送代码,而不必克隆整个存储库。 Because git checkout requires an existing git repo to be in the working directory, I didn't know how to do this. 因为git checkout要求将现有的git repo放在工作目录中,所以我不知道该怎么做。 I tried git init -ing the directory where my files are, creating a new branch locally, comitting them and then pushing them to a remote branch with git push git@mydomain.com:me/the_repo +local_branch:cdn_branch but this didn't work and left a Git repository in my web servers WWW folder. 我尝试了git init init-将文件放置到目录中,在本地创建一个新分支,将其提交,然后使用git push git@mydomain.com:me/the_repo +local_branch:cdn_branch将其推到远程分支,但这没有工作并在我的Web服务器的WWW文件夹中保留了一个Git存储库。

You can skip a lot of that: 您可以跳过很多:

cd /var/www
git init
git add .
git commit -m"Importing existing code to branch blah"
git push u://r/l master:blah
rm -rf .git

After some experimentation I came to the following solution: 经过一些试验,我得出以下解决方案:

  1. First goto the directory you wish to push to the orphan branch 首先转到您要推送到孤儿分支的目录

    EG: cd /var/www EG: cd /var/www

  2. Initialize an empty Git Repository 初始化一个空的Git仓库

    EG: git init . EG: git init .

  3. Add all the files from the current directory 添加当前目录中的所有文件

    EG: git add . EG: git add .

  4. Commit them 提交他们

    EG: git commit -m "Importing existing code to branch blah" EG: git commit -m "Importing existing code to branch blah"

  5. Push the branch to the remote repo 将分支推送到远程仓库

    EG: git push <URL> master:<BRANCH NAME> EG: git push <URL> master:<BRANCH NAME>

  6. Clean up time 清理时间

    EG: rm -fr .git EG: rm -fr .git

That did it for me without having to clone the entire Git Repository down onto my webserver! 这样做对我来说无需将整个Git存储库克隆到我的Web服务器上! What got me for some time was the fact that the orphaned branch will only be created once you commit files to it. 使我困扰了一段时间的事实是,仅当您将文件提交后,孤立分支才会创建。 Multiple times I tried git checkout --orphan BRANCH and confused me because the branch didn't show when I typed in git branch . 我多次尝试git checkout --orphan BRANCH并迷惑了我,因为当我输入git branch时分支没有显示。

A way to merge two repositories is to simply copy the objects from one .git/objects directory to the other. 合并两个存储库的一种方法是简单地将对象从一个.git/objects目录复制到另一个.git/objects

After copying repoA/.git/objects/* to repoB/.git/objects/ , objects (commits and revisions of files, not branches and simple tags) of repoA should appear in repoB as-is. repoA/.git/objects/*复制到repoB/.git/objects/repoB/.git/objects/对象(文件的提交和修订,不是分支和简单标签)应原样出现在repoB中。

Commits imported from repoA are not pointed by any branch of repoB, so they will be dangling commits for a moment. 从repoA导入的提交未由repoB的任何分支指向,因此它们将暂时悬空提交。 You can create a branch pointing at those imported commits in repoB afterwards. 之后,您可以创建一个分支,指向在repoB中导入的那些提交。

A possible risk is that conflicting SHA1 hash may exist (just mentioning the possibility, this shouldn't occur often). 可能的风险是可能存在冲突的SHA1哈希(仅提及可能性,这种情况不应该经常发生)。

====== ======

Not pretty sure if I understood what you want to do. 不太确定我是否了解您想要做什么。 Maybe you want to manage portion of contents with git submodule? 也许您想使用git子模块管理部分内容?

听起来您想提交到一台服务器,然后释放到另一台服务器,而没有发布服务器上的所有文件,历史记录等-我建议为复制发布文件的标签设置提交后过滤器到其他两个服务器。

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

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