Alice: first commits
Now that Alice has a proper git repository in place, she can take advantage of it to start developing her novel.
Lab
- Our author wants to take a look at the current state of her repo:
git status
- Time to add the current version of the chapter to the git index (aka staging zone):
git add chapter-01.md
- The output of the
add
command was not very verbose, so Alice looks again at the state of the repo
git status
- Now that everything seems on its place, she will put the current text of the chapter in the repository by creating her first commit:
git commit -m "Initial draft of the first chapter"
- She wants to check that everything is being tracked as expected. To do it, she starts by using the
log
command:
git log
- How does the repo knows what is the current position in the chain of revisions? We can explore the structure created on disk by the
commit
:
cat .git/HEAD
cat .git/refs/heads/main # Whole sha-1 HEAD
Actually, there are several ways to get the value stored in a
reference. What would be the best way to programmatically
retrieve the SHA-1 of the current revision?git rev-p████ HEAD
git rev-p████ HEAD
Solution
git rev-parse HEAD
- Now Alice can safely apply the new paragraph to the text, without any fear to lose the content of the previous version:
git status
ed chapter-01.md << EOF
2i
Tim is a lean and athletic young man with short brown hair and piercing blue eyes. He has a strong jawline and a sun-kissed complexion from spending so much time at the beach. His body is toned and muscular from his active lifestyle, and he exudes a sense of energy and enthusiasm for life.
.
w
q
EOF
- Good. Let’s see what are the
unstaged
changes
git status
- The author is interested to check what are the differences (the changesets) between the current working area and the commited revision.
git diff chapter-01.md
- Looks good, so Alice puts the new changes in the
git
database
git add chapter-01.md
git commit -m "Added description of Tim"
git status
- Alice is happy to be able to travel through the history of all the
applied changes by using the
log
command:
git log
- After taking a break and having her breakfast, our writer wants to refresh how was the first version of the story. She starts by listing all the available revisions:
git rev-list --all
- Alice knows how to get the SHA-1 the current revision:
git rev-parse HEAD
- And also, the SHA-1 of the previous revision, corresponding in this
case to the original
commit
git rev-parse HEAD~
- Being a developer, Alice cannot resist the temptation of playing with the revision SHA-1s to retrieve a particular version of the file:
PREV_REVISION=$(git rev-parse HEAD~)
echo The previous revision was $PREV_REVISION
git show $PREV_REVISION:chapter-01.md
- Although, in the end, she knows it is easier to just use the symbolic reference name:
git show HEAD~:chapter-01.md
Umh... ok... and how can Alice see the differences between the current version of the chapter and the originally commited one (two revisions ago)?git d███ HEAD~ chapter-01.md
git d███ HEAD~ chapter-01.md
Solution
git diff HEAD~ chapter-01.md
Questions
- What does it mean that a set of files are untracked?
- Which command would allow you to access the version of
chapter-01.md
stored two revisions ago? And five?