Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab4: Time Travel

IUT d'Orsay, Université Paris-Saclay

Objectives

The goal of this lab is to understand the following commands:

A quick review

Time travel

We learned that Git uses a commit graph to represent the different versions of our project over time. Now, we are going to learn how to navigate through this graph.

Checkout

We can navigate through the different nodes of this graph, which means we can go back to different states of our repository in the past, saved by commits. Before you start, make sure your repository is clean (all changes are in a commit).

  1. Run git checkout <commit hash> using the commit hash of an old commit. Your command could look like git checkout abc0123, for example.

  1. Observe the state of your (old) repo.

  2. Go back to the most recent node (and leave the detached HEAD state) by running git checkout main.

  1. You can also use git diff <commit hash> to see the differences between your current working tree and the state of your repo after an old commit.

  2. Travel through your commit graph and observe the state of your repo (with checkout, diff, status, and graph), then come back to the present once you have mastered this time travel.

Stash

To prepare for our time travel, we are going to learn how to keep the current changes from the present that are not yet in our commit graph, because going back to the past will change the reality observed in our working tree.

  1. Before you start, check the state of your commit graph with git graph.

  2. Create a Lab4/ directory with a time-travel.txt file containing whatever content you want.

  3. Run git add and git commit to record these changes.

  4. Use git graph to confirm that a new node has been added with your commit message.

  5. Modify the contents of time-travel.txt without running git add or git commit.

  6. Run git stash and observe the contents of this file.

  1. Run git stash pop and observe the contents of time-travel.txt.

  1. Create three files modified-time-travel.txt, staged-time-travel.txt, and ignored-time-travel (without an extension) containing whatever content you want.

  2. Add, commit, push, and verify that the changes are on the remote repo, except for ignored-time-travel, which is ignored (if your .gitignore is correctly configured to ignore files without an extension; otherwise, fix your .gitignore, clean up your remote repo, and start again from step 8).

  3. Create a new file untracked-time-travel.txt in Lab4/ with the content that you want without running git add.

  4. Modify the contents of modified-time-travel.txt without running git add.

  5. Modify the contents of staged-time-travel.txt and run git add staged-time-travel.txt without running git commit.

  6. Modify the contents of ignored-time-travel.

At this point, make sure you have a new untracked file (untracked-time-travel.txt), a modified file that was already tracked (modified-time-travel.txt), a modified file that is staged (staged-time-travel.txt), and a modified but ignored file (ignored-time-travel).

  1. Run git stash and observe the contents of the four files. Which changes disappeared, and which ones stayed?

  2. Run git stash pop to restore all your changes.

  3. Run git stash -u and observe the contents of the four files. Which changes disappeared, and which ones stayed?

  4. Run git stash pop to restore all your changes.

  5. This time, run git stash -a and observe the contents of the four files. Which changes disappeared, and which ones stayed?

  6. Run git stash pop to restore all your changes.

Now, you should know which changes are stored in the git stash stack.

  1. Run git checkout <old commit hash> and observe the error message.

  1. Add, commit, and push all your changes.

Reset

Le voyage dans le temps nous rend parfois nostalgiques d’un meilleur passé.
Malheureusement, nous ne pouvons que l’observer avec une tête détachée.
Si seulement nous pouvions changer la réalité et réinitialiser...

  1. Create a reset.txt file containing a line of your choice, then add and commit without pushing.

  2. Add a second line to reset.txt, then add and commit without pushing.

  3. Check git graph and git status.

  4. Run git reset HEAD~1 to remove the last commit.

  5. Check git graph, git status, and the contents of reset.txt.

  1. Add and commit without pushing your changes once again. Then make changes to reset.txt on the following lines, making a different commit without pushing for each line.

Now, you should have several consecutive commits that modify the contents of reset.txt.

  1. Choose a commit in the past (after the creation of reset.txt) and run git reset <commit hash>. Then check git graph and git status.

  1. Create a few more commits by modifying reset.txt without pushing.

  2. Run git reset --soft with HEAD~<number> or <commit hash> and check git graph and git status. What is the difference between --soft and the default behavior of reset?

  3. Create commits again without pushing.

  4. Run git reset --hard with HEAD~<number> or <commit hash> and check git graph and git status. What is the difference?

We insist that commits and deletions must be done locally before being pushed to the remote repo. Why?

Does that mean a synchronized commit can never be “undone”? We are going to see how to undo a commit that is already on the remote repo.

  1. Add, commit, and push all your changes.

Revert

Et je croyais pouvoir modifier la réalité...
Alors que ce n’est que ma réalité...
Comment puis-je annuler mes commits erronés ?
Si je les ai déjà partagés avec le monde entier.

  1. Create a revert.txt file with one line of text and make a commit with push.

  2. Add a second line to revert.txt and make a commit with push.

Let’s imagine that the second line added is a mistake. We are going to revert this commit while keeping the history.

  1. Run git revert HEAD. Enter a message in the editor, just like for a commit message.

  2. Check the contents of revert.txt, git status, and git graph.

  1. Experiment with a new commit and git revert HEAD --no-edit, then check the contents of revert.txt, git status, and git graph.

  1. Make at least three changes in revert.txt with add, commit, and push for each line separately.

We can also revert multiple commits. Each undone commit will be accompanied by a revert commit.

  1. Run git revert HEAD~3..HEAD --no-edit to revert the last three commits. Check the contents of revert.txt, git status, and git graph.

  1. Experiment with multiple commits, then git revert -n HEAD~3..HEAD and git commit -m "<message>", while checking the contents of revert.txt, git status, and git graph.

Since you are not deleting any commits but creating new commits that “undo” old ones, there is no risk to the remote repo history.

  1. Make sure your repository is clean. Go back to the objectives and check off the points you have mastered. Practice the commands and concepts you have not yet fully understood. Ask your instructor for help if needed.