简体   繁体   中英

How to import an existing directory to an orphan branch

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. I also need to leave the current, unversioned, files untouched on the web server.

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.

Background Information

I've recently gotten into the trend of using Git for versioning all my websites. It seems so much easier than FTPing things, and allows working in teams all with full history and revisions. I am going through the process of importing everything into Git repos, but I've hit a bit of an issue.

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. Two are Web Servers and 1 is a hybrid Web-Git Server.

One of the Web Servers is a small VM that operates as a CDN backend for Cloudflare. 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).

My second Web Server hosts the actual Website and contains the entire server backend for the app.

Both of the Web Servers are, for illustrational purposes, separate machines. The third machine is simply the Git server and hosts a bunch of Atlassian apps.

I also had a previous SVN repository that contained C# Source code for a server related to the project.

The Problem

I have imported the existing SVN repository into the "master" of the Git repository. I now want to create 2 orphan branches, one for the CDN files and one for the website files.

After some research I have come across something like this: 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. Because git checkout requires an existing git repo to be in the working directory, I didn't know how to do this. 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.

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

  2. Initialize an empty Git Repository

    EG: git init .

  3. Add all the files from the current directory

    EG: git add .

  4. Commit them

    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>

  6. Clean up time

    EG: rm -fr .git

That did it for me without having to clone the entire Git Repository down onto my webserver! 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 .

A way to merge two repositories is to simply copy the objects from one .git/objects directory to the other.

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.

Commits imported from repoA are not pointed by any branch of repoB, so they will be dangling commits for a moment. You can create a branch pointing at those imported commits in repoB afterwards.

A possible risk is that conflicting SHA1 hash may exist (just mentioning the possibility, this shouldn't occur often).

======

Not pretty sure if I understood what you want to do. Maybe you want to manage portion of contents with git submodule?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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