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.
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 project dir
cd my_project_dir
then do:
# commit one file git add myFile.py git commit myFile.py -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 myFile.py # add a new file git commit myFile.py -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 filename
git commit filename -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 dir 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."