git 如何将文件添加到git中的最后一次提交?

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

How to add a file to the last commit in git?

git

提问by Searene

Sometimes after I did a commit, I found out that I left out a file which should also be included in the commit but actually not. What I often did was commit again.

有时在我提交之后,我发现我遗漏了一个文件,该文件也应该包含在提交中,但实际上没有。我经常做的是再次提交。

git add the_left_out_file
git commit "include the file which should be added in the last commit"

I think it might not be a good idea to do so, what I want to do is just include the file without adding a commit. Something like this,

我认为这样做可能不是一个好主意,我想要做的只是包含文件而不添加提交。像这样的东西,

git add the_left_out_file
git add_staged_files_to_previous_commit

Is it possible?

是否可以?

回答by Konrad 'Zegis'

Yes, there's a command git commit --amendwhich is used to "fix" last commit.

是的,有一个命令git commit --amend用于“修复”上次提交。

In your case it would be called as:

在您的情况下,它将被称为:

git add the_left_out_file
git commit --amend --no-edit

The --no-edit flag allow to make amendment to commit without changing commit message.

--no-edit 标志允许在不更改提交消息的情况下修改提交。

EDIT: WarningYou should never amend public commits, that you already pushed to public repository, because what amend does is actually removing from history last commit and creating new commit with combined changes from that commit and new added when amending.

编辑:警告您永远不应该修改已经推送到公共存储库的公共提交,因为修改实际上是从历史记录中删除上次提交并创建新提交,并结合该提交的更改和修改时添加的新内容。

回答by Muhammed Imran Hussain

If you didn't push the update in remote then the simple solution is remove last local commit using following command: git reset HEAD^. Then add all files and commit again.

如果您没有在推更新远程那么简单的解决方法是删除最后一个本地提交使用下面的命令:git reset HEAD^。然后添加所有文件并再次提交。