从 Git 提交中删除文件

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

Remove files from Git commit

gitgit-commit

提问by Lolly

I am using Git and I have committed few files using

我正在使用 Git 并且我使用了几个文件

git commit -a

Later, I found that a file had mistakenly been added to the commit.

后来我发现一个文件被错误地添加到了提交中。

How can I remove a file from the last commit?

如何从上次提交中删除文件?

回答by juzzlin

I think other answers here are wrong, because this is a question of moving the mistakenly committed files back to the staging area from the previous commit, without cancelling the changes done to them. This can be done like Paritosh Singh suggested:

我认为这里的其他答案是错误的,因为这是一个将错误提交的文件从上次提交移回暂存区的问题,而不取消对它们所做的更改。这可以像 Paritosh Singh 建议的那样完成:

git reset --soft HEAD^ 

or

或者

git reset --soft HEAD~1

Then reset the unwanted files in order to leave them out from the commit:

然后重置不需要的文件,以便将它们从提交中排除:

git reset HEAD path/to/unwanted_file

Now commit again, you can even re-use the same commit message:

现在再次提交,您甚至可以重复使用相同的提交消息:

git commit -c ORIG_HEAD  

回答by CharlesB

ATTENTION! If you only want to remove a file from your previous commit, and keep it on disk, read juzzlin's answerjust above.

注意!如果您只想从之前的提交中删除一个文件,并将其保留在磁盘上,请阅读上面juzzlin 的回答

If this is your last commit and you want to completely delete the file from your local and the remote repository, you can:

如果这是您的最后一次提交,并且您想从本地和远程存储库中完全删除该文件,您可以:

  1. remove the file git rm <file>
  2. commit with amend flag: git commit --amend
  1. 删除文件 git rm <file>
  2. 提交修改标志: git commit --amend

The amend flag tells git to commit again, but "merge" (not in the sense of merging two branches) this commit with the last commit.

修改标志告诉 git 再次提交,但是“合并”(不是合并两个分支的意义)这次提交与最后一次提交。

As stated in the comments, using git rmhere is like using the rmcommand itself!

正如评论中所述,git rm在这里使用就像使用rm命令本身一样!

回答by Brian

Existing answers are all talking about removing the unwanted files from the lastcommit.

现有答案都在谈论从上次提交中删除不需要的文件。

If you want to remove unwanted files from an oldcommit (even pushed) and don't want to create a new commit, which is unnecessary, because of the action:

如果您想从提交(甚至推送)中删除不需要的文件并且不想创建新提交,这是不必要的,因为操作:

1.

1.

Find the commit that you want the file to conform to.

找到您希望文件符合的提交。

git checkout <commit_id> <path_to_file>

you can do this multiple times if you want to remove many files.

如果要删除许多文件,可以多次执行此操作。

2.

2.

git commit -am "remove unwanted files"

3.

3.

Find the commit_id of the commit on which the files were added mistakenly, let's say "35c23c2" here

找到错误添加文件的提交的 commit_id ,我们在这里说“35c23c2”

git rebase 35c23c2~1 -i  // notice: "~1" is necessary

This command opens the editor according to your settings. The default one is vim.

此命令根据您的设置打开编辑器。默认是vim。

Move the last commit, which should be "remove unwanted files", to the next line of the incorrect commit("35c23c2" in our case), and set the command as fixup:

将最后一次提交,应该是“删除不需要的文件”,移动到不正确提交的下一行(在我们的例子中是“35c23c2”),并将命令设置为fixup

pick 35c23c2 the first commit
fixup 0d78b28 remove unwanted files

You should be good after saving the file.

保存文件后你应该会好起来的。

To finish :

完成 :

git push -f

If you unfortunately get conflicts, you have to solve them manually.

如果不幸遇到冲突,则必须手动解决它们。

回答by Patrick

As the accepted answer indicates, you can do this by resetting the entire commit. But this is a rather heavy handed approach.
A cleaner way to do this would be to keep the commit, and simply remove the changed files from it.

正如接受的答案所示,您可以通过重置整个提交来做到这一点。但这是一种相当严厉的方法。
一种更简洁的方法是保留提交,并简单地从中删除更改的文件。

git reset HEAD^ -- path/to/file
git commit --amend --no-edit

The git resetwill take the file as it was in the previous commit, and stage it in the index. The file in the working directory is untouched.
The git commitwill then commit and squash the index into the current commit.

git reset将文件,因为它是在以前的承诺,并在索引阶段,。工作目录中的文件未受影响。
然后git commit将提交并将索引压缩到当前提交中。

This essentially takes the version of the file that was in the previous commit and adds it to the current commit. This results in no net change, and so the file is effectively removed from the commit.

这实质上是获取上一次提交中的文件版本并将其添加到当前提交中。这不会导致任何净更改,因此该文件已从提交中有效删除。

回答by Paritosh Singh

If you have not pushed the changes on the server you can use

如果您尚未在服务器上推送更改,则可以使用

git reset --soft HEAD~1

It will reset all the changes and revert to one commit back

它将重置所有更改并恢复到一次提交

If you have pushed your changes then follow steps as answered by @CharlesB

如果您已推送更改,请按照@CharlesB 回答的步骤操作

回答by Bob Flannigon

Removing the file using rm will delete it!

使用 rm 删除文件将删除它!

You're always adding to a commit in git rather than removing, so in this instance return the file to the state it was in prior to the first commit (this may be a delete 'rm' action if the file is new) and then re-commit and the file will go.

您总是在 git 中添加提交而不是删除,因此在这种情况下,将文件返回到第一次提交之前的状态(如果文件是新的,这可能是删除“rm”操作)然后重新提交,文件就会消失。

To return the file to some previous state:

要将文件返回到之前的某个状态:

    git checkout <commit_id> <path_to_file>

or to return it to the state at the remote HEAD:

或将其返回到远程 HEAD 的状态:

    git checkout origin/master <path_to_file>

then amend the commit and you should find the file has disappeared from the list (and not deleted from your disk!)

然后修改提交,你应该发现文件已经从列表中消失了(并且没有从你的磁盘中删除!)

回答by Denis Shchepetov

git checkout HEAD~ path/to/file
git commit --amend

回答by ThatsAMorais

The following will unstage just the file you intended, which is what the OP asked.

以下将仅取消您想要的文件,这就是 OP 所要求的。

git reset HEAD^ /path/to/file

You'll see something like the following...

你会看到类似下面的内容...

Changes to be committed: (use "git reset HEAD ..." to unstage)

modified: /path/to/file

Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory)

modified: /path/to/file

要提交的更改:(使用“git reset HEAD ...”取消暂存)

修改:/path/to/file

未为提交暂存的更改:(使用“git add ...”更新将提交的内容)(使用“git checkout -- ...”放弃工作目录中的更改)

修改:/path/to/file

  • "Changes to be committed" is the previous version of the file before the commit. This will look like a deletion if the file never existed. If you commit this change, there will be a revision that reverts the change to the file in your branch.
  • "Changes not staged for commit" is the change you committed, and the current state of the file
  • “要提交的更改”是提交之前文件的先前版本。如果文件不存在,这看起来像是删除。如果您提交此更改,将会有一个修订版本将更改还原到您分支中的文件。
  • “Changes not staged for commit”是您提交的更改,以及文件的当前状态

At this point, you can do whatever you like to the file, such as resetting to a different version.

此时,您可以对文件执行任何您喜欢的操作,例如重置为不同的版本。

When you're ready to commit:

当您准备好提交时:

git commit --amend -a

or (if you've got some other changes going on that you don't want to commit, yet)

或者(如果您还有其他一些不想提交的更改)

git commit add /path/to/file
git commit --amend

回答by Moaz Rashad

I will explain to you with example.
Let A, B, C be 3 successive commits. Commit B contains a file that should not have been committed.

我会用例子向你解释。
设 A、B、C 为 3 次连续提交。提交 B 包含不应提交的文件。

git log  # take A commit_id
git rebase -i "A_commit_ID" # do an interactive rebase
change commit to 'e' in rebase vim # means commit will be edited
git rm unwanted_file
git rebase --continue
git push --force-with-lease <branchName>    

回答by HamzaMushtaq

You can simply try.

你可以简单地尝试。

git reset --soft HEAD~1

and create a new commit.

并创建一个新的提交。

However, there is an awesome software "gitkraken". which makes it easy to work with git.

但是,有一个很棒的软件“gitkraken”。这使得使用 git 变得容易。