Exercise Version Control#
1) Places to create Git repositories#
Along with information about the Zipf’s Law project, Amira would also like to keep some notes on Heaps’ Law.
Despite her colleagues’ concerns, Amira creates a heaps-law
project inside her zipf
project as follows:
$ cd ~/zipf
$ mkdir heaps-law
$ cd heaps-law
$ git init heaps-law
Is the git init
command that she runs inside the heaps-law
subdirectory
required for tracking files stored there?
2) Removing before saving#
If you’re working on an older version of Git,
you may see an output from git status
suggesting
you can take files out of the staging area
using git rm --cached
.
Try this out:
Create a new file in an initialized Git repository called
example.txt
.Use
git add example.txt
to add this file.Use
git status
to check that Git has noticed it.Use
git rm --cached example.txt
to remove it from the list of things to be saved.
What does git status
now show?
What (if anything) has happened to the file?
3) Viewing changes#
Make a few changes to a file in a Git repository, then view those differences using both git diff
and git diff --word-diff
.
Which output do you find easiest to understand?
Note: If you performed this exercise in your Zipf’s Law project, we recommend discarding (not committing) your changes made to a file.
4) Committing changes#
Which command(s) below would save changes to myfile.txt
to a local Git repository?
# Option 1
$ git commit -m "Add recent changes"
# Option 2
$ git init myfile.txt
$ git commit -m "Add recent changes"
# Option 3
$ git add myfile.txt
$ git commit -m "Add recent changes"
# Option 4
$ git commit -m myfile.txt "Add recent changes"
5) Write your biography#
Create a new Git repository on your computer called
bio
. Make sure the directory containing this repository is outside yourzipf
project directory!Write a three-line biography for yourself in a file called
me.txt
and commit your changes.Modify one line and add a fourth line.
Display the differences between the file’s original state and its updated state.
6) Workflow and history#
Assume you made the following changes in your bio
repository.
What is the output of the last command in the sequence below?
$ echo "Sharing information about myself." > motivation.txt
$ git add motivation.txt
$ echo "Documenting major milestones." > motivation.txt
$ git commit -m "Motivate project"
$ git restore motivation.txt
$ cat motivation.txt
Sharing information about myself.
Documenting major milestones.
Sharing information about myself.
Documenting major milestones.
An error message because we have changed
motivation.txt
without committing first.
7) Ignoring nested files#
Suppose our project has a directory results
with two subdirectories called data
and plots
.
How would we ignore all of the files in results/plots
but not ignore files in results/data
?
8) Including specific files#
How would you ignore all .dat
files in your root directory except for final.dat
?
(Hint: find out what the exclamation mark !
means in a .gitignore
file.)
9) Exploring the GitHub interface#
Browse to your zipf
repository on GitHub.
Under the Code
tab, find and click on the text that says “NN commits” (where “NN” is some number).
Hover over and click on the three buttons to the right of each commit.
What information can you gather/explore from these buttons?
How would you get that same information in the shell?
10) Push versus commit#
Explain in one or two sentences how git push
is different from git commit
.
11) License and README files#
When we initialized our remote zipf
GitHub repo, we didn’t add a README.md
or license file.
If we had, what would have happened when we tried to link our local and remote repositories?
12) Recovering older versions of a file#
Amira made changes this morning to a shell script called data_cruncher.sh
that she has been working on for weeks.
Her changes broke the script, and she has now spent an hour trying to get it back in working order.
Luckily, she has been keeping track of her project’s versions using Git.
Which of the commands below can she use to recover the last committed version of her script?
$ git checkout HEAD
$ git checkout HEAD data_cruncher.sh
$ git checkout HEAD~1 data_cruncher.sh
$ git checkout <unique ID of last commit> data_cruncher.sh
$ git restore data_cruncher.sh
$ git restore HEAD
13) Understanding git diff
#
Using your zipf
project directory:
What would the command
git diff HEAD~9 bin/plotcounts.py
do if we run it?What does it actually do?
What does
git diff HEAD bin/plotcounts.py
do?
14) Getting rid of staged changes#
git checkout
can be used to restore a previous commit when unstaged changes have been made,
but will it also work for changes that have been staged but not committed?
To find out,
use your zipf
project directory to:
Change
bin/plotcounts.py
.Use
git add
on those changes tobin/plotcounts.py
.Use
git checkout
to see if you can remove your change.
Does it work?
15) Figuring out who did what#
We never committed the last edit that removes the calculation of inverse rank.
Remove this line from plotcounts.py
,
then commit the change:
df['inverse_rank'] = 1 / df['rank']
Run the command git blame bin/plotcounts.py
.
What does each line of the output show?