Git 只请求一个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8421552/
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 12:26:45  来源:igfitidea点击:

Git pull request for just one file

gitgithubhomebrewpull-request

提问by mankoff

I've made multiple changes to two files in a Git repository (specifically, added two formulae to brew).

我对 Git 存储库中的两个文件进行了多次更改(具体来说,向 brew 添加了两个公式)。

I committed the changes individually:

我单独提交了更改:

git commit file1
git commit file2

I then did one push to GitHub:

然后我对 GitHub 进行了一次推送:

git push [email protected]:myname/homebrew.git

I'd now like to send two pull requests to the upstream repository, one for file1, one for file2. Is this possible?

我现在想向上游存储库发送两个拉取请求,一个用于文件 1,一个用于文件 2。这可能吗?

采纳答案by Cascabel

If you changed both files in the same commit, then no, this isn't possible. Pushes and pulls operate at a commit level; they won't split them apart.

如果您在同一次提交中更改了两个文件,那么不,这是不可能的。推和拉操作在提交级别;他们不会把他们分开。

If you haven't shared the changes yet, you could split the commit into two, making a branch for each, and then initiate pull requests for those.

如果您还没有共享更改,您可以将提交拆分为两个,为每个创建一个分支,然后为这些提交拉取请求。

This is one of those things there are many ways to do, but for example, you could do something like this:

这是有很多方法可以做的事情之一,但例如,您可以执行以下操作:

# make sure the commit in question is the most recent
# make branch to point to the previous commit, leaving the changes in your work tree
git reset HEAD^
# commit the changes to the first file
git add file1
git commit
# make a branch for the first commit
git branch first-branch HEAD^
# commit the changes to the second file
git add file2
git commit
# create and check out a branch for this commit
git checkout -b second-branch
# rebase the branch back, so that it doesn't include the first commit
git rebase --onto HEAD^^ HEAD^ second-branch

# point your master branch somewhere that makes sense - maybe before either branch
git checkout master
git reset --hard first-branch^

This would leave you with history like this:

这会给你留下这样的历史:

- x (master) - A (first-branch)
   \
    - B (second-branch)

where commit A modified file1, and commit B modified file2.

其中提交 A 修改后的文件 1,提交 B 修改后的文件 2。

Once the history looks the way you like it, you can push the two branches separately and do what you need to do with them:

一旦历史看起来像您喜欢的样子,您就可以分别推送两个分支并执行您需要对它们执行的操作:

git push origin first-branch second-branch

回答by Mat

Put each file on its own branch. You can generate a pull request for each branch, which should do what you want.

将每个文件放在自己的分支上。您可以为每个分支生成一个拉取请求,这应该可以满足您的需求。

回答by Michael Krelin - hacker

You can only fetch the whole revision (actually, the whole branch leading to the revision), but you can then access individual files from your repository:

您只能获取整个修订版(实际上是指向修订版的整个分支),但您可以从存储库中访问单个文件:

git checkout <rev> -- <file>