让我们用一个具体的例子来展示 Git 中的作者(author)和提交者(committer)之间的区别,并通过实际的 Git 命令来演示。
假设 Alice 写了一些代码并创建了一个补丁文件,然后 Bob 使用这个补丁文件在他的 Git 仓库中创建了一个提交。在这个场景中,Alice 是作者,而 Bob 是提交者。
Alice 创建补丁文件
- Alice 在她的本地仓库中编写了代码并提交:
echo "print('Hello, world!')" > hello.py
git add hello.py
git commit -m "Add hello script"
- 然后,Alice 使用
git format-patch
创建补丁文件:
git format-patch -1 HEAD
这将创建一个补丁文件,例如 0001-Add-hello-script.patch
。
Bob 应用补丁文件
- Bob 收到了 Alice 的补丁文件,并将其应用到他自己的仓库中:
git apply 0001-Add-hello-script.patch
- Bob 使用
git commit
创建提交,同时保留 Alice 作为作者:
git commit --author="Alice <alice@example.com>" -m "Add hello script from Alice"
- Bob 将这些更改推送到远程仓库:
git push origin master
检查提交信息
Bob 可以使用 git log
查看提交信息,以确认作者和提交者的信息:
git log --format=fuller
输出可能如下:
commit ead4a5f7f8c6b0d4d9f2a9b8b1c6e8d2d9f3a1c4
Author: Alice <alice@example.com>
AuthorDate: Mon May 19 14:22:34 2024 +0000
Commit: Bob <bob@example.com>
CommitDate: Mon May 19 15:45:56 2024 +0000
Add hello script from Alice
在这个例子中:
- Author(作者):Alice,表明是谁编写了这段代码。
- Committer(提交者):Bob,表明是谁在仓库中创建了这个提交。
通过这种方式,Git 记录了谁是实际编写代码的人(Alice),以及谁将其提交到仓库中的人(Bob),从而区分了作者和提交者。