git 如何将文件添加到以前的提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14096721/
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
How to add file to a previous commit?
提问by James Raitsev
In last hour or so i have modified files
在过去的一个小时左右,我修改了文件
A
ATest
B
BTest
In order to make sure my commit messages line up with the actual change, committed A
with a description. Unfortunately i have not included ATest
into that commit.
为了确保我的提交消息与实际更改一致,提交A
了描述。不幸的是,我没有包含ATest
在那个提交中。
Meanwhile, still not committed are B
and BTest
.
同时,仍然没有提交的是B
和BTest
。
What is the best way to proceed at this point? I'd like to either:
在这一点上进行的最佳方法是什么?我想:
- Revert previous commit without affecting my currently uncommitted files?
- Add another file under the same description to the previous commit?
- 恢复以前的提交而不影响我当前未提交的文件?
- 将相同描述下的另一个文件添加到之前的提交?
回答by William Pursell
To add a new file to the previous commit:
将新文件添加到之前的提交:
$ git add new-file
$ git commit --amend
You can use git commit --amend --no-edit
if you don't want to change the commit message.
git commit --amend --no-edit
如果您不想更改提交消息,则可以使用。
回答by amalloy
Here's an amusing flowchart1which is also surprisingly handy: it gives the correct recommendation both for the original question and for the amended "What if it weren't the last commit?" question.
这是一个有趣的流程图1,它也非常方便:它为原始问题和修改后的“如果它不是最后一次提交怎么办?”都给出了正确的建议。题。
1Taken from http://justinhileman.info/article/git-pretty/
回答by amalloy
Add a file to a previous commit
将文件添加到先前的提交
If you have already pushed the branch you are working on, please see the man pagesfirst. In particular, please note:
如果您已经推送了您正在处理的分支,请先查看手册页。请特别注意:
Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history.
重新定位(或任何其他形式的重写)其他人基于工作的分支是一个坏主意:它下游的任何人都被迫手动修复他们的历史记录。
However, if you haven't pushed your branch, prepare to enter the danger zone.
但是,如果您还没有推动分支,请准备好进入危险区域。
Find the commit hash
找到提交哈希
First, you need to know the commit hash of the commit that you want to add to. This is shown by git log
. You actually want to specify the commit priorto the one you want to add to. (You can think of it as the start index to the slice of commits you want to alter.) You can make sure you have the right commit by running git log -1 HEAD~n
. Where 'n' is an integer you increment until you have the right commit. Or you could count, no really.
首先,您需要知道要添加到的提交的提交哈希。这由 显示git log
。您实际上希望在要添加的提交之前指定提交。(您可以将其视为要更改的提交片段的起始索引。)您可以通过运行git log -1 HEAD~n
. 其中 'n' 是一个整数,您可以递增,直到正确提交。或者你可以数数,真的没有。
But, if you do count, at least confirm you have the right commit with git log -1 HEAD~5
or whatever your count was. You should NOT see the commit you want to add to.
但是,如果您确实计数了,至少要确认您有正确的提交git log -1 HEAD~5
或无论您的计数是什么。您不应该看到要添加到的提交。
DANGER, heh
危险,呵呵
Now you are ready to run git rebase -i HEAD~5
. Or whatever your commit hash is. This will bring up your favourite text editor and a file to edit. The file is the todo list for the rebase command. The comments in the file tell you what options you have. Simply find the line with the commit you want to add to, and on that line change 'pick' to 'edit'. Now save and close the file.
现在您已准备好运行git rebase -i HEAD~5
。或者无论您的提交哈希是什么。这将调出您最喜欢的文本编辑器和要编辑的文件。该文件是 rebase 命令的待办事项列表。文件中的注释告诉您有哪些选项。只需找到您要添加的提交所在的行,然后在该行中将“pick”更改为“edit”。现在保存并关闭文件。
The rebase will stop once it reaches the commit you told it to edit. Run a git status
to see the extra information it provides. Stage your files to add to the commit with git add .
or whatever the filenames are.
一旦到达你告诉它编辑的提交,rebase 就会停止。运行 agit status
以查看它提供的额外信息。暂存您的文件以添加到提交中,git add .
无论文件名是什么。
Then, do git commit --amend
. This will amend the commit you chose to edit.
然后,做git commit --amend
。这将修改您选择编辑的提交。
Finally, run git rebase --continue
.
最后,运行git rebase --continue
。
If in doubt, on Linux you can find out more by reading through the docs output by man git-rebase
or git --help rebase
.
如果有疑问,在 Linux 上,您可以通过阅读man git-rebase
或的文档输出来了解更多信息git --help rebase
。