Git: Commit Files
After you made some change to files, you need to do the following steps, in order:
- Add the file to staging area.
- Commit staging area (files) into (local) repository.
- Optionally, 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 xProjectDir
git add xfilename
git commit xfilename -m "summary_of_what_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 git add:
git add file1 file2 file3
- several files.
git add .
- all files in current dir.
git add -A .
-
all files in current dir, including new or deleted file/dir
The
-A
option will include any new files in your dir, or files you deleted. Basically, it adds your working dir.
How to unadd a file?
How to delete a file or dir?
Use the git rm
command, then do commit.
git rm filename
git rm -r dirName
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 dir in your normal work flow.
When all's good, simply git add -A .
, then do a commit.
Git automatically take care of removed files, or files that have changed name.