我有以下提交历史记录:
头部头部~头部~2头部~3
gitcommit--修改当前HEAD提交。但是如何修改HEAD~3?
我有以下提交历史记录:
头部头部~头部~2头部~3
gitcommit--修改当前HEAD提交。但是如何修改HEAD~3?
当前回答
如果您还没有推送提交,则可以使用git reset HEAD^[1,2,3,4…]返回到上一次提交
例如
git commit <file1> -m "Updated files 1 and 2"
git commit <file3> -m "Updated file 3"
抱歉,忘记在第一次提交时添加file2。。。
git reset HEAD^1 // because I only need to go back 1 commit
git add <file2>
这将在第一次提交时添加file2。
其他回答
Run:
$git rebase--交互式commit_hash^
每个^表示要编辑的提交次数,如果只有一个(指定的提交哈希),则只需添加一个^。
使用Vim,您可以更改要更改、保存和退出的提交的单词pick to reword(:wq)。然后git将提示您标记为reword的每个提交,以便您可以更改提交消息。
您必须保存并退出(:wq)以转到下一个提交消息的每个提交消息
如果要退出而不应用更改,请按:q!
编辑:要在vim中导航,请使用j向上,k向下,h向左,l向右(所有这些都在正常模式下,按ESC键转到正常模式)。要编辑文本,请按i以进入INSERT模式,在该模式下插入文本。按ESC返回正常模式:)
更新:这里有一个来自github的链接,列出了如何使用git撤消(几乎)任何操作
当我需要更深入地修复历史中以前的提交时,我经常使用交互式rebase和--autosquash。它本质上加快了ZelluX的回答所说明的过程,当您需要编辑多个提交时,它特别方便。
根据文档:
--自动撤销当提交日志消息以“squash…“(或”修复…“),并且有一个提交的标题以相同的开头…, 自动修改rebase-i的todo列表,以便标记为挤压的提交在要修改的提交之后立即出现
假设您的历史记录如下:
$ git log --graph --oneline
* b42d293 Commit3
* e8adec4 Commit2
* faaf19f Commit1
并且您有要修改为Commit2的更改,然后使用
$ git commit -m "fixup! Commit2"
或者,您可以使用commit sha而不是commit消息,因此“fixup!e8adec4”或甚至只是commit消息的前缀。
然后在提交之前启动交互式重新基础
$ git rebase e8adec4^ -i --autosquash
编辑器将打开已正确排序的提交
pick e8adec4 Commit2
fixup 54e1a99 fixup! Commit2
pick b42d293 Commit3
你只需要保存并退出
git存储+再基础自动化
因为当我需要多次修改Gerrit审查的旧提交时,我一直在做:
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 stash
git checkout "$apply_to"
git stash apply
git add -u
git commit --amend --no-edit
new_sha="$(git log --format="%H" -n 1)"
git checkout "$current_branch"
git rebase --onto "$new_sha" "$apply_to"
)
GitHub上游。
用法:
修改源文件,如果已在repo中,则无需gitaddgit修改旧$old_sha
我很喜欢这一点,因为它不会挤压其他无关的修复。
自动交互式重新基础编辑,然后提交恢复,以备重做
我发现自己经常修复一个过去的错误,为此我写了一个脚本。
以下是工作流程:
git提交编辑<提交哈希>这将在您要编辑的提交时将您丢弃。修复并按您希望的方式进行提交。(您可能希望使用git存储保存来保存未提交的文件)重新提交--修改,如:修改最后一次提交完成重新基准:git rebase—继续
为了实现上述功能,请将以下脚本放入$PATH中名为gitcommitedit的可执行文件中:
#!/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 ]]; then
sed_inplace=(sed -Ei "")
else
sed_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 commit
git commit --quiet --amend --no-edit --allow-empty # Commit an empty commit so that that cache diffs are un-reversed
echo
echo "Editing commit: $message" >&2
echo
要获得非交互式命令,请在PATH中放置包含以下内容的脚本:
#!/bin/sh
#
# git-fixup
# Use staged changes to modify a specified commit
set -e
cmt=$(git rev-parse $1)
git commit --fixup="$cmt"
GIT_EDITOR=true git rebase -i --autosquash "$cmt~1"
使用它,先暂存更改(使用gitadd),然后运行gitfixup<commit to modify>。当然,如果您遇到冲突,它仍然是交互式的。