Essential Git for Scientists
  • Introduction
  • Basic Concepts
    • Version Control
    • Git
    • Repo
    • Commits
    • Branches
    • Remotes
    • GitHub
    • De-centralisation
    • Summary
  • Basic Operations
    • Install Git
    • Create a Repo
    • Make a Commit
    • Inspect a Previous Commit
    • Revert a Change
    • Make a Branch
    • Extend a Branch
    • Fast-forward merge
    • Resolve conflicts
  • Intermediate Concepts
    • Commits
    • Three Trees
    • Rebase
    • Fetch
    • Pull
    • Push
  • Advanced Concepts
    • Reset
    • Interactive rebase
    • Formatted patches
    • Blame
    • Stash
    • Log filter
  • Cookbook
    • Undo
    • Branches
    • Diff
    • Stash
    • Merge
    • Hooks
    • Squashing
    • Rebase
    • Interactive Rebase
    • LFS
    • Submodules
    • Remote
    • Force push
    • Identify merged branches
    • Formated patches
    • Apply patches
    • Interactive rebase
    • Squash commits
    • Pull rebase
    • Log
    • Blame
    • Biset
    • Reset
  • Exercise
    • Exercise 1
    • Exercise 2
Powered by GitBook
On this page
  • Steps
  • In action
  • Discussion
  1. Basic Operations

Inspect a Previous Commit

In this section, we will inspect a previous commit, performing the "time machine" function of Git.

The files reflect where HEAD points to

Your location on the commit history is indicated by a reference called the HEAD pointer. The HEAD always points to a commit. Your working directory reflects the status of that commit.

The commit history is like an audio tape. The HEAD pointer is similar to the playhead of the tape machine, indicating the current position. Normally, the HEAD is point to the tip (last commit) of a branch, because that's where the next commit will follow right after. When we inspect a previous commit, Git moves the HEAD to somewhere in the middle of the tape. This is called an detached HEAD.

We will inspect a previous commit by detaching the HEAD. When the HEAD points to a different commit, the files in the working directory will change accordingly. This is automatically coordinated by Git, like magic.

Steps

  1. Look at the commit history. Each commit has a corresponding HASH value.

  2. Get the HASH for the commit you want to inspect

  3. Go to a previous commit: git checkout that HASH

  4. Get back to the latest commit: git checkoutthe branch name

In action

Out current myfile.py looks like this

D:\Work\git-prj (master)
λ cat myfile.py
import numpy as np
arr = np.arange(10)
print(arr.mean())
print(arr.std())

The commit history:

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                                                

Say we want to jump to the second commit of "Print out the mean". Grab the HASH (actually the first 6 digits will be distinctive enough), and git checkout that commit.

D:\Work\git-prj (master)
λ git checkout b1e55d3fd716d784826f877572f7af33ab2f13e5
Note: switching to 'b1e55d3fd716d784826f877572f7af33ab2f13e5'.

You are in 'detached HEAD' state. You ...

Notice it's now in a detached HEAD state. This means the HEAD moved away from the last commit. If we look at myfile.py now, it will reflect the content at that snapshot.

D:\Work\git-prj (HEAD detached at b1e55d3)
λ cat myfile.py
import numpy as np
arr = np.arange(10)
print(arr.mean())

Note that the last line print(arr.std()) is not there.

Finally, we get back to the latest commit. If you check out the branch name, Git will move to the latest commit.

D:\Work\git-prj (HEAD detached at ee7ebbd)
λ git checkout master
Switched to branch 'master'

The last line is back in myfile.py.

D:\Work\git-prj (master)
λ cat myfile.py
import numpy as np
arr = np.arange(10)
print(arr.mean())
print(arr.std())

Discussion

Can you make new commits with a detached HEAD?

You can make new commits. It's best to create a new branch for it. Otherwise once you move back to the original branch, these commits will be orphaned.

How do I find out the branch name?

When you do git log, it shows which branch the HEAD is pointing to.

D:\Work\git-prj (master)                                        
λ git log                                                       
commit ee7ebbd447a36e29e08287ff525023c5db017e56 (HEAD -> master)
Author: Yu Sun <sunyu0410@gmail.com> 

Here it's called master, which is the default branch name in Git.

Alternatively, use git branch. More on that in the branch section.

PreviousMake a CommitNextRevert a Change

Last updated 1 year ago