[英]How to make a small fix of an old commit when I haven't pushed yet?
For example one commit I have added a .gitignore
file. 例如,我添加了一个
.gitignore
文件。 I have done some work after that, but now realized that I should have add one more folder that I should ignore at that time. 之后,我做了一些工作,但现在意识到我应该再添加一个我当时应该忽略的文件夹。
mybranch HEAD 1: Much work 2: Much work 3: Add .gitignore origin/master 4: Others have this
I do not want to make a new commit that says "Edit .gitignore" but rather go back and fix that. 我不想做一个新的提交,说“ Edit .gitignore”,而是回去修复它。 Since I haven't pushed my commits to others yet I would like to tidy up my commits as much as possible.
由于我尚未将自己的提交推送给其他人,因此我想尽可能地整理自己的提交。
I figured I might have to check out that commit and amend it. 我认为我可能必须检查该提交并进行修改。 Then how can I repeat the newer commits?
那我该如何重复较新的提交呢? Doing merge one by one to the final point would make me ended up on a new branch.
一步一步地合并到最后一点将使我最终进入新的分支。 I would like to end up like the starting point minus the small change.
最后,我想像起点减去零钱一样。
You might want to use rebase
and squash commits into one. 您可能要使用
rebase
和squash提交合而为一。
Assuming you want to squash your last two commits into one commit: 假设您要将最后两次提交压缩为一次提交:
$ git rebase -i HEAD~3
Now you should have something like: 现在,您应该具有以下内容:
pick ccccccc third commit
pick bbbbbbb second commit
pick aaaaaaa first commit
# Rebase ...... onto ......
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
Replace pick
with squash
(or simply s
) on whatever commits you want to squash into one. 在想要将其压缩为一个的所有提交中,将
pick
替换为squash
(或简单地为s
)。
For more detailed explanation, go through the link posted above. 有关详细说明,请访问上面发布的链接。
What you can do is introduce a new commit, with the change to .gitignore
, and then move and squash it into the old commit. 您可以做的是引入一个新的提交,对
.gitignore
进行更改,然后将其移动并压缩到旧的提交中。
After the new commit you'll have 新提交后,您将拥有
HEAD 1: .gitignore edit
2: Much work
3: Much work
4: Add .gitignore
...
Then if you run git rebase -i HEAD~4
you'll be presented with the commits in an editor and have to choose which to keep. 然后,如果您运行
git rebase -i HEAD~4
您将在编辑器中看到提交,并且必须选择保留。 You can move the new [1] to right after [4], and set it to squash
instead of pick
, which would make it "meld into previous commit". 您可以将新的[1]移到[4]之后,然后将其设置为
squash
而不是pick
,这会使它“融合到先前的提交中”。
You can read more about this process here . 您可以在此处阅读有关此过程的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.