How to modify a specified commit in git?
I usually submit a list of commits for review. If I have:
HEAD
Commit3
Commit2
Commit1
I know that I can modify head commit with git commit --amend
, but how can I modify Commit1
, given that it is not the HEAD
commit?
You can use git rebase, for example, if you want to modify back to commit bbc643cd
, run
$ git rebase --interactive 'bbc643cd^'
In the default editor, modify pick
to edit
in the line whose commit you want to modify. Make your changes and then commit them with the same message you had before:
$ git commit --all --amend --no-edit
to modify the commit, and after that
$ git rebase --continue
to return back to the previous head commit.
WARNING : Note that this will change the SHA-1 of that commit as well as all children -- in other words, this rewrites the history from that point forward. You can break repos doing this if you push using the command git push --force
Use the awesome interactive rebase:
git rebase -i @~9 # Show the last 9 commits in a text editor
Find the commit you want, change pick
to e
( edit
), and save and close the file. Git will rewind to that commit, allowing you to either:
git commit --amend
to make changes, or git reset @~
to discard the last commit, but not the changes to the files (ie take you to the point you were at when you'd edited the files, but hadn't committed yet). The latter is useful for doing more complex stuff like splitting into multiple commits.
Then, run git rebase --continue
, and Git will replay the subsequent changes on top of your modified commit. You may be asked to fix some merge conflicts.
Note: @
is shorthand for HEAD
, and ~
is the commit before the specified commit.
Read more about rewriting history in the Git docs.
Don't be afraid to rebase
ProTip™: Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:
$ git reset @~3 # go back 3 commits
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~3
2c52489 HEAD@{1}: commit: more changes
4a5246d HEAD@{2}: commit: make important changes
e8571e4 HEAD@{3}: commit: make some changes
... earlier commits ...
$ git reset 2c52489
... and you're back where you started
* Watch out for options like --hard
and --force
though — they can discard data.
* Also, don't rewrite history on any branches you're collaborating on.
On many systems, git rebase -i
will open up Vim by default. Vim doesn't work like most modern text editors, so take a look at how to rebase using Vim. If you'd rather use a different editor, change it with git config --global core.editor your-favorite-text-editor
.
Interactive rebase with --autosquash
is something I frequently use when I need to fixup previous commits deeper in the history. It essentially speeds up the process that ZelluX's answer illustrates, and is especially handy when you have more than one commit you need to edit.
From the documentation:
--autosquash
When the commit log message begins with "squash! …" (or "fixup! …"), and there is a commit whose title begins with the same …, automatically modify the todo list of rebase -i so that the commit marked for squashing comes right after the commit to be modified
Assume you have a history that looks like this:
$ git log --graph --oneline
* b42d293 Commit3
* e8adec4 Commit2
* faaf19f Commit1
and you have changes that you want to amend to Commit2 then commit your changes using
$ git commit -m "fixup! Commit2"
alternatively you can use the commit-sha instead of the commit message, so "fixup! e8adec4
or even just a prefix of the commit message.
Then initiate an interactive rebase on the commit before
$ git rebase e8adec4^ -i --autosquash
your editor will open with the commits already correctly ordered
pick e8adec4 Commit2
fixup 54e1a99 fixup! Commit2
pick b42d293 Commit3
all you need to do is save and exit
链接地址: http://www.djcxy.com/p/548.html上一篇: 我怎样才能将两个提交合并成一个?
下一篇: 如何修改git中的指定提交?