Create a commit by a different author

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a popular version control system used by developers to manage their codebase. One of the essential features of Git is the ability to create commits, which are snapshots of the code at a particular point in time. By default, Git uses the user's name and email to identify the author of a commit. However, there may be situations where you need to create a commit by a different author. In this challenge, you will learn how to create a commit by a different author in Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12709{{"`Create a commit by a different author`"}} end

Create a commit by a different author

Suppose you are working on a project with a team of developers, and one of your team members has made some changes to the code. However, they are not available to commit the changes themselves, and you need to create a commit on their behalf. In this scenario, you can use the --author option to change the name and email of the commit's author. This option is useful when you need to attribute a commit to a different person, such as when you are committing code on behalf of a colleague who is on vacation or sick leave.

To create a commit by a different author, you can use the following command:

git commit -m < message > --author="<name> <email>"

Let's say you are working on a project hosted on the https://github.com/labex-labs/git-playground repository. You have made some changes to the code, and you need to create a commit on behalf of your colleague, John Doe, who is not available to commit the changes himself. To do this, you can use the following command:

git clone https://github.com/labex-labs/git-playground
cd git-playground
git config --global user.email "your email"
git config --global user.name "your username"
echo "Fix the network bug" > README.md
git add .
git commit -m "Fix the bug" --author="John Doe <[email protected]>"

This command will create a new commit with the message "Fix the bug" and attribute it to John Doe.

This is the finished result:

Summary

In this challenge, you learned how to create a commit by a different author in Git. You can use the --author option to change the name and email of the commit's author. This option is useful when you need to attribute a commit to a different person, such as when you are committing code on behalf of a colleague who is not available.

Other Git Tutorials you may like