$ git reset @~3 # go back 3 commits$ git reflogc4f708b HEAD@{0}: reset: moving to @~32c52489 HEAD@{1}: commit: more changes4a5246d HEAD@{2}: commit: make important changese8571e4 HEAD@{3}: commit: make some changes... earlier commits ...$ git reset 2c52489... and you're back where you started
*注意#0和#1等选项-它们可以丢弃数据。 另外,不要在你正在合作的任何分支上重写历史。
在许多系统上,git rebase -i将默认打开Vim。Vim不像大多数现代文本编辑器那样工作,所以看看如何使用Vim。如果你想使用不同的编辑器,请用git config --global core.editor your-favorite-text-editor更改它。
#!/bin/sh## git-fixup# Use staged changes to modify a specified commitset -ecmt=$(git rev-parse $1)git commit --fixup="$cmt"GIT_EDITOR=true git rebase -i --autosquash "$cmt~1"
For the above to work, put the below script into an executable file called git-commit-edit somewhere in your $PATH:
#!/bin/bash
set -euo pipefail
script_name=${0##*/}
warn () { printf '%s: %s\n' "$script_name" "$*" >&2; }die () { warn "$@"; exit 1; }
[[ $# -ge 2 ]] && die "Expected single commit to edit. Defaults to HEAD~"
# Default to editing the parent of the most recent commit# The most recent commit can be edited with `git commit --amend`commit=$(git rev-parse --short "${1:-HEAD~}")message=$(git log -1 --format='%h %s' "$commit")
if [[ $OSTYPE =~ ^darwin ]]; thensed_inplace=(sed -Ei "")elsesed_inplace=(sed -Ei)fi
export GIT_SEQUENCE_EDITOR="${sed_inplace[*]} "' "s/^pick ('"$commit"' .*)/edit \\1/"'git rebase --quiet --interactive --autostash --autosquash "$commit"~git reset --quiet @~ "$(git rev-parse --show-toplevel)" # Reset the cache of the toplevel directory to the previous commitgit commit --quiet --amend --no-edit --allow-empty # Commit an empty commit so that that cache diffs are un-reversed
echoecho "Editing commit: $message" >&2echo
git-amend-old() (# Stash, apply to past commit, and rebase the current branch on to of the result.current_branch="$(git rev-parse --abbrev-ref HEAD)"apply_to="$1"git stashgit checkout "$apply_to"git stash applygit add -ugit commit --amend --no-editnew_sha="$(git log --format="%H" -n 1)"git checkout "$current_branch"git rebase --onto "$new_sha" "$apply_to")
#This to show all the commits on one line$git log --oneline4f3d0c8 (HEAD -> documentation) docs: Add project description and included files"4d95e08 docs: Add created date and project title"eaf7978 (origin/master , origin/HEAD, master) Inital commit46a5819 Create README.md
Now I use git rebase to change the 2 last commits messages:$git rebase -i HEAD~2It opens the code editor and show this:
pick 4d95e08 docs: Add created date and project titlepick 4f3d0c8 docs: Add project description and included files
# Rebase eaf7978..4f3d0c8 onto eaf7978 (2 commands)## Commands:# p, pick <commit> = use commit# r, reword <commit> = use commit, but edit the commit message...
docs: Add created date and project title to the documentation "README.md"
# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit....
保存并关闭编辑第二条消息
docs: Add project description and included files to the documentation "README.md"
# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit....
保存并关闭。
在rebase结束时,您会收到这样的消息:Successfully rebased and updated refs/heads/documentation,这意味着您成功了。您可以显示更改:
5dff827 (HEAD -> documentation) docs: Add project description and included files to the documentation "README.md"4585c68 docs: Add created date and project title to the documentation "README.md"eaf7978 (origin/master, origin/HEAD, master) Inital commit46a5819 Create README.md