Nik*_*man 156 git git-rebase
我对git中的整个rebasing功能有点新意.假设我做了以下提交:
A -> B -> C -> D
Run Code Online (Sandbox Code Playgroud)
之后,我意识到D包含一个修复程序,它依赖于添加的一些新代码A,并且这些提交属于一起.如何壁球A和D在一起,离开B与C一个人吗?
Rud*_*udi 218
你可以git rebase --interactive在B之前运行并重新排序D并将D压入A.
Git会打开一个编辑器,你会看到一个这样的文件:
pick aaaaaaa Commit A
pick bbbbbbb Commit B
pick ccccccc Commit C
pick ddddddd Commit D
# Rebase aaaaaaa..ddddddd onto 1234567 (4 command(s))
#
# 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
Run Code Online (Sandbox Code Playgroud)
现在您更改它看起来像这样的文件:
pick aaaaaaa Commit A
squash ddddddd Commit D
pick bbbbbbb Commit B
pick ccccccc Commit C
Run Code Online (Sandbox Code Playgroud)
然后git将A和D的变化合并为一个提交,然后将B和C放在一起.如果您不想保留D的提交消息,也可以使用"fix"关键字.
Nat*_*ate 41
注意:除非您知道后果,否则不应以任何方式更改已推送到其他仓库的提交.
git log --oneline -4
D commit_message_for_D
C commit_message_for_C
B commit_message_for_B
A commit_message_for_A
Run Code Online (Sandbox Code Playgroud)
git rebase --interactive
pick D commit_message_for_D
pick C commit_message_for_C
pick B commit_message_for_B
pick A commit_message_for_A
Run Code Online (Sandbox Code Playgroud)
键入i(将VIM置于插入模式)
将列表更改为如下所示(您不必删除或包含提交消息).不要拼错squash!:
pick C commit_message_for_C
pick B commit_message_for_B
pick A commit_message_for_A
squash D
Run Code Online (Sandbox Code Playgroud)
Esc然后键入ZZ(保存并退出VIM)
# This is a combination of 2 commits.
# The first commit's message is:
commit_message_for_D
# This is the 2nd commit message:
commit_message_for_A
Run Code Online (Sandbox Code Playgroud)
类型 i
将文本更改为您希望新提交消息的样子.我建议这是对提交中的更改的描述,A并且D:
new_commit_message_for_A_and_D
Run Code Online (Sandbox Code Playgroud)
Esc然后键入ZZ
git log --oneline -4
E new_commit_message_for_A_and_D
C commit_message_for_C
B commit_message_for_B
Run Code Online (Sandbox Code Playgroud)
git show E
(You should see a diff showing a combination of changes from A and D)
Run Code Online (Sandbox Code Playgroud)
您现在已经创建了一个新提交E.提交A并且D不再在您的历史中,但不会消失.您仍然可以在此时恢复它们一段时间git rebase --hard D(git rebase --hard将破坏任何本地更改!).
对于那些使用SourceTree的人:
确保您尚未推送提交。
Squash with previous| 归档时间: |
|
| 查看次数: |
23066 次 |
| 最近记录: |