git 你如何以不同的用户身份提交代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696938/
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 do you commit code as a different user?
提问by Carl
I want to be able to do this for a script. I'm essentially re-creating the entire version history of some code in Git - it currently uses a different version control system. I need the script to be able to add in the commits to Git while preserving the commit's original author (and date).
我希望能够为脚本执行此操作。我基本上是在 Git 中重新创建某些代码的整个版本历史记录 - 它目前使用不同的版本控制系统。我需要脚本能够将提交添加到 Git,同时保留提交的原始作者(和日期)。
Assuming I know the commit author and the date/time the change was made, is there a Git command that allows me to do this? I'm assuming there is, because git-p4 does something similar. I'm just asking for the best way to do it.
假设我知道提交作者和更改的日期/时间,是否有 Git 命令允许我执行此操作?我假设有,因为 git-p4 做了类似的事情。我只是要求最好的方法来做到这一点。
回答by Tim Henigan
Check out the --author
option for git commit
:
查看以下--author
选项git commit
:
From the man page:
从手册页:
--author=<author>
Override the commit author. Specify an explicit author using the standard
A U Thor <[email protected]>
format. Otherwise<author>
is assumed to be a pattern and is used to search for an existing commit by that author (i.e.rev-list --all -i --author=<author>
); the commit author is then copied from the first such commit found.
--author=<author>
覆盖提交作者。使用标准
A U Thor <[email protected]>
格式指定明确的作者。否则<author>
假定为模式并用于搜索该作者的现有提交(即rev-list --all -i --author=<author>
);然后从找到的第一个此类提交中复制提交作者。
回答by friederbluemle
Just to add to this: The --author
option mentioned in the accepted answer will only override the author, not the committerinformation of the commit.
只需添加到这样的:--author
在接受的答案中提到的选项只覆盖了作家,而不是提交者的提交信息。
That is the correct behavior in most cases, but if for some reason you need to manually override the committer informationas well, use the GIT_COMMITTER_NAME
and GIT_COMMITTER_EMAIL
environment variables (there is a GIT_COMMITTER_DATE
as well). See Git-Internals-Environment-Variables
在大多数情况下这是正确的行为,但如果由于某种原因您还需要手动覆盖提交者信息,请使用GIT_COMMITTER_NAME
和GIT_COMMITTER_EMAIL
环境变量(也有GIT_COMMITTER_DATE
)。请参阅Git-内部-环境-变量
$ GIT_COMMITTER_NAME="New Name" GIT_COMMITTER_EMAIL="[email protected]" git commit --author="New Name <[email protected]>"
This will make the commit look like it was authored andcommitted by the specified user.
这将使提交看起来像是由指定用户创作和提交的。
回答by BeardOverflow
Edit your ~/.gitconfig file and append a new alias where you can customize a non-default user and email.
编辑您的 ~/.gitconfig 文件并附加一个新别名,您可以在其中自定义非默认用户和电子邮件。
[user]
name = My Name
email = [email protected]
[alias]
commit-x = -c user.name='My X Name' -c user.email='[email protected]' commit
commit-y = -c user.name='My Y Name' -c user.email='[email protected]' commit
commit-z = -c user.name='My Z Name' -c user.email='[email protected]' commit
Test it
测试一下
git commit -m "Custom message with committer and author My Name <[email protected]>"
git commit-x -m "Custom message with committer and author My X Name <[email protected]>"
git commit-y -m "Custom message with committer and author My Y Name <[email protected]>"
git commit-z -m "Custom message with committer and author My Z Name <[email protected]>"