Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git忘记将文件更改添加到尚未推送的提交(不是最后一个)_Git - Fatal编程技术网

Git忘记将文件更改添加到尚未推送的提交(不是最后一个)

Git忘记将文件更改添加到尚未推送的提交(不是最后一个),git,Git,我想将文件更改添加到尚未推式提交(不是最后一个)中 假设我有以下场景: git init touch test.txt // do some changes to the text file git commit -a -m "First change" # if I now change something I can add it to the previous commit with: git add test.txt git commit --amend -C HEAD # mor

我想将文件更改添加到尚未推式提交(不是最后一个)中

假设我有以下场景:

git init
touch test.txt
// do some changes to the text file
git commit -a -m "First change"

# if I now change something I can add it to the previous commit with:
git add test.txt
git commit --amend -C HEAD


# more changes to test.txt
git commit -a -m "Second change"

# more changes to test.txt
git commit -a -m "Third change"

# one more change
# how can I add the change to my second commit (second change)?

我希望问题是清楚的。如果没有,请告诉我。我尝试了git提交--amend-C HEAD~2,但那一个在我真正的git项目中引起了一些奇怪的麻烦。

你可以使用git-rebase。我建议阅读
git-help-rebase
,尤其是
INTERACTIVE_MODE
部分,因为在执行rebase时小心是明智的

假设上次提交是“第三次更改”,您可以:

  • 调用
    git-rebase-i头~2
  • git将用两行代码调用编辑器,其中显示第二次和第三次提交,每行代码上都有一个“pick”前缀。将“第二次更改”提交的“拾取”前缀更改为“编辑”。然后它应该看起来像:
  • 保存并退出编辑器
  • 您现在将回滚到“第二次更改之后”的状态,git将停止以允许您编辑/修改提交。因此,现在您可以
    git添加
    其他文件,并在准备就绪后执行
    git提交--修改
    ,这将加载一个编辑器,您可以在其中编辑第二个更改的提交日志消息
  • 一旦您对新的“第二次更改”感到满意,就发布一个
    git-rebase——继续
    。这将在修改后的第二次更改上重播第三次更改

  • 您可以使用
    git-rebase
    。我建议阅读
    git-help-rebase
    ,尤其是
    INTERACTIVE_MODE
    部分,因为在执行rebase时小心是明智的

    假设上次提交是“第三次更改”,您可以:

  • 调用
    git-rebase-i头~2
  • git将用两行代码调用编辑器,其中显示第二次和第三次提交,每行代码上都有一个“pick”前缀。将“第二次更改”提交的“拾取”前缀更改为“编辑”。然后它应该看起来像:
  • 保存并退出编辑器
  • 您现在将回滚到“第二次更改之后”的状态,git将停止以允许您编辑/修改提交。因此,现在您可以
    git添加
    其他文件,并在准备就绪后执行
    git提交--修改
    ,这将加载一个编辑器,您可以在其中编辑第二个更改的提交日志消息
  • 一旦您对新的“第二次更改”感到满意,就发布一个
    git-rebase——继续
    。这将在修改后的第二次更改上重播第三次更改
  • edit xxxxxxx Second change
    pick xxxxxxx Third change