git 如何从我的提交中删除一些文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14386744/
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
How to delete some files from my commit
提问by user1934146
I have uploaded a gerrit and now I want to delete some files from my commit, how can I do that?
我已经上传了 gerrit,现在我想从我的提交中删除一些文件,我该怎么做?
回答by Kalle Pokki
So you need to generate a new patch set that will replace the old one. Assuming you haven't commited anything else since the commit you are trying to edit, do
所以你需要生成一个新的补丁集来替换旧的补丁集。假设自您尝试编辑的提交以来您没有提交任何其他内容,请执行
git rm <files>
git commit --amend
You have installed the gerrit commit hook, haven't you? If you have, you are good to go and are ready to push. You you don't have it, you need to copy the Change-id line from the gerrit web interface to the end of your commit message, or gerrit won't be able to replace the previous patch set with the new one.
你已经安装了 gerrit 提交钩子,不是吗?如果你有,你就可以开始了,并准备好推动。您没有它,您需要将 gerrit 网络界面中的 Change-id 行复制到提交消息的末尾,否则 gerrit 将无法用新补丁集替换之前的补丁集。
When you committed the file (and you have the same Change-id line there as you had in patch set 1), push the fix to gerrit
当您提交文件时(并且您在那里拥有与补丁集 1 中相同的 Change-id 行),将修复推送到 gerrit
git push origin HEAD:refs/for/master
or whatever repository and branch you are pushing to.
或您要推送到的任何存储库和分支。
In the future, you should install the commit hook as soon as you clone the repository from gerrit. If you clone with e.g.
将来,您应该在从 gerrit 克隆存储库后立即安装提交挂钩。如果你用例如克隆
git clone ssh://firstname.lastname@gerrit/project
you can get the commit hook with
你可以得到提交钩子
cd project
scp firstname.lastname@gerrit:hooks/commit-msg .git/hooks
Substitute paths and machine names that apply to your case.
替换适用于您的情况的路径和机器名称。
回答by Sankaran Srinivasan
Say you have commited three files a.java, b.java, c.java to gerrit and you want to remove b.java from the commit. Follow the below steps.
假设您已向 gerrit 提交了三个文件 a.java、b.java、c.java,并且您想从提交中删除 b.java。请按照以下步骤操作。
- If you are in a different branch, cherry-pick the particular commit.
Use the below command to remove the file from the commit. (Do this for as many files you want to remove.)
git reset HEAD^ path/to/file/b.java
Amend the commit with the below command.
git commit --amend
- 如果您在不同的分支中,请挑选特定的提交。
使用以下命令从提交中删除文件。(对要删除的文件执行此操作。)
git reset HEAD^ path/to/file/b.java
使用以下命令修改提交。
git commit --amend
参考:https: //superuser.com/questions/229290/how-to-amend-the-last-commit-to-un-add-a-file/229296#229296?newreg=011d1b3234a8444295fbdd12b6d513b4