Git: Commit Files

By Xah Lee. Date: . Last updated: .

After you made some change to files, you need to do the following steps, in order:

  1. Add the file to staging area.
  2. Commit staging area (files) into (local) repository.

Then, optionally, you can git push to a remote repository. [see Git: Push to Server]

When using git, you should always cd to your project dir first.

How to commit a change?

After you made changes to the file, then you can commit your changes to local repository. To commit, cd to the working dir, then do:

# commit one file
git add
git commit -m "summary of what you changed."

Any file you want to commit, you must first git add, even if the file already exists in the repository.

The git add command basically means add it to the “index” file. The purpose of “index” is for next commit. This “index” file is also called “staging area”.

More examples of commit:

# commit several files
git add file1 file2
git commit -m"Fixed bug 123."
# commit all files in current dir (not including newly created files)
git add .
git commit -m"Fixed bug 123."
# commiting all files in current dir, including new or deleted file/dir
git add -A .
git commit -m"Fixed bug 123."

The -A option will include any new files in your dir, or files you deleted. Basically, it'll commit your working dir to the local repository.

How to add a new file or dir?

Use the command git add, then git commit.

git add               # add a new file
git commit -m "summary of what you changed."

How to unadd a file?

If you added a file by mistake, you can unadd.

# unadd a file
git reset myFileName
# unadd all added files in current dir
git reset .

How to delete a file or dir?

Use the git rm command, then commit. The git rm command is similar to git add, in the sense that both changes the staging area.

git rm file_name
git commit file_name -m"summary of change."
git rm -r dir_name
git commit dir_name -m"summary of change."

git will delete the files/dir for you.

If you already deleted the files, you can still call git rm to update the staging area, then do a commit.

With git, you normally never use git rm command. You simply {delete, add, change} files on your working tree in your normal work flow. When all's good, simply git add -A . then git commit . -m "msg". Git will automatically take care of removed files, or files that changed name.

# commiting all files in current dir, including new or deleted file/dir
git add -A .
git commit -m"summary of what you changed."

If you have a question, put $5 at patreon and message me.


  1. Install git
  2. Git Basics
  3. Git Ignore File
  4. Commit Files
  5. Push to Server
  6. Pull from Server
  7. Find Difference
  8. View Log
  9. Revert Change
  10. Branching
  11. Temp Save: git stash
  12. What's HEAD
  13. FAQ