git 从将要提交的列表中删除一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3564598/
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 the list that will be committed
提问by Max Frai
I have a list of changed files in git repository. There is one file I don't wanna commit for the current moment. Can I do:
我在 git 存储库中有一个已更改文件的列表。目前我不想提交一个文件。我可不可以做:
git commit -a
To commit all files and then somehow remove that file from current commit? After such removing it should still be in the list of uncommited files.
提交所有文件,然后以某种方式从当前提交中删除该文件?在这样删除之后,它应该仍然在未提交的文件列表中。
回答by Cascabel
You want to do this:
你想这样做:
git add -u
git reset HEAD path/to/file
git commit
Be sure and do this from the top level of the repo; add -u
adds changes in the current directory (recursively).
确保从 repo 的顶层执行此操作;add -u
在当前目录中添加更改(递归)。
The key line tells git to reset the version of the given path in the index (the staging area for the commit) to the version from HEAD (the currently checked-out commit).
关键行告诉 git 将索引中给定路径(提交的暂存区)的版本重置为 HEAD 中的版本(当前检出的提交)。
And advance warning of a gotcha for others reading this: add -u
stages all modifications, but doesn't add untracked files. This is the same as what commit -a
does. If you want to add untracked files too, use add .
to recursively add everything.
并提前警告其他人阅读本文时遇到的问题:add -u
暂存所有修改,但不添加未跟踪的文件。这和做什么是commit -a
一样的。如果您也想添加未跟踪的文件,请使用add .
递归添加所有内容。
回答by DavidR
git rm --cached
will remove it from the commit set ("un-adding" it); that sounds like what you want.
git rm --cached
将从提交集中删除它(“取消添加”它);这听起来像你想要的。
回答by Ashish Sajwan
if you have already pushed your commit then. do
如果你已经推送了你的提交。做
git checkout origin/<remote-branch> <filename>
git commit --amend
AND If you have not pushed the changes on the server you can use
并且如果您尚未在服务器上推送更改,则可以使用
git reset --soft HEAD~1
回答by Doug
Use stash; like this:
使用藏匿处;像这样:
git add .
git reset Files/I/Want/To/Keep
git stash --keep-index
git commit -a -m "Done!"
git stash pop
If you accidentallycommit a file, and want to rewrite your git history, use:
如果您不小心提交了一个文件,并想重写您的 git 历史记录,请使用:
git reset HEAD~1 path/to/file
git commit -a -m "rollback"
git rebase -i HEAD~2
and squash to the two leading commits. You can write a helper script to do either of these if you have a known set of files you prefer not to automatically commit.
并压缩到两个主要提交。如果您有一组不希望自动提交的已知文件,您可以编写一个帮助程序脚本来执行其中任何一项。
回答by ThR37
Maybe you could also use stash to store temporaly your modifications in a patch file and then reapply it (after a checkout to come back to the old version). This could be related to this other topic : How would I extract a single file (or changes to a file) from a git stash?.
也许您还可以使用 stash 将您的修改临时存储在补丁文件中,然后重新应用它(在结帐后返回旧版本)。这可能与另一个主题有关:如何从 git stash 中提取单个文件(或对文件的更改)?.
回答by Jordan Stefanelli
Answer:
回答:
git reset HEAD path/to/file
回答by Daniel Alder
You have to reset that file to the original state and commit it again using --amend
. This is done easiest using git checkout HEAD^
.
您必须将该文件重置为原始状态并使用--amend
. 使用git checkout HEAD^
.
Prepare demo:
准备演示:
$ git init
$ date >file-a
$ date >file-b
$ git add .
$ git commit -m "Initial commit"
$ date >file-a
$ date >file-b
$ git commit -a -m "the change which should only be file-a"
State before:
之前的状态:
$ git show --stat
commit 4aa38f84e04d40a1cb40a5207ccd1a3cb3a4a317 (HEAD -> master)
Date: Wed Feb 7 17:24:45 2018 +0100
the change which should only be file-a
file-a | 2 +-
file-b | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
Here it comes: restore the previous version
来了:恢复以前的版本
$ git checkout HEAD^ file-b
commit it:
提交它:
$ git commit --amend file-b
[master 9ef8b8b] the change which should only be file-a
Date: Wed Feb 7 17:24:45 2018 +0100
1 file changed, 1 insertion(+), 1 deletion(-)
State after:
之后的状态:
$ git show --stat
commit 9ef8b8bab224c4d117f515fc9537255941b75885 (HEAD -> master)
Date: Wed Feb 7 17:24:45 2018 +0100
the change which should only be file-a
file-a | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
回答by Breedly
Most of these answers circulate around removing a file from the "staging area" pre-commit, but I often find myself looking here after I've already committed and I want to remove some sensitive information from the commit I just made.
这些答案中的大多数都围绕从“暂存区”预提交中删除文件而流传,但我经常发现自己在已经提交后查看这里,并且我想从我刚刚提交的提交中删除一些敏感信息。
An easy to remember trick for all of you git commit --amend
folks out there like me is that you can:
对于git commit --amend
像我这样的所有人来说,一个容易记住的技巧是,你可以:
- Delete the accidentally committed file.
git add .
to add the deletion to the "staging area"git commit --amend
to remove the file from the previous commit.
- 删除意外提交的文件。
git add .
将删除添加到“暂存区”git commit --amend
从上一次提交中删除文件。
You will notice in the commit message that the unwanted file is now missing. Hooray! (Commit SHA
will have changed, so be careful if you already pushed your changes to the remote.)
您会在提交消息中注意到不需要的文件现在丢失了。万岁!(提交SHA
会发生变化,所以如果您已经将更改推送到远程,请小心。)
回答by David
if you did a git add and you haven't pushed anything yet, you just have to do this to unstage it from your commit.
如果你做了一个 git add 并且你还没有推送任何东西,你只需要这样做就可以从你的提交中取消它。
git reset HEAD <file>
git reset HEAD <file>