是否可以在 git 中重新定位特定文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25881306/
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
Is it possible to rebase specific files in git?
提问by merlin2011
This is similar to this question on merging, except that I am attempting to rebase branch A onto branch B, instead of merging branch B into branch A.
这类似于关于 merging 的这个问题,除了我试图将分支 A 重新绑定到分支 B 上,而不是将分支 B 合并到分支 A 中。
It is possible to achieve a similar effect in branch A using a combination of git checkout
and git commit
on individual files, but that does not have the same effect on the history as a rebase.
使用git checkout
和git commit
对单个文件的组合可以在分支 A 中实现类似的效果,但这对历史记录的影响与 rebase 不同。
Is it possible to rebase only specific files so that all future rebases will not need to touch these files, without rebasing the history of all files?
是否可以只对特定文件进行变基,以便所有未来的变基都不需要触及这些文件,而无需对所有文件的历史进行变基?
回答by kostix
Sort of. You might do interactive rebase, and when presented with the rebase script in your text editor, change all the actions from "pick" to "edit" then save and quit. Rebasing will now begin and it will stop after each commit applied making it possible for you to:
有点。您可以进行交互式变基,当在文本编辑器中显示变基脚本时,将所有操作从“选择”更改为“编辑”,然后保存并退出。重新定位现在将开始,并在每次提交后停止,使您可以:
Delete the changes in the files you're not interested in.
This is doable by something like
git reset <reference_commit> -- pathname
where the
<reference_commit>
is the name of a commit which contains the files you don't want the rebasing to modify.Apply these changes by running
git commit --amend -C HEAD
.- Run
git rebase --continue
. - Rinse, repeat.
删除您不感兴趣的文件中的更改。
这是可行的
git reset <reference_commit> -- pathname
其中
<reference_commit>
是包含您不希望变基修改的文件的提交的名称。通过运行应用这些更改
git commit --amend -C HEAD
。- 运行
git rebase --continue
。 - 冲洗,重复。
Any time git rebase
applies the next commit being rebased you might be presented with a conflict instead of just a chance to edit the already recorded commit. This does not differ much from the normal editing — you just need to pay more attention to the current status of the index.
任何时候git rebase
应用下一次重新定位的提交,您都可能会遇到冲突,而不仅仅是有机会编辑已记录的提交。这与正常编辑没有太大区别——您只需要多注意索引的当前状态即可。
In any case, what you want to do, smells pretty strange to me: rebasing, normally, is "forward-porting your local changes on top of the updatedbase (upstream code)" which means if your changes can't work with updated upstream (and why else you'd want to keep some files from updating?) there's no sense in rebasing—simply because you'll not be rebasing, you'll be doing something else, and this might bite you later.
在任何情况下,您想要做的事情对我来说都很奇怪:重新定位通常是“在更新的基础(上游代码)之上向前移植您的本地更改”,这意味着如果您的更改无法使用更新上游(以及您为什么要阻止某些文件更新?)重新定位没有任何意义 - 仅仅是因为您不会重新定位,您将做其他事情,这可能会在以后咬你。
回答by user128364
Git really works on a whole-repository basis, not a per-file basis, so you will find no easy way in git to do this
Git 确实在整个存储库的基础上工作,而不是在每个文件的基础上工作,因此您在 git 中找不到简单的方法来做到这一点
回答by Ib Aprekuma
You can revert a file to what is last committed with "git checkout [file path]". For example
您可以使用“git checkout [文件路径]”将文件恢复为上次提交的文件。例如
git checkout /views/index.html
回答by jhill515
Try git revert
. Search for the specific commit hash that introduced the unwanted change and use that as the argument. git revert
then will generate a patch that undoes those exact changes.
试试git revert
。搜索引入不需要的更改的特定提交哈希并将其用作参数。git revert
然后将生成一个补丁来撤消这些确切的更改。
Note, this means that your commits are small in volume (not modifying lots of files), as they should be. As such, the updates in the repository to other files will remain unaffected and only those dealing with that specific commit will be changed.
请注意,这意味着您的提交量很小(不修改大量文件),因为它们应该是。因此,存储库中对其他文件的更新将不受影响,只有那些处理该特定提交的文件才会更改。