Make a Commit
In this section, we are going to
Check the status of your repo
Make a commit
Check status
If we inspect the commit history now, it will show nothing. To do that use git log
λ git log
fatal: your current branch 'master' does not have any commits yet
Let's create a file myfile.py
and place it in the repo (the same folder where .git
is located.
import numpy as np
arr = np.arange(10)
To check what Git oberves, use git status
λ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
myfile.py
nothing added to commit but untracked files present (use "git add" to track)
There are two pieces of key information here:
No commits yet: this makes sense as this is an empty repo and we haven't made any commits. If you see some commit, it will be strange.
Git finds a file and lists it as "Untracked files". Although Git is tracking the contents in this folder, it needs us to tell it exactly what to put in a commit. So let's track this file. To do that, use
git add
.
git add myfile.py
If you have multiple files, then
git add file1 file2 folder1 ...
If you just want to add all changes
git add .
Then do git status
again.
λ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: myfile.py
Commit
Now it's ready to be commited. To commit use git commit -m <message>
. You might be prompted to enter your user name and password if it's your first commit. Just follow the instructions to set it up using git config --global
.
λ git commit -m "First commit"
[master (root-commit) c96c526] First commit
1 file changed, 2 insertions(+)
create mode 100644 myfile.py
This shows a successful commit. Git also identifies that one file is added and there are two lines of change in that file (2 insertions). You can aslo see the first-8-digit excerpt of the hash for this commit (c96c526). Your hash might be different from here, since the first commit hash is random.
If we inspect the commit history now, we can see the whole hash as well as the commit message.
λ git log
commit c96c526a878cb72dd05e41b47fd14599a2305312 (HEAD -> master)
Author: Yu Sun <sunyu0410@gmail.com>
Date: Tue May 2 16:05:29 2023 +1000
First commit
What is the direct command prior to
git commit
. Why do you think it's needed?
Do it yourself
Make two more commits. Each commit adds one line to myfile.py
. One prints the mean and the other prints the standard deviation of arr
.
By the end, you will get something like this (note the long string following the word "commit", i.e. a HASH, will be different from here).
D:\Work\git-prj (master)
λ git log
commit ee7ebbd447a36e29e08287ff525023c5db017e56 (HEAD -> master)
Author: Yu Sun <sunyu0410@gmail.com>
Date: Tue May 16 10:21:57 2023 +1000
Print out the std dev
commit b1e55d3fd716d784826f877572f7af33ab2f13e5
Author: Yu Sun <sunyu0410@gmail.com>
Date: Tue May 16 10:21:35 2023 +1000
Print out the mean
commit c96c526a878cb72dd05e41b47fd14599a2305312
Author: Yu Sun <sunyu0410@gmail.com>
Date: Tue May 2 16:05:29 2023 +1000
First commit
Last updated