本文介绍了如何将文件添加到最后一次提交git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"

我认为这可能不是一个好主意,我想要的do只是在不添加提交的情况下包含文件。就像这样,

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?

推荐答案

git commit --amend 它用于修复上次提交。

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

在你的情况下,它会被调用as:

In your case it would be called as:

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

--no-edit标志允许修改提交而不改变提交信息。

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

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

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.

这篇关于如何将文件添加到最后一次提交git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:16