pau*_*doo 153 git git-commit git-rewrite-history
我在Git中做了一系列提交,现在我意识到我忘记了正确设置我的用户名和用户邮件属性(新机器).我还没有将这些提交推送到我的存储库,所以在我这样做之前如何更正这些提交(只有主分支上的3个最新提交)?
我一直在看git reset和git commit -C <id> --reset-author,但我不认为我是在正确的轨道上.
Cas*_*bel 200
当您拥有filter-branch的强大功能时,重新/修改似乎效率低下:
git filter-branch --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "incorrect@email" ]; then
GIT_AUTHOR_EMAIL=correct@email;
GIT_AUTHOR_NAME="Correct Name";
GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all
Run Code Online (Sandbox Code Playgroud)
(为清晰起见,分隔线,但没有必要)
完成后一定要检查结果,以确保你没有改变任何你不想做的事情!
Ale*_*lex 128
当与exec一起使用时,交互式rebase方法非常好.您可以针对特定提交或rebase中的所有提交运行任何shell命令.
首先设置你的git作者设置
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
Run Code Online (Sandbox Code Playgroud)
然后在给定的SHA之后重置作者的所有提交
git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"
Run Code Online (Sandbox Code Playgroud)
这将弹出您的编辑器以确认更改.您需要做的就是保存并退出,它将遍历每次提交并运行-x标志中指定的命令.
根据Per @ Dave的评论,您还可以在保持原始时间戳的同时更改作者:
git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <new_address@example.com>' -CHEAD"
Run Code Online (Sandbox Code Playgroud)
Chr*_*aes 67
要仅为最后一次提交更改作者:
git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
Run Code Online (Sandbox Code Playgroud)
假设您只想更改最后N次提交的作者:
git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit"
Run Code Online (Sandbox Code Playgroud)
笔记
--no-edit标志可以确保git commit --amend不要求额外的确认git rebase -i,您可以手动选择提交更改作者的位置,您编辑的文件将如下所示:
pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <author.name@mail.com>' --no-edit
pick dc18f70 bugfix
Run Code Online (Sandbox Code Playgroud)
cha*_*rsi 54
这里得票最高的答案现在已经过时了。当使用 git filter-branch 时,Git 显示这个可怕的警告 -
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead.
Run Code Online (Sandbox Code Playgroud)
filter-repo还不是 git 的一部分,需要单独安装。
WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as 'git filter-repo'
(https://github.com/newren/git-filter-repo/) instead.
Run Code Online (Sandbox Code Playgroud)
要仅替换以前提交中的电子邮件,请运行如下命令 -
# Requires git v2.22+ and python v3.5+. Check with -
git --version && python3 --version
# Install using pip
pip3 install git-filter-repo
Run Code Online (Sandbox Code Playgroud)
要替换之前提交中的电子邮件和作者姓名,请运行如下命令 -
git filter-repo --email-callback '
return email if email != b"incorrect@email" else b"correct@email"
'
Run Code Online (Sandbox Code Playgroud)
确保将命令粘贴到终端时有缩进。回调使用 python 语法,因此缩进很重要。
在文档中阅读有关过滤器存储库回调的更多信息。
GitHub 为此目的记录了此方法(尽管 GitHub 已将其删除)。步骤是:
git clone --bare https://github.com/user/repo.git
cd repo
Run Code Online (Sandbox Code Playgroud)
OLD_EMAIL,CORRECT_EMAIL和CORRECT_NAME)#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Run Code Online (Sandbox Code Playgroud)
git push --force --tags origin 'refs/heads/*'您就完成了!| 归档时间: |
|
| 查看次数: |
39697 次 |
| 最近记录: |