当我在本地存储库中只有该项目的子目录时,是否可以启动Git? [英] Is it possible to push on Git when I only have a subdirectory of the project on my local repository?

查看:44
本文介绍了当我在本地存储库中只有该项目的子目录时,是否可以启动Git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地git存储库,该存储库配置为在克隆我喜欢保留的存储库后,通过git在gitlab上通过SSH进行远程连接(我们称之为 git@gitlab.com:myrepo/myproject.git )只有本地项目的特定文件夹,我使用以下命令进行操作:

I have a local git repository that is configured to connect remotely over SSH on Gitlab (let's call it git@gitlab.com:myrepo/myproject.git), after cloning repositories I like to keep only a specific folder of the project locally, I do it using the command:

git filter-branch --prune-empty --subdirectory-filter subdirectory HEAD~..

此命令使本地存储库跟踪远程git项目上的 subdirectory 文件夹(并忽略我项目上此文件夹之前的所有文件).只要我仅将其用于拉动就可以了...问题是如果我尝试将更改推送到该远程分支,如果尝试,则会看到以下错误:

This command makes the local repository keeps track of the subdirectory folder on the remote git project (and overlook all the files that are before this folder on my project). As long as I'm using it only for pulling it's fine... The problem is if I try to push changes to that remote branch, if I try I see the following error:

$ git push -u origin master
To git@gitlab.com:myrepo/myproject.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@gitlab.com:myrepo/myproject.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我没有在 git push --help 上找到任何与此特定情况有关的东西.当我在本地存储库中只有该项目的子文件夹时,有人知道是否可以 push 在远程文件夹上吗?

I haven't found anything to this specific case on git push --help. Does anyone know if it's possible to push on a remote folder when I only have a subfolder of the project on my local repository?

推荐答案

git push 推送完整的历史记录;您正在执行的是子树合并.Git可以做到这一点,将您的过滤分支从 HEAD 更改为 HEAD〜.,而不必重写整个历史记录,那么您可以

git push pushes complete histories; what you're doing is a subtree merge. Git can do this, change your filter-branch from HEAD to HEAD~.. to not rewrite the entire history, then you can

git checkout origin/master
git merge -s subtree master
git push origin HEAD:master

任何其他保留合并基础的过滤器分支截止都将执行此操作,这完全取决于您真正想在本地重写多少.

Any other filter-branch cutoff that preserves a merge base will do, it all depends on how much you really want rewritten locally.

如果完整原始快照的检出非常痛苦,那么即使您可以通过一些简单操作避免不必要的工作树搅动,也不想通过它们.

If checkouts of the full origin snapshot are so painful you don't want to go through them even once you can avoid the unnecessary worktree churn with some light finagling.

创建一个轻量级的克隆以进行临时工作并切换到它:

Create a vanishingly-lightweight clone for scratch work and switch to it:

git branch -tf mergeback origin/master
git clone -nsb mergeback -o local . `mktemp -d`
cd $_

请注意,以这种方式完成的整个Linux存储库的克隆在我的机器上使用了320KB(不是错字).然后,您可以进行最小结帐合并:

Note that a clone of the entire linux repo done this way uses 320KB (that's not a typo) on my box. Then, you can do a minimum-checkout merge:

git reset --quiet                    # load just the index, not the worktree
git merge -s subtree local/master    # merge my master branch into the right subtree
git push local mergeback             # push the results back to the main repo
cd -                                 # back to my repo

从那里您可以 git push origin mergeback:master 将结果推送到gitlab.

and from there you can git push origin mergeback:master to push the results up to gitlab.

除了使用filter-branch重写提交之外,还可以在新提交中提升子树: git read-tree -um @:path/to/subdir;git commit ,这样合并将不会引入现有提交的邪恶双胞胎.

p.s. rather than rewriting a commit with filter-branch, you can also hoist the subtree in a new commit: git read-tree -um @:path/to/subdir; git commit, that way the merge won't be introducing evil twins of existing commits.

这篇关于当我在本地存储库中只有该项目的子目录时,是否可以启动Git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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