Exercises

Exercise 1 - Recovering Older Versions of a File

Audrey has made changes to the Python script that she has been working on for weeks, and the modifications she made this morning “broke” the script and it no longer runs. She has spent ~ 1hr trying to fix it, with no luck…

Luckily, she has been keeping track of her project’s versions using Git! Which commands below will let her recover the last committed version of her Python script called data_cruncher.py?

  1. git checkout HEAD

  2. git checkout HEAD data_cruncher.py

  3. git checkout HEAD~1 data_cruncher.py

  4. git checkout <unique ID of last commit> data_cruncher.py

  5. Both 2 and 4

Exercise 2 - Reverting a Commit

Audrey is collaborating with colleagues on her Python script. She realizes her last commit to the project’s repository contained an error, and wants to undo it. Audrey wants to undo correctly so everyone in the project’s repository gets the correct change. The command git revert [erroneous commit ID] will create a new commit that reverses the erroneous commit.

Git revert is an operation that takes a specified commit and creates a new commit which inverses the specified commit.

Below are the right steps and explanations for Audrey to use git revert, what is the missing command?

________ Look at the git history of the project to find the commit ID

Copy the ID (the first few characters of the ID, e.g. 0b1d055).

git revert [commit ID]

Type in the new commit message.

Save and close

Exercise 3 - Understanding Workflow and History

What is the output of the last command in

$ cd planets #planet is the name of a directory
$ echo "Venus is beautiful and full of love" > venus.txt
$ git add venus.txt
$ echo "Venus is too hot to be suitable as a base" >> venus.txt
$ git commit -m "Comment on Venus as an unsuitable base"
$ git checkout HEAD venus.txt
$ cat venus.txt #this will print the contents of venus.txt to the screen

Outputs:

1. Venus is too hot to be suitable as a base

2. Venus is beautiful and full of love

3. Venus is beautiful and full of love
   Venus is too hot to be suitable as a base

4. Error because you have changed venus.txt without committing the changes

Last updated

Massachusetts Institute of Technology