$ git log
commit 5c4a8e76f951eb7ee157f4136257f6666fddf1d1
Author: John Doe <johndoe@gmail.com>
Date: Sun Aug 28 14:40:30 2016 +0300
Changed 1.txt
commit 7f2ad8c26ad032800c049d0d6122c43410a5cbbc
Author: John Doe <johndoe@gmail.com>
Date: Sun Aug 28 14:39:30 2016 +0300
Initial commit
假设有一个文件需要添加到之前的提交中。
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
使用 将此文件添加到索引中git add。
$ git add --all
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test.txt
然后你发出一个命令git rebase -i HEAD~2并在提交 B 中用 squash 替换 pick (你可以只替换字母 s)
那些。最初它会向您显示类似的内容:
[ak@hostname testfolder]$ git rebase -i HEAD~2
pick 7921448 Commit A
pick 8a84e59 Commit B
# Rebase ce97832..8a84e59 onto ce97832
#
# 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
~
~
~
"~/testfolder/.git/rebase-merge/git-rebase-todo" 20L, 650C
您需要像这样将提交 B 合并到 A 中:
pick 7921448 Commit A
s 8a84e59 Commit B
保存后,将出现一个窗口,提示您选择新的合并提交的名称:
Rebasing (2/2)
# This is a combination of 2 commits.
# The first commit's message is:
Commit A
# This is the 2nd commit message:
Commit B
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached at 7921448
# You are currently editing a commit while rebasing branch 'master' on 'ce97832'.
#
# Changes to be committed:
# (use "git reset HEAD^1 <file>..." to unstage)
#
# new file: file2
# new file: file3
#
~
~
".git/COMMIT_EDITMSG" 20L, 494C
是的,该选项
--amend用于此。使用示例
让我们看看提交历史。
假设有一个文件需要添加到之前的提交中。
使用 将此文件添加到索引中
git add。然后,您可以
--amend使用git commit. 您还可以通过添加更改提交消息-m 'Commit message'。要使提交消息保持不变,只需传递一个空字符串而不是-m ''.让我们检查提交历史。
改变历史的其他方法
git rebase诸如和之类的命令可用于更改提交历史记录git filter-branch,但它们用于更复杂的情况。例如,如果你想更改一个在多次提交之前进行的提交,你可以使用交互式rebase-git rebase -i。您可以在此处阅读有关在 git 中更改历史记录的更多信息。
PS使用此技术时要小心
git commit --amend,因为它实质上会创建一个新提交,其中包括您正在修改的提交的更改 + 添加的更改。如果您已经将最新提交推送到远程存储库,请不要编辑它。有两种方法,一种是@Volen在上面已经介绍过,所以不再重复,第二种是使用rebase。应该做什么。
有一个要在其中添加文件的提交 A。
您创建添加此文件的提交 B。
然后你发出一个命令
git rebase -i HEAD~2并在提交 B 中用 squash 替换 pick (你可以只替换字母 s)那些。最初它会向您显示类似的内容:
您需要像这样将提交 B 合并到 A 中:
保存后,将出现一个窗口,提示您选择新的合并提交的名称:
您可能认为 amend 方法更简单。确实如此。但是,基于 rebase,您可以如此广泛地修改提交 - 拆分、合并、重新排列 - 其他方法无法在功能方面进行比较。
因此,我喜欢通过简单的操作来展示 rebase 的可能性,而你的操作非常简单。
PS 我举了一个来自 git 命令行的例子,当然,图形客户端也可以变基——例如,在 SourceTree 中,您在 A 之前的上一个提交上单击右键,然后在上下文菜单中选择 Rebase children interactively 项。
我会很简短:
如果在此之前的最后一次提交已经在服务器后面
push或服务器上,那么下一次提交push将不得不--force重写。如果服务器还没有被推送,那么你可以撤销最后一次提交
git reset --soft HEAD^,添加丢失的文件git add,再次重复提交。更简单地说:
修改将做同样的事情(将更改附加到最新的提交):