3 Tracking Changes (
git add & commit)
The key commands for documenting changes are
git add and
Commits: a “commit” adds your latest changes to your repository. If a change is not committed, Git would not recognize the change.
git add: tells git which file’s changes you want to document. Formally, this is called adding a file to the “index/staging area”.
git commit: tells git to document the changes you specified using
git add(i.e., the files in the staging area), with an accompanying commit message that you have to provide to explain what changes were made.
While the process of adding and then committing seems redundant, overtime it will be apparent why we don’t always just commit ALL changes that have been made to your files, and how the ‘mandatory’ commit message creates a good documentation for your project.
3.1 Adding new files/modification (
Make sure you are in the correct directory
Make a new file
Check the status
## On branch master ## ## No commits yet ## ## Untracked files: ## (use "git add <file>..." to include in what will be committed) ## ## foo.txt ## ## nothing added to commit but untracked files present (use "git add" to track)
The status tells you
foo.txtis not being tracked, and that you can use
git addto start tracking it.
If there are untracked files, we would like to add those files so git will track their changes:
Check the status again,
foo.txt should now be ready to get committed (a commit is a revision to your files).
## On branch master ## ## No commits yet ## ## Changes to be committed: ## (use "git rm --cached <file>..." to unstage) ## ## new file: foo.txt
The status now tells you there are “Changes to be commited”, and
foo.txtis listed as a new file (in green).
3.2 Saving these changes to the repository (
Commit the file and note the identifier for this commit (e.g.,
## [master (root-commit) 33c7791] Add foo.txt to repo ## 1 file changed, 0 insertions(+), 0 deletions(-) ## create mode 100644 foo.txt
The output of a commit will tell you what has changes has been commited to the repository. In this example, “1 file was changed”. There is 0 insertions or deletions as the file is empty. “create mode” means that a file is being added to the repository (first time Git is being told this file exist).
What have we done so far? Let’s check the log, which list all the commits made so far.
## commit 33c779105a462d255ceb920665567007d27937ff ## Author: Shady Whale <email@example.com> ## Date: Fri Aug 21 21:57:39 2020 +0000 ## ## Add foo.txt to repo
Each commit in the log is identified by a long unique identifier. The author (and their email) of this commit is listed Lastly, the commit message will appear under the identifier and author.
PRACTICE - Let’s add a line to the file and
Add some text to the file
Add & commit your changes to
## [master 3c247c8] Add hello in foo.txt ## 1 file changed, 1 insertion(+)
The output tells you that you have added 1 file and there is 1 insertion(+)
Check the status and log again using
git status and
Git does not track an empty directory, so until a directory has a file in it, git would not track it.
If you want to correct the most recent commit message, use
git commit --amend
You have now used Git to track the file
foo.txt. You can use
git log to read what you have done so far in this repository!