Connect with us

Git

How to Commit Code in Git?

Spread the love

Committing code is a fundamental operation in Git, allowing developers to record changes to a repository’s history. Each commit serves as a snapshot of the project’s state, complete with a message describing the changes.

This blog explains how to commit code in Git, covering essential commands, best practices, and troubleshooting tips.

What Does It Mean to Commit in Git?

In Git, a commit captures the state of your project at a particular point in time. It is an atomic unit of change, containing the following:

  • A message that describes the changes made.
  • A unique identifier (SHA) to track the commit.
  • The actual changes to the files in the repository.

Prerequisites

  1. Git Installed: Ensure Git is installed on your system. Run git --version to confirm.
  2. Initialized Repository: You must have a Git repository. If not, initialize one using:
   git init
  1. Configured User Details: Set up your name and email, as these appear in commit metadata:
   git config --global user.name "Your Name"
   git config --global user.email "[email protected]"

Step 1: Stage Your Changes

Before committing, you need to stage the changes. Staging tells Git which files or modifications should be included in the commit.

1.1 Check the Status of Your Repository

Run:

git status

This command shows the current state of your working directory and staging area, including:

  • Untracked files: New files not yet tracked by Git.
  • Modified files: Files with changes not staged for commit.
  • Staged files: Files ready to be committed.

1.2 Stage Files

To stage changes, use the git add command:

  • Stage a specific file:
  git add file-name
  • Stage multiple files:
  git add file1 file2
  • Stage all changes (use with caution):
  git add .
  • Stage a directory:
  git add directory-name

Step 2: Commit Your Changes

Once changes are staged, commit them using the git commit command.

2.1 Basic Commit

Run:

git commit -m "Your commit message here"

For example:

git commit -m "Added user authentication module"

2.2 Writing a Detailed Commit Message

For a more descriptive commit, skip the -m option:

git commit

This opens your default text editor (e.g., vim or nano), where you can write a multi-line commit message. For example:

Added user authentication module

- Implemented login and registration features.
- Integrated JWT-based authentication.
- Updated database schema to store user credentials securely.

2.3 Amend an Existing Commit

If you forgot to stage a file or made a typo in your commit message, you can amend the last commit:

git add missing-file
git commit --amend

This replaces the previous commit with the amended version. Be cautious if you’ve already pushed the commit to a remote repository.


Step 3: Verify the Commit

After committing, check the commit history to verify your changes.

3.1 View Recent Commits

Run:

git log

This shows a detailed history of commits, including:

  • Author name and email
  • Commit date
  • Commit message

For a concise history, use:

git log --oneline

3.2 View Changes in the Commit

To see the changes included in a specific commit:

git show commit-hash

Replace commit-hash with the SHA of the commit.


Best Practices for Writing Commit Messages

  1. Be Descriptive: Use clear and concise messages that explain what was done and why.
  2. Use the Present Tense: Write messages as if you’re giving instructions (e.g., “Add feature” instead of “Added feature”).
  3. Separate Subject and Body:
  • Subject Line: Summarize the change in 50 characters or less.
  • Body: Provide additional context if necessary.

Example:

Fix bug in user authentication

- Resolved an issue where users couldn't log in with valid credentials.
- Added tests to prevent regression.

Troubleshooting Commit Issues

1. Forgot to Stage Changes

If you attempt to commit without staging files, you’ll see:

nothing to commit, working tree clean

Solution: Stage the changes using git add and try again.


2. Committed to the Wrong Branch

If you mistakenly commit to the wrong branch:

  1. Create a new branch:
   git branch new-branch-name
  1. Switch to the new branch:
   git checkout new-branch-name

3. Remove an Unwanted Commit

If you need to undo a commit that hasn’t been pushed:

git reset --soft HEAD~1

This unstages the commit but preserves your changes in the working directory.


Conclusion

Committing code in Git is an essential skill for maintaining a structured, traceable history of changes in your project. By staging changes, writing clear commit messages, and following best practices, you can ensure your commits are meaningful and collaborative. With practice, committing code will become second nature in your development workflow.


Spread the love
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *