๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Save Changes to Repository - git commit


Introduction

  • git commit command is used to save the changes from the staging area to the Git repository.
  • Below is the diagram represents the working of git commit command :- Git Working
  • It saves the state of your project, including all tracked files.
  • When you make a commit in Git, it is simply added to the currently active branch.
    Git Commit
  • Each commit has a unique identifier (SHA-1 hash), allowing you to track changes accurately.
  • A commit usually includes a commit message, describing the purpose of the change. For example :
    git commit -m "message"
  • Commits help in version control, enabling rollback to previous states if needed.
  • You can see the history of your commits using git log to understand how your project has changed over time.

How to use git commit ?

  1. Stage your changes by any one below command:
    • git add <file-name> # Stages a specific file
    • git add . # Stages all changes in the directory
  2. Commit your changes:
    • git commit -m "Your commit message here" # Commits with a message
  3. The command will show the following:
    • Git Commit Command

Different ways to use git commit ?

  1. Commit with a Message:
    • git commit -m "Your commit message"
    • Saves staged changes with a short description.
    • This command is the most commonly used way to commit changes in Git.
  2. Commit Only Specific Files:
    • git commit filename.txt -m "Commit message"
    • Commits only the specified file(s).
  3. Commit with a Default Message Editor:
    • git commit
    • Opens the default editor to write a commit message.
  4. Commit All Tracked Files (Skip git add):
    • git commit -a -m "Your commit message"
    • Automatically stages and commits modified and deleted files (but not new untracked files).
  5. Amend the Last Commit:
    • git commit --amend -m "Updated commit message"
    • Modifies the last commit (useful for fixing commit messages or adding missed changes).