从 Git 拉取请求中删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38743912/
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
Remove a file from a Git Pull Request
提问by hard_working_ant
I have added a local changes to an existing file into my PR by mistake. I want to revert only this file without deleting this file locally. I just want that it doesn't show up in the Pull Request. I don't want to create new branch as many people have commented on other files in this PR.
我错误地将现有文件的本地更改添加到我的 PR 中。我只想恢复这个文件而不在本地删除这个文件。我只是希望它不会出现在拉取请求中。我不想创建新分支,因为很多人在这个 PR 中评论了其他文件。
采纳答案by Tunbola Ogunwande
git checkout -- thefilename
git checkout -- 文件名
This will discard previous changes to the file and revert it to the last committed change.
这将放弃对文件的先前更改并将其恢复为上次提交的更改。
You can learn more here Git-Basics-Undoing-Things
你可以在这里了解更多Git-Basics-Undoing-Things
Read the section Unmodifying a Modified File.
阅读取消修改已修改文件部分。
回答by Eugene Yarmash
You could 'unstage' a particular file with git reset
and then amend the original commit:
您可以使用“取消暂存”特定文件,git reset
然后修改原始提交:
git reset HEAD^ /path/to/file
git commit --amend --no-edit
git push -f
This will not affect the version of the file in your working copy.
这不会影响工作副本中文件的版本。
Alternatively, if you want to remove multiple files, you can reset the branch's head to the previous commit and create a new one:
或者,如果你想删除多个文件,你可以将分支的 head 重置为之前的提交并创建一个新的:
git reset HEAD^ # move the tip of the branch to the previous commit
git commit -C ORIG_HEAD file1 file2 [...] # list the required files
git push -f
回答by Jared Wilber
If you updated a file that already existed and want to remove it from the PR:
如果您更新了一个已经存在的文件并希望将其从 PR 中删除:
Assume you're working on a branch off of staging
, and you PR'd a file named validate_model.py
.
假设你工作在一个分支关闭的staging
,而你PR'd命名文件validate_model.py
。
To remove this file from the PR andrevert its changes, simply do:
要从 PR 中删除此文件并恢复其更改,只需执行以下操作:
git checkout staging -- validate_model.py
git checkout staging -- validate_model.py
回答by AnoE
I assume what you mean is that you committed a change to some file X
and other files Y
, Z
and so on. Now you want to undo onlythe change on X
. You don't want to show it in the push(I assume you mean push instead of pull), so you cannot simply edit X
back to how it was before (maybe you accidently committed a password or other secret in your source file or whatever...).
我假设您的意思是您对某些文件X
和其他文件进行了更改Y
,Z
依此类推。现在要撤消仅在变化X
。您不想在推送中显示它(我假设您的意思是推送而不是拉取),因此您不能简单地编辑X
回之前的状态(也许您不小心在源文件中输入了密码或其他机密或其他内容。 ...)。
If there is one single commit which introduced your change in X
, then it will be easiest if you use git rebase
to "skip" that commit. Let's say previouscommit
is the commit just before your change, and brokencommit
is the commit you want to remove, then:
如果只有一个提交在 中引入了您的更改X
,那么如果您习惯于git rebase
“跳过”该提交,那将是最简单的。假设previouscommit
是您更改之前的提交,并且brokencommit
是您要删除的提交,那么:
git rebase --onto previouscommit brokencommit yourbranch
If, on the other hand, the changes on X
are mixed across many commits and/or combined with changes on other files, then (where previouscommit
is the last "good" commit):
另一方面,如果更改在X
许多提交中混合和/或与其他文件的更改相结合,那么(previouscommit
最后一个“好的”提交在哪里):
git checkourbranch
git -i rebase previouscommit
In the interactive rebase, chose the edit
action for every line where X
was changed, and edit it out manually. c/f git help rebase
for more details, though it should be pretty obvious what to do.
在交互式 rebase 中,为更改的edit
每一行选择操作X
,然后手动编辑。c/fgit help rebase
了解更多详细信息,尽管该做什么应该很明显。
回答by Mr. Blond
There are probably many ways how to remove/undo file changes in your PR.
I didn't find exact steps how to do this, so here it is:
可能有很多方法可以删除/撤消 PR 中的文件更改。
我没有找到如何做到这一点的确切步骤,所以这里是:
Scenario:
设想:
Lets suppose that the file that you want to remove from PR is: C:\GitRepos\MyProject\SomeFile.cs
假设您要从 PR 中删除的文件是: C:\GitRepos\MyProject\SomeFile.cs
This file is either modified or contains no changes (is the same as in master) but still is shown in your PR.
此文件被修改或不包含任何更改(与 master 中的相同)但仍显示在您的 PR 中。
Solution:
解决方案:
1)Make sure that you have checked out the feature branch from which the PR has been created.
1)确保您已检出创建 PR 的功能分支。
2)Search the file commit history:
git log "C:\GitRepos\MyProject\SomeFile.cs"
2)搜索文件提交历史:
git log "C:\GitRepos\MyProject\SomeFile.cs"
You will see multiple records in format:
您将看到多条格式的记录:
commit <commit id>
Author: <author name>
Date: <Date of the commit>
<commit message>
3)Find the commit before you made any changes to the SomeFile.cs
. Copy the commit id
.
3)在对SomeFile.cs
. 复制commit id
.
4)Check out that commit using previously copied commit id
:
4)使用先前复制的检查提交commit id
:
git checkout <commit id> "C:\GitRepos\MyProject\SomeFile.cs"
git checkout <commit id> "C:\GitRepos\MyProject\SomeFile.cs"
5)If you now check the git status
then you will see that the SomeFile.cs
is now under pending changes.
5)如果您现在检查 ,git status
那么您将看到SomeFile.cs
现在处于挂起的更改中。
6)The last thing that you need to do is to commit these changes by simply using:
6)您需要做的最后一件事是通过简单地使用以下命令来提交这些更改:
git commit -m "Remove the SomeFile.cs from PR"
git commit -m "Remove the SomeFile.cs from PR"
And push your changes. :)
并推动您的更改。:)
回答by Vishwa G
To remove the file from pull request which was added newfollow below steps from your branch,
要从新添加的拉取请求中删除文件,请按照您的分支中的以下步骤操作,
git reset HEAD^ path of the file
git reset HEAD^ 文件路径
then do amend commit, then do force push.
然后做修改提交,然后做强制推送。
To remove the file from pull request which was existing(just modified in your latest PR) follow as below,
要从现有的拉取请求中删除文件(刚刚在您最新的 PR 中修改),请按照以下步骤操作,
Open the file in any text editor (like Notepad or npp). Replace (cut and paste) the contents of the file with unmodified version (likely copy from the source latest updated or simply not having your changes). Then stage and amend commit the file. Then do a force push.
在任何文本编辑器(如记事本或 npp)中打开文件。用未修改的版本替换(剪切和粘贴)文件的内容(可能从最新更新的源复制或根本没有您的更改)。然后暂存并修改提交文件。然后用力推。
回答by SauerTrout
You will need to know the hash value from the most recent commit effecting the file you want to remove from your Pull Request, hereafter , and the path to the file to which you want to undo all changes. Then, execute these commands from your working directory:
您将需要知道影响要从拉取请求中删除的文件的最近提交的哈希值,此后,以及要撤消所有更改的文件的路径。然后,从您的工作目录执行这些命令:
1) git checkout <my_feature_branch>
2) git checkout <latest_commit_hash> -- <Path_to_file_you_want_to_remove>
Example:
例子:
git checkout experimentBranch
git checkout xxxx123 -- MyProject/Resources/Constants.java
With these changes, the file in question will revert to how it was at the start of your branch, i.e., before the first commit on your feature branch.
通过这些更改,有问题的文件将恢复到它在您的分支开始时的状态,即,在您的功能分支上的第一次提交之前。
回答by 1antares1
In my particular case, they just didn't want to be reflected as: File changes(regardless of the commits to be added).
在我的特定情况下,他们只是不想被反映为:文件更改(无论要添加的提交如何)。
I just deleted the files and performed Commit/Push.
我刚刚删除了文件并执行了提交/推送。
When updating the Pull Request, they are no longer reflected (as if they were never added).
更新 Pull Request 时,它们不再被反映(就像它们从未添加过一样)。
I hope this can be useful.
我希望这会很有用。
回答by Maddy
You can also use Source tree app and reverse the hunk. If no file change was there then it would be removed from the PR.
您还可以使用源树应用程序并反转大块。如果没有文件更改,那么它将从 PR 中删除。