如何在 Git 中修改多个提交以更改作者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4981126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to amend several commits in Git to change author
提问by pauldoo
I have made a series of commits in Git and I realise now that I forgot to set my user name and user email properties correctly (new machine). I have not yet pushed these commits to my repository, so how can I correct these commits before I do so (only the 3 latest commits on the master branch)?
我已经在 Git 中进行了一系列提交,现在我意识到我忘记正确设置我的用户名和用户电子邮件属性(新机器)。我还没有将这些提交推送到我的存储库,那么在我这样做之前如何更正这些提交(只有主分支上的 3 个最新提交)?
I have been looking at git reset
and git commit -C <id> --reset-author
, but I don't think I'm on the right track.
我一直在研究git reset
和git commit -C <id> --reset-author
,但我认为我没有走在正确的轨道上。
回答by Cascabel
Rebase/amend seems inefficient, when you have the power of filter-branch at your fingertips:
当您拥有触手可及的 filter-branch 功能时,rebase/amend 似乎效率低下:
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
(split across lines for clarity, but not necessary)
(为了清晰起见,在线条上分开,但不是必需的)
Be sure to inspect the result when you're done, to make sure that you didn't change anything you didn't mean to!
完成后一定要检查结果,以确保您没有更改任何您不想要的东西!
回答by Alex
The interactive rebase approach is pretty nice when used in conjunction with exec. You can run any shell command against a specific commit or all commits in the rebase.
与 exec 结合使用时,交互式 rebase 方法非常好。您可以针对特定提交或变基中的所有提交运行任何 shell 命令。
First set your git author settings
首先设置你的 git 作者设置
git config --global user.name "John Doe"
git config --global user.email [email protected]
Then to reset the author for all commits after the given SHA
然后在给定的 SHA 之后为所有提交重置作者
git rebase -i YOUR_SHA -x "git commit --amend --reset-author -CHEAD"
This will pop up your editor to confirm the changes. All you need to do here is save and quit and it will go through each commit and run the command specified in the -x flag.
这将弹出您的编辑器以确认更改。您在此处需要做的就是保存并退出,它将完成每次提交并运行 -x 标志中指定的命令。
Per @Dave's comment below, you can also change the author while maintaining the original timestamps with:
根据下面@Dave 的评论,您还可以在保持原始时间戳的同时更改作者:
git rebase -i YOUR_SHA -x "git commit --amend --author 'New Name <[email protected]>' -CHEAD"
回答by Chris Maes
To change the author only for the last commit:
仅更改最后一次提交的作者:
git commit --amend --author 'Author Name <[email protected]>' --no-edit
Suppose you only want to change the author for the last N commits:
假设您只想更改最后 N 次提交的作者:
git rebase -i HEAD~4 -x "git commit --amend --author 'Author Name <[email protected]>' --no-edit"
NOTES
笔记
- the
--no-edit
flag makes sure thegit commit --amend
doesn't ask an extra confirmation - when you use
git rebase -i
, you can manually select the commits where to change the author,
- 该
--no-edit
标志确保git commit --amend
不会要求额外的确认 - 使用时
git rebase -i
,您可以手动选择提交更改作者的位置,
the file you edit will look like this:
您编辑的文件将如下所示:
pick 897fe9e simplify code a little
pick abb60f9 add new feature
exec git commit --amend --author 'Author Name <[email protected]>' --no-edit
pick dc18f70 bugfix
回答by Fernando Diaz Garrido
I believe what you are looking for is git rebase --interactive
我相信你正在寻找的是 git rebase --interactive
It allows you to go reset to an specific commit and then go throw the history changing adding or grouping commits
它允许您重置为特定提交,然后抛出历史更改添加或分组提交
Here you have an explanation https://web.archive.org/web/20100213104931/http://blog.madism.org/index.php/2007/09/09/138-git-awsome-ness-git-rebase-interactive
回答by stevec
Thismethod was documented by github for this very purpose. The steps are:
github为此目的记录了此方法。步骤是:
- Open the terminal and make a bareclone of your repo
- 打开终端并制作你的 repo的裸克隆
git clone --bare https://github.com/user/repo.git
cd repo
- Edit the following script (replacing
OLD_EMAIL
,CORRECT_EMAIL
, andCORRECT_NAME
)
- 编辑下面的脚本(取代
OLD_EMAIL
,CORRECT_EMAIL
和CORRECT_NAME
)
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
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
- Copy/paste the script into your terminal and press enter to run it.
- Push your changes with
git push --force --tags origin 'refs/heads/*'
and you're done!
- 将脚本复制/粘贴到您的终端中,然后按 Enter 运行它。
- 推送您的更改,
git push --force --tags origin 'refs/heads/*'
您就完成了!
回答by Edison
If you're feeling unsafe about debasing and amending you could do it this way. At the same time you'd also be setting the global config which you probably meant to do anyway.
如果你对贬低和修改感到不安全,你可以这样做。同时,您还将设置您可能打算进行的全局配置。
git reset HEAD~
(undo last commit)
git reset HEAD~
(撤销上次提交)
git config --global user.name "Your Name"
git config --global user.name "Your Name"
git config --global user.email [email protected]
git config --global user.email [email protected]
git commit -m "message"
git commit -m "message"