How to Commit Changes in Git

How do I commit my changes using Git?

Committing in Git is a two-phase process: first you stage your changes with git add, then you record them with git commit.

Step 1: Stage your changes

Before committing, you must add the files you want to include to the staging area.

  • Stage a specific file: git add README.md
  • Stage a specific directory: git add directory/
  • Stage all files in the repository: git add -A
  • Interactively choose parts of a file: git add -p

Without running git add, git commit won't include any changes.

Run git status before committing to confirm you are on the correct branch and that the right files are staged.

Step 3: Create the commit

Once your files are staged, use one of these commands:

  1. Commit with an inline message (most common): git commit -m "descriptive commit message"

  2. Commit and open your default text editor to write the message: git commit (If your editor opens as VI or Vim, press Esc, then type :wq, then press Enter to save and exit.)

  3. Skip staging for already-tracked files and commit in one step: git commit -am "descriptive commit message" (This only works for files Git is already tracking — it will not include brand-new files.)

Writing a good commit message

Commit messages should be short, descriptive, and written in the present tense. Examples:

  • git commit -m "create file structure for Git guides"
  • git commit -m "update broken URL to Git resources"

Step 4: Push your commit

To share your commit with others, upload it to the remote branch: git push

Need to fix the most recent commit?

If your last commit has a typo in the message or is missing a change, and it hasn't been pushed yet, you can update it with: git commit --amend

Source: https://github.com/git-guides/git-add, https://github.com/git-guides/git-commit

Related articles