How to Commit Changes in Git

Updated on 29 May, 2025
Learn how to create, skip staging, and amend commits in Git to maintain a clear project history.
How to Commit Changes in Git header image

Committing changes is one of the most important actions in Git. It records the current state of your project by saving staged changes along with a message that explains what was done and why. Whether you're fixing a typo, adding a feature, or cleaning up old code, well-structured commits help you track progress, collaborate with others, and maintain a clean version history. This article covers basic commits, skipping the staging step, and amending previous commits.

The Short Answer Version

# Stage all modified files
$ git add .

# Commit with a message
$ git commit -m "Describe what this commit does"

# Commit changes to tracked files without staging
$ git commit -a -m "Skip staging and commit modified files"

# Update the most recent commit message
$ git commit --amend -m "New commit message"

Common Options for Committing Changes

Command Description
git add <file> Stages a specific file for the next commit
git add . Stages all modified and new files in the current directory
git commit -m "message" Commits all staged changes with a commit message
git commit -a -m "message" Commits all modified tracked files without staging
git commit --amend Updates the most recent commit with new changes or message

Git Commit Without Staging

The -a flag lets you commit all modified tracked files directly without staging them first. This is useful when you want to skip git add but still commit file changes.

Command Syntax

git commit -a -m "Your commit message"
  • -a: Automatically stages all modified tracked files.
  • -m: Attaches a commit message directly in the command.

Command Demonstration

  1. Create a tracked file under Git.

    console
    $ echo "Hello" > demo.txt
    
  2. Stage the file.

    console
    $ git add demo.txt
    
  3. Make an initial commit.

    console
    $ git commit -m "Initial commit"
    
  4. Modify the file without staging.

    console
    $ echo "Appended second line to demo.txt" >> demo.txt
    
  5. Commit the changes directly using -a.

    console
    $ git commit -a -m "Added second line to demo.txt"
    
  6. Check your commit history.

    console
    $ git log --oneline
    

This workflow skips git add but only works on tracked files. Any new files still require staging before committing.

Update or Amend a Git Commit

You can use the --amend flag with git commit to modify your most recent commit. This is helpful for adding missing changes, fixing typos in messages, or cleaning up your commit history before pushing.

Command Syntax

git commit --amend
  • --amend: Replaces the previous commit with a new one. This can modify the commit message, content, or both.
  • -m: (Optional) Use this to supply a new message inline instead of opening your editor.

Command Demonstration

Example 1: Add a forgotten file to the last commit

  1. Stage the missing file.

    console
    $ git add file2.txt
    
  2. Amend the last commit.

    console
    $ git commit --amend
    

    This opens your text editor to update the message (or keep it unchanged).

Example 2: Change the commit message without changing content

console
$ git commit --amend -m "Updated commit message"

This rewrites the most recent commit message without modifying the files.

Warning
Avoid amending commits that have already been pushed to a remote repository shared with others. It rewrites history and may cause conflicts for your collaborators.

Conclusion

In this article, you learned how to commit changes in Git effectively. You explored the core concepts of staging files, creating commits, skipping the staging step with -a, and updating the most recent commit using --amend. These workflows help maintain a clean, organized history and improve team collaboration.

With these techniques, you're now equipped to write clear commit messages, streamline your Git workflow, and maintain a version history that's easy to navigate and collaborate on.

Tags:

Comments

No comments yet.