简体   繁体   English

当我还没有推送时,如何对旧提交进行一些小的修复?

[英]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.

相关问题 Git:如何确保不将哈希提交/推入尚未推送到远程的子模块? - Git: how do I make sure I don't commit/push hashes to submodules which haven't been pushed to remote yet? 如何撤消尚未推送的提交? - How do I undo commits that haven't been pushed yet? 如何查看尚未推送的提交列表? - How do I view the list of commits that haven't been pushed yet? 如何取消对 dev 分支的意外提交(尚未推送)? - How can I cancel the accidental commit to dev branch (hasn't pushed it yet)? 我在错误的分支上进行了更改。 我还没有承诺。 如何将我的更改提交到预期的分支? - I made changes while on the wrong branch. I haven't committed yet. How do commit my changes to the intended branch? 从尚未推送的旧提交中删除文件(Git扩展) - remove a file from an old commit not pushed yet (Git Extension) 如何修复我的本地存储库,以便我可以保存尚未推送到主 git 存储库的提交的工作? - How do I fix my local repo so I can save work from commits that haven’t been pushed to the master git repo? 如何修改旧的推送提交,然后推送新提交 - How to modify old, pushed commit, with new commits pushed after it 我如何使用git使旧的提交成为当前的主提交? - How do I make an old commit the current master commit with git? 如何更改尚未推送的倒数第二个提交消息 - How to change second last commit message which is not pushed yet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM