git 只推送一个文件夹到远程仓库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5870170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:51:05  来源:igfitidea点击:

Pushing only one folder to remote repo

git

提问by Dave

I'm using Git 1.7.4.1. I want to push commits I did on one folder to my remote repo. How do I push only changes from one folder to my remote repo? All the documentation I've found only lists how to push the entire repo ...

我正在使用 Git 1.7.4.1。我想将我在一个文件夹上所做的提交推送到我的远程仓库。如何仅将更改从一个文件夹推送到我的远程存储库?我找到的所有文档只列出了如何推送整个 repo ...

davea-mbp2:systems davea$ git push origin trunk
Password: 
To http://[email protected]/systems.git
 ! [rejected]        trunk -> trunk (non-fast-forward)
error: failed to push some refs to 'http://[email protected]/systems.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

The conflicts I'm being warned about do not apply to the folder that I want to push. Any help is appreciated, - Dave

我被警告的冲突不适用于我想要推送的文件夹。任何帮助表示赞赏, - 戴夫

采纳答案by sehe

Git is not like subversion. The short answer is, you can't.

Git 不像颠覆。简短的回答是,你不能。

You should consider rebasing your change (only the one relevant) against the remote

您应该考虑将您的更改(仅相关的更改)针对远程

git rebase -i origin/trunk

should get you underway. Be sure to read the comments and instructions along the way. There is also man git-rebaseof course.

应该让你开始。请务必阅读沿途的评论和说明。man git-rebase当然也有。

In the end, when you are satisfied with the result, you can either create a new branch from there

最后,当您对结果感到满意时,您可以从那里创建一个新分支

 git checkout -b rebased HEAD

or you can opt to make it your new master. I'll leave it up to you how to manage your branches locally (because you didn't tell us about them).

或者你可以选择让它成为你的新主人。我将把如何在本地管理您的分支机构留给您(因为您没有告诉我们有关它们的信息)。

Pushing woud then be easy with

推动woud然后很容易

 git push origin trunk

回答by Adam Dymitruk

Git works by pushing entire commits. Either rebase interactively to isolate your changes to the directory you are interested in., or make a submodule for it.

Git 通过推送整个提交来工作。要么以交互方式变基以隔离对您感兴趣的目录的更改,要么为其创建子模块。

回答by Mike Mazur

git's unit of work is the commit. If you want to push changes to one directory but not others, those changes need to be separate commits.

git 的工作单元是提交。如果要将更改推送到一个目录而不是其他目录,则这些更改需要单独提交。

If you haven't pushed these changes elsewhere yet, you can try an interactive rebase to make your commits look the way you need. See the git-rebase man pageand this chapter in the Git community bookfor more details.

如果您还没有将这些更改推送到其他地方,您可以尝试交互式 rebase,使您的提交看起来符合您的需要。有关更多详细信息,请参阅Git 社区手册中git-rebase 手册页本章

回答by Mark Longair

The error that you report is not reporting conflicts - you only get conflicts locally when merging (e.g. as part of a git pull). That error is just refusing to update the remote branch with your commit, since your commit doesn't include the history of the branch you're trying to push to. Typically that means that someone else has pushed some divergent development to that branch and you need to pull or rebase - there's more on that here: How git works when two peers push changes to same remote simultaneously

您报告的错误不是报告冲突 - 合并时您只会在本地遇到冲突(例如作为 a 的一部分git pull)。该错误只是拒绝使用您的提交更新远程分支,因为您的提交不包括您尝试推送到的分支的历史记录。通常,这意味着其他人已将一些不同的开发推送到该分支,您需要拉取或重新设置基础 - 这里还有更多内容:当两个对等方同时将更改推送到同一个远程时,git 是如何工作的

However, to answer your question directly, commits always represent the complete state of the tree, so you just need to create a commit which only has the changes in the subdirectory of interest. Just change into that subdirectory and use git addto stage the files that you've changed in there. Then, when you commit, make sure that you don'tuse the -aparameter, otherwise all the changes in your repository will be introduced into that commit.

但是,要直接回答您的问题,提交始终代表树的完整状态,因此您只需要创建一个仅在感兴趣的子目录中进行更改的提交即可。只需更改到该子目录并用于git add暂存您在其中更改的文件。然后,在提交时,请确保使用该-a参数,否则存储库中的所有更改都将引入该提交中。