Sam*_*iao 2077 git git-rewrite-history
我通常会提交一份提交列表以供审核.如果我有以下提交:
HEAD
Commit3
Commit2
Commit1
...我知道我可以修改头部提交git commit --amend
.但是我怎么能修改Commit1
,因为它不是HEAD
提交?
Zel*_*luX 2744
例如,如果要修改回提交bbc643cd
,运行,可以使用git rebase
$ git rebase --interactive 'bbc643cd^'
Run Code Online (Sandbox Code Playgroud)
在默认的编辑,修改^
,以pick
在您要修改其犯行.进行更改,然后使用之前的相同消息提交它们:
$ git commit --all --amend --no-edit
Run Code Online (Sandbox Code Playgroud)
修改提交,然后修改
$ git rebase --continue
Run Code Online (Sandbox Code Playgroud)
返回上一个头部提交.
警告:请注意,这将更改该提交的SHA-1 以及所有子项 - 换句话说,这将从此时开始重写历史记录.如果使用命令推送,可以中断repos执行此操作edit
Zaz*_*Zaz 418
git rebase -i @~9 # Show the last 9 commits in a text editor
Run Code Online (Sandbox Code Playgroud)
找到所需的提交,更改pick
为e
(edit
),然后保存并关闭文件.Git将回退到该提交,允许您:
git commit --amend
进行更改,或git reset @~
丢弃最后一次提交,但不丢弃对文件的更改(即将您带到编辑文件时所处的位置,但尚未提交).后者对于执行更复杂的操作(如拆分为多个提交)非常有用.
然后,运行git rebase --continue
,Git将在修改后的提交之上重放后续更改.系统可能会要求您修复某些合并冲突.
注意:@
是指定提交之前的简写HEAD
,并且~
是指定提交之前的提交.
阅读有关在Git文档中重写历史记录的更多信息.
ProTip™:不要害怕尝试重写历史记录的"危险"命令* - Git默认情况下不会删除你的提交90天; 你可以在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
Run Code Online (Sandbox Code Playgroud)
*注意喜欢--hard
和选择的选项--force
- 他们可以丢弃数据.
* 此外,不要在您正在协作的任何分支上重写历史记录.
在许多系统上,git rebase -i
默认情况下会打开Vim.Vim不像大多数现代文本编辑器那样工作,所以看看如何使用Vim进行rebase.如果您更愿意使用其他编辑器,请更改它git config --global core.editor your-favorite-text-editor
.
thr*_*rau 72
互动变基带--autosquash
的东西,当我需要修正内容以前犯历史更深我经常使用.它实质上加快了ZelluX的答案所说明的过程,并且当您需要编辑多个提交时,它尤其方便.
从文档:
--autosquash
当提交日志消息以"squash!..."(或"fixup!...")开头,并且有一个提交标题以相同的...开头时,自动修改rebase -i的待办事项列表以便提交标记为压缩是在提交修改后立即进行的
假设您的历史记录如下所示:
$ git log --graph --oneline
* b42d293 Commit3
* e8adec4 Commit2
* faaf19f Commit1
Run Code Online (Sandbox Code Playgroud)
并且您有要修改为Commit2的更改,然后使用提交更改
$ git commit -m "fixup! Commit2"
Run Code Online (Sandbox Code Playgroud)
或者你可以使用commit-sha而不是commit消息,所以"fixup! e8adec4
甚至只是提交消息的前缀.
然后在提交之前启动交互式rebase
$ git rebase e8adec4^ -i --autosquash
Run Code Online (Sandbox Code Playgroud)
您的编辑器将打开已经正确订购的提交
pick e8adec4 Commit2
fixup 54e1a99 fixup! Commit2
pick b42d293 Commit3
Run Code Online (Sandbox Code Playgroud)
你需要做的就是保存并退出
bet*_*res 41
跑:
$ git rebase --interactive commit_hash^
each ^
表示要编辑的提交数量,如果只有一个(您指定的提交哈希),则只需添加一个^
.
使用Vim,您pick
可以reword
为要更改的提交更改单词,保存并退出(:wq
).然后git将提示您标记为reword的每个提交,以便您可以更改提交消息.
您必须保存每个提交消息并退出(:wq
)以转到下一个提交消息
如果要退出而不应用更改,请按 :q!
编辑:导航vim
你使用j
上升,k
下降,h
左转,然后l
向右(所有这些在NORMAL
模式下,按下ESC
进入NORMAL
模式).要编辑文本,请按i
以便进入INSERT
插入文本的模式.按ESC
返回NORMAL
模式:)
更新:这是github列出的一个很棒的链接如何用git撤消(几乎)任何东西
Fee*_*ure 18
如果由于某种原因你不喜欢交互式编辑器,你可以使用git rebase --onto
.
假设您要修改Commit1
.首先,从以前 分支Commit1
:
git checkout -b amending [commit before Commit1]
Run Code Online (Sandbox Code Playgroud)
二,抢Commit1
用cherry-pick
:
git cherry-pick Commit1
Run Code Online (Sandbox Code Playgroud)
现在,修改您的更改,创建Commit1'
:
git add ...
git commit --amend -m "new message for Commit1"
Run Code Online (Sandbox Code Playgroud)
最后,在隐藏任何其他更改之后,将其余提交移植到master
新提交之上:
git rebase --onto amending Commit1 master
Run Code Online (Sandbox Code Playgroud)
阅读:"rebase,进入分支amending
,所有提交Commit1
(非包含)和master
(包括)".也就是说,Commit2和Commit3完全削减了旧的Commit1.你可以只挑选它们,但这种方式更容易.
记得清理你的树枝!
git branch -d amending
Run Code Online (Sandbox Code Playgroud)
jus*_*tMe 15
基于文档
修改旧的或多个提交消息的消息
git rebase -i HEAD~3
Run Code Online (Sandbox Code Playgroud)
上面显示了当前分支上最后3个提交的列表,如果需要更多,则将3更改为其他提交.该列表将类似于以下内容:
pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.
Run Code Online (Sandbox Code Playgroud)
在要更改的每个提交邮件之前,使用reword替换pick.假设您更改了列表中的第二个提交,您的文件将如下所示:
pick e499d89 Delete CNAME
reword 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.
Run Code Online (Sandbox Code Playgroud)
保存并关闭提交列表文件,这将弹出一个新的编辑器,您可以更改提交消息,更改提交消息并保存.
Finaly Force推动修改后的提交.
git push --force
Run Code Online (Sandbox Code Playgroud)
Det*_*iel 11
我以为我会分享一个我正在使用的别名.它基于非交互式交互式rebase.要将它添加到您的git,请运行此命令(下面给出解释):
git config --global alias.amend-to '!f() { SHA=`git rev-parse "$1"`; git commit --fixup "$SHA" && GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^"; }; f'
Run Code Online (Sandbox Code Playgroud)
这个命令的最大优点是它不是vim的事实.
(1)当然,在rebase期间没有冲突
git amend-to <REV> # e.g.
git amend-to HEAD~1
git amend-to aaaa1111
Run Code Online (Sandbox Code Playgroud)
这个名字amend-to
似乎合适恕我直言.比较流程--amend
:
git add . && git commit --amend --no-edit
# vs
git add . && git amend-to <REV>
Run Code Online (Sandbox Code Playgroud)
git config --global alias.<NAME> '!<COMMAND>'
- 创建一个名为的<NAME>
执行非git命令的全局git别名<COMMAND>
f() { <BODY> }; f
- "匿名"bash功能.SHA=`git rev-parse "$1"`;
- 将参数转换为git revision,并将结果赋给变量 SHA
git commit --fixup "$SHA"
- fixup-commit for SHA
.查看git-commit
文档GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^"
git rebase --interactive "$SHA^"
部分已被其他答案所涵盖.--autosquash
是与之结合使用的内容git commit --fixup
,请参阅git-rebase
文档以获取更多信息GIT_SEQUENCE_EDITOR=true
是什么使整个事物非互动.我从这篇博文中了解到了这个黑客攻击.小智 10
更改最后一次提交:
\ngit commit --amend\n// or\ngit commit --amend -m "an updated commit message"\n
Run Code Online (Sandbox Code Playgroud)\nDon\xe2\x80\x99t 修改公共提交\n修改后的提交实际上是全新的提交,之前的提交将不再位于当前分支上。
\n例如,如果您想要更改最后三个提交消息或该组中的任何提交消息,您可以将要编辑的最后一个提交的父级作为参数提供给 git rebase -i,即 HEAD~2 ^ 或 HEAD~3。记住 ~3 可能会更容易,因为您\xe2\x80\x99正在尝试编辑最后三个提交,但请记住,您\xe2\x80\x99实际上是在指定四个提交之前,即您最后一次提交的父级想要编辑:
\n$ git rebase -i HEAD~3\n
Run Code Online (Sandbox Code Playgroud)\n\n
最好的选择是使用"Interactive rebase command"。
该
git rebase
命令非常强大。它允许您编辑 提交消息,合并提交,重新排序它们......等。每次重新提交提交时,都会为每次提交创建一个新的 SHA,无论内容是否会更改!使用此命令时应小心,因为它可能会产生严重影响,尤其是当您与其他开发人员合作时。他们可能会在您重新调整某些提交时开始处理您的提交。在您强制推送提交后,它们将不同步,您可能会在稍后发现混乱的情况。所以要小心!
建议
backup
在 rebase 之前创建一个分支,这样当您发现事情失控时,您可以返回到以前的状态。
git rebase -i <base>
Run Code Online (Sandbox Code Playgroud)
-i
代表“互动”。请注意,您可以在非交互模式下执行变基。前任:
#interactivly rebase the n commits from the current position, n is a given number(2,3 ...etc)
git rebase -i HEAD~n
Run Code Online (Sandbox Code Playgroud)
HEAD
表示您的当前位置(也可以是分支名称或提交 SHA)。该~n
手段“Nbeforeé,所以HEAD~n
将是列表中的‘一个你目前的前N’的提交。
git rebase
有不同的命令,如:
p
或者pick
保持原样提交。r
或reword
: 保留提交的内容但更改提交消息。s
或squash
: 将此提交的更改合并到先前的提交中(列表中其上方的提交)。... 等等。
注意:最好让 Git 与您的代码编辑器一起工作以使事情变得更简单。例如,如果您使用可视化代码,您可以像这样添加git config --global core.editor "code --wait"
。或者您可以在 Google 中搜索如何将您喜欢的代码编辑器与 GIT 相关联。
git rebase
我想更改我所做的最后 2 次提交,因此我的处理方式如下:
#This to show all the commits on one line
$git log --oneline
4f3d0c8 (HEAD -> documentation) docs: Add project description and included files"
4d95e08 docs: Add created date and project title"
eaf7978 (origin/master , origin/HEAD, master) Inital commit
46a5819 Create README.md
Run Code Online (Sandbox Code Playgroud)现在我git rebase
用来更改最后 2 个提交消息:
$git rebase -i HEAD~2
它打开代码编辑器并显示:
pick 4d95e08 docs: Add created date and project title
pick 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
...
Run Code Online (Sandbox Code Playgroud)
因为我想更改这 2 次提交的提交消息。所以我会输入r
或reword
代替pick
. 然后保存文件并关闭选项卡。请注意,它rebase
是在多步骤过程中执行的,因此下一步是更新消息。另请注意,提交按时间倒序显示,因此最后一次提交显示在该行中,第一次提交显示在第一行中,依此类推。
更新消息: 更新第一条消息:
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.
...
Run Code Online (Sandbox Code Playgroud)
保存并关闭 编辑第二条消息
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.
...
Run Code Online (Sandbox Code Playgroud)
保存并关闭。
在 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 commit
46a5819 Create README.md
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助新用户:)。
采用这种方法(它可能与使用交互式rebase完全相同),但对我而言,它是直截了当的.
注意:我提出这种方法是为了说明你可以做什么,而不是日常的替代方案.因为它有很多步骤(可能还有一些警告.)
假设您要更改提交,0
并且您当前正在使用feature-branch
some-commit---0---1---2---(feature-branch)HEAD
Run Code Online (Sandbox Code Playgroud)
签出此提交并创建一个quick-branch
.您还可以将功能分支克隆为恢复点(在启动之前).
?(git checkout -b feature-branch-backup)
git checkout 0
git checkout -b quick-branch
Run Code Online (Sandbox Code Playgroud)
你现在会有这样的事情:
0(quick-branch)HEAD---1---2---(feature-branch)
Run Code Online (Sandbox Code Playgroud)
阶段变化,隐藏其他一切.
git add ./example.txt
git stash
Run Code Online (Sandbox Code Playgroud)
提交更改并结帐回复 feature-branch
git commit --amend
git checkout feature-branch
Run Code Online (Sandbox Code Playgroud)
你现在会有这样的事情:
some-commit---0---1---2---(feature-branch)HEAD
\
---0'(quick-branch)
Run Code Online (Sandbox Code Playgroud)
重订feature-branch
到quick-branch
(解决沿途的任何冲突).申请藏匿并删除quick-branch
.
git rebase quick-branch
git stash pop
git branch -D quick-branch
Run Code Online (Sandbox Code Playgroud)
你最终得到:
some-commit---0'---1'---2'---HEAD(feature-branch)
Run Code Online (Sandbox Code Playgroud)
在重新定位时,Git不会复制(虽然我不能真正说明在多大程度上)0提交.
注意:所有提交哈希值都是从我们最初要更改的提交开始更改的.
我发现自己经常修复过去的提交,因此我为它编写了一个脚本.
这是工作流程:
git commit-edit <commit-hash>
Run Code Online (Sandbox Code Playgroud)
这将使您想要编辑的提交.
根据您的意愿修复并暂存提交.
(你可能想用来git stash save
保存你没有提交的任何文件)
重做提交--amend
,例如:
git commit --amend
Run Code Online (Sandbox Code Playgroud)完成rebase:
git rebase --continue
Run Code Online (Sandbox Code Playgroud)要使上述工作正常,请将以下脚本放入一个名为git-commit-edit
somewhere 的可执行文件中$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 ]]; 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
Run Code Online (Sandbox Code Playgroud)
我执行以下操作,包括更改本地提交的日期和时间:
git rebase -i HEAD~6
Run Code Online (Sandbox Code Playgroud)
~6
是要显示的提交历史记录的数量。
pick
为edit
要编辑的提交。Ctrl+O
保存并Ctrl+X
退出)git commit --amend --date="2022-09-02T19:10:04" -m "NEW_MSG"
git rebase --continue
如果有更多提交需要编辑,则从第 1 点开始重复
最后我验证更改,如果一切正常,我会执行push
要获取非交互式命令,请将包含此内容的脚本放在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"
Run Code Online (Sandbox Code Playgroud)
通过暂存更改(使用git add
)然后运行来使用它git fixup <commit-to-modify>
.当然,如果你遇到冲突,它仍然是互动的.
我解决了这个,
1)通过创建具有我想要的更改的新提交..
r8gs4r commit 0
Run Code Online (Sandbox Code Playgroud)
2)我知道我需要与哪个提交合并。这是提交3。
所以,git rebase -i HEAD~4
#4 代表最近的 4 次提交(这里提交 3 排在第 4 位)
3) 在交互式 rebase 中,最近的提交将位于底部。它看起来很像,
pick q6ade6 commit 3
pick vr43de commit 2
pick ac123d commit 1
pick r8gs4r commit 0
Run Code Online (Sandbox Code Playgroud)
4)如果你想与特定的合并,我们需要重新排列提交。应该是这样的
parent
|_child
pick q6ade6 commit 3
f r8gs4r commit 0
pick vr43de commit 2
pick ac123d commit 1
Run Code Online (Sandbox Code Playgroud)
重新排列后,您需要替换p
pick
为f
(修复将在没有提交消息的情况下合并)或s
(带有提交消息的壁球合并可以在运行时更改)
然后保存你的树。
现在与现有提交合并完成。
注意:除非您自己维护,否则这不是首选方法。如果您的团队规模很大,那么重写 git 树的方法是不可接受的,最终会导致冲突,而您知道其他人不会这样做。如果你想用更少的提交来保持你的树干净可以试试这个,如果它的小团队否则它不是可取的......
git stash
+ rebase
自动化
当我需要多次修改旧提交以进行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"
)
Run Code Online (Sandbox Code Playgroud)
用法:
git add
如果已经在仓库中则不需要git-amend-old $old_sha
我喜欢这个,--autosquash
因为它不会压缩其他无关的修复程序。
归档时间: |
|
查看次数: |
691599 次 |
最近记录: |