Exploring Commits

(this post if part of the material I cover in my devops course)

Adding more commits

  • In the previous post we have created our first commit.
  • It was a new file that we added to our repository
  • let's add more commits:
    • Add file2 that is already in the working directory
    • Add another file, this time inside a directory
    • a change in a file already there

Adding more file

  • Adding file2:
 1$> 
 2$> git status
 3On branch master
 4Untracked files:
 5  (use "git add <file>..." to include in what will be committed)
 6	file2
 7
 8nothing added to commit but untracked files present (use "git add" to track)
 9$> git add file2 
10$> git status
11On branch master
12Changes to be committed:
13  (use "git restore --staged <file>..." to unstage)
14	new file:   file2
15
16$> git commit -m "adding file2"
17[master 1d84dea] adding file2
18 1 file changed, 1 insertion(+)
19 create mode 100644 file2
20$> 
  • Now, create a directory with a file, then add it:
 1$> 
 2$> pwd
 3/home/osboxes/my-project
 4$> mkdir dir-A
 5$> echo "good content" > dir-A/file3
 6$> git status
 7On branch master
 8Untracked files:
 9  (use "git add <file>..." to include in what will be committed)
10	dir-A/
11
12nothing added to commit but untracked files present (use "git add" to track)
13$> git add dir-A/file3 
14$> git status
15On branch master
16Changes to be committed:
17  (use "git restore --staged <file>..." to unstage)
18	new file:   dir-A/file3
19
20$> git commit -m "adding file3"
21[master bc9e96b] adding file3
22 1 file changed, 1 insertion(+)
23 create mode 100644 dir-A/file3
24$> 

Looking at the commit history

  • Let's look at those commit we have created by using the git log command:
 1$> 
 2$> git log
 3commit bc9e96bdd271ffea773de812a69689ff13ac2dab (HEAD -> master)
 4Author: Yuval Shaul <yuval.shaul@gmail.com>
 5Date:   Tue Jan 23 03:26:58 2024 -0500
 6
 7    adding file3
 8
 9commit 1d84deaa1e6a0b6389ee8e50debfd3fbd4c5cd01
10Author: Yuval Shaul <yuval.shaul@gmail.com>
11Date:   Tue Jan 23 03:25:01 2024 -0500
12
13    adding file2
14
15commit 696c35e3aa1aef1df4e3aa26a50974732b97a560
16Author: Yuval Shaul <yuval.shaul@gmail.com>
17Date:   Tue Jan 23 02:26:58 2024 -0500
18
19    this is my first commit
20$> 
  • We can see each commit, with its commit message.
  • Each commit has a SHA-1 id, that looks like this:
1bc9e96bdd271ffea773de812a69689ff13ac2dab
  • SHA-1 is a cryptograpic has function.
    It means that the code you get (the hash) represent that data that it was computed from.
    If you change even one bit in the data, you'll get a different hash.

Seeing your project in older versions

  • This is not a practical way to do that, but we can ask git to show us the project as it was before a specific commit was saved.
  • I'm going to retreive the project as it was immediatelly after commiting the 2nd commit.
  • I do not use the whole hash value:
    • The full value is: 1d84deaa1e6a0b6389ee8e50debfd3fbd4c5cd01
    • I'll be using just the first 10 digits: 1d84deaa1e
 1$> 
 2$> git checkout 1d84deaa1e
 3Note: switching to '1d84deaa1e'.
 4
 5You are in 'detached HEAD' state. You can look around, make experimental
 6changes and commit them, and you can discard any commits you make in this
 7state without impacting any branches by switching back to a branch.
 8
 9If you want to create a new branch to retain commits you create, you may
10do so (now or later) by using -c with the switch command. Example:
11
12  git switch -c <new-branch-name>
13
14Or undo this operation with:
15
16  git switch -
17
18Turn off this advice by setting config variable advice.detachedHead to false
19
20HEAD is now at 1d84dea adding file2
21$> 
22$> ls
23file1  file2
24$> 
  • Since we have not covered git branches yet, we'll not explain all of the messages in the output.
  • You can get back to see all commits using this command:
    git checkout master