Create a Git Commit

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 code changes. 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. In this lab, you will learn how to create a Git commit.


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-12715{{"`Create a Git Commit`"}} end

Create a Git Commit

You have made some changes to your code and want to save them as a snapshot in your Git repository. However, you don't want to save all the changes you made, only the ones that are relevant to the current feature or bug fix. How can you create a commit containing only the relevant changes?

For this lab, let's use the repository from https://github.com/labex-labs/git-playground, follow these steps:

  1. Clone the repository and navigate it:

    git clone https://github.com/labex-labs/git-playground
    cd git-playground
  2. Configure your github account in the environment:

    git config --global user.name "your-name"
    git config --global user.email "your-email"
  3. Add "hello,labex" to the README.md file, add it to the staging area and commit it with the message "Update README.md":

    echo "hello,labex" >> README.md
    git add .
    git commit -m "Update README.md"

    The -m option allows you to specify a commit message. Make sure the message is descriptive and explains what changes the commit contains.

This is the result of running the git log command:

Summary

Creating a Git commit is an essential part of the development process. It allows you to save snapshots of your code and keep track of the changes you make over time. By following the steps outlined in this lab, you can create a commit containing only the relevant changes and add a descriptive message to explain the changes made.

Other Git Tutorials you may like