在git仓库的子文件夹上推送到远程源? [英] Push to a remote origin on a subfolder of git repository?

查看:172
本文介绍了在git仓库的子文件夹上推送到远程源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个git存储库,该存储库使用Vagrant为WordPress项目(仅本地开发)构建虚拟服务器.文件夹结构大致如下(出于问题的目的).

I have a git repository which uses Vagrant to build a virtual server for a WordPress project (local development only). The folder structure is roughly as follows (for the purposes of the question).

- Vagrantfile
- puppet/
- wordpress/
    - {www public folder / root Wordpress files}
- files/

现在所有这些文件都位于我的存储库的根目录中,我希望它们保留在那里,因此,如果我需要向Vagrant添加任何更新,它们将被添加到存储库中.现在,主机可以通过直接使用git推送更新,使我能够部署到我的登台服务器和生产服务器.但是这些文件存储在/wordpress/中,并且它们服务器上的原始文件是根目录.理想情况下,我只想将/wordpress/文件夹中的内容推送到远程源的根目录.

Now all these files are in the root of my repository and I would like them to stay there so if I need to add any updates to Vagrant they will be added to the repo. Now my host has the ability to allow me to deploy to my staging and production servers by pushing updates directly using git. But these files are stored in /wordpress/ and the origin on their server is the root. Ideally I would like to push only from the /wordpress/ folder to the root of the remote origin.

我曾考虑将/wordpress/文件夹移动到项目的根目录,但是随后所有Vagrant文​​件将被推送到生产服务器(除非有一种方法可以添加特定于远程源的.gitignore).

I thought of moving the /wordpress/ folder to the root of the project but then all the Vagrant files will be pushed to the production server (unless there is a way to add a .gitignore specific to remote origins).

我敢肯定有几种方法可以做到这一点,我也查看了git子树,但是对于这个特定问题来说似乎有些过头了.如果有人可以建议最好的解决方案,将不胜感激.

I am sure there are a few ways to accomplish this, I also looked at git subtrees but it looked like a bit of overkill for this specific issue. If anyone could suggest the best solution it would be appreciated.

推荐答案

间接的,卑鄙的回答是git不是部署工具.因此,您应该使用phing,capistrano,ant,puppet,chef,ansible或grunt,或者只写一些bash脚本等.

The indirect, and snotty answer, is that git isn't a deployment tool. So you should use phing, or capistrano, or ant, or puppet, or chef, or ansible, or grunt, or just write some bash script, etc.

简短的答案是git-subtree split.我会去的.

The short answer is git-subtree split. I'll go with this.

说明

子树,甚至更多的子模块,如果您真的不知道发生了什么,可能会造成混乱.通常,它们可作为子组件的替代品用于模块化开发.我不打算讨论这个问题,因为那不是您在这种情况下的用法.

Subtree, even more so that submodules, can be confusing if you don't really know what's going on. Normally they are used for modular development as an alternative to submdodules. I won't go into that because thats not how you would use it in this case.

出于您的目的,您将执行称为subtree split的操作来创建一个分支,该分支包含仅 所需的目录. subtree split命令可查看存储库的所有历史记录,以查找影响给定目录中的文件的提交,并将它们与不会引起更改的内容分开.然后,您可以将这些提交(或它们的压缩版本)放入其自己的分支中.结果,当您签出该分支时,给定目录将位于根目录,而没有其他内容.

For your purposes, you would perform whats called a subtree split to create a branch that contains only the directory you want. The subtree split command looks through all your repository's history for commits that effect files in a given directory and separates them from changes that don't. You're then able to put those commits (or a squashed version of them) to their own branch. As a consequence, when you checkout that branch, the given directory will be at the root, and nothing else.

螺母和螺栓

我将在解释中为您做出一些选择,但您可以自由更改.我正在指定deploy分支,并且我决定将您的子树历史记录压缩为单个提交,因为您永远不要使用该分支来进行任何开发.我还假设您的存储库名称为原产地.

I'm going to make some choices for you in my explanation, but you're free to change those. I'm designating the deploy branch, and I'm deciding to squash your subtree history into a single commit since you should never use that branch to do any development. I'm also assuming your repository name is origin.

git subtree split --prefix=wordpress --squash --branch deploy
git push -f origin deploy:deploy

通过使用deploy:deploy表示法进行推送,您实际上不必为了进行推送而必须签出deploy分支(这可能需要花费一些时间).实际上,您甚至可以直接将其推入远程分支,从而避免甚至完全创建分支.

By pushing with the deploy:deploy notation, you dont actually have to checkout the deploy branch (which might take time to do) in order to push it. You could actually avoid even creating the branch all together by just pushing to the remote branch directly.

git subtree push --prefix=wordpress --squash deploy master

我建议使用最后一种方法,因为这样可以避免意外地使用压缩的子树分支.

I recommend using this last method, since it avoids accidentally doing anything with the squashed subtree branch.

但是,...子树对我不起作用,因为XYZ

是的,子树拆分有时仍然参差不齐.如果无论出于何种原因,subtree命令都不适合您,那就可以了.您可以使用更旧,更混乱的git-filter-branch命令.有关该命令的完整说明,请参见此处.

Yeah, subtree splitting is still spotty sometimes. If for whatever reason the subtree command isn't an option for you, thats ok. You can use the older, even more confusing git-filter-branch command. See here for a full explanation on that command.

如果您坚持使用此选择,那么..这太臭了.您可能最好只使用子模块(这时可能是这样),或者编写一些bash脚本将文件复制到本地的其他仓库中并推送...或其他可怕的事情.

If your stuck with this choice, then.. that stinks. You're probably better off just using submodules (probably the way to go if your at this point), or writing some bash script to copy files to a different repo on you local and pushing... or something else terrible.

或者您可以只使用一个真正的部署工具.

Or you could just use a real deployment tool.

这篇关于在git仓库的子文件夹上推送到远程源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆