5.1 Homework
Getting to grips with different undo options
This practical homework session will walk you through different “undo” options that you can use in git.
There’s a reason we only did a short version of this during the live session: there are lots and lots of diferent options.
The point of this homework is for you to experience the different options available, so that you’re aware they exist, and can successfully search for help online when you might need to use one of them!
1. Create a new repository with a readme file and open it “locally” (either clone it, or open it in codespaces).
- See Practical 1 for guidance on creating a repository and opening it with Codespaces.
- See the GitHub documentation on cloning a repository for more detail if using git locally.
2. Edit your readme file
Copy and paste the below content into your README.md file:
# Testing `git` undo options is so much fun!
This is a very important project being carried out by [NAME] to investigate the different possibilities of git and the various way to "undo" different changes.
It's a good idea to use throwaway testing repositories like this to practice and get used to different commands, so that you don't accidentally lose your important research work.You can add in your name if you like!
3. Create a new file
Create a new file called “secrets_of_the_universe.txt”, and copy and paste the below into it:
The answer is 42
4. Create another new file
Create a new file called lorem_ipsum.txt and copy and paste the below into it:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
5. Complete the git cycle
All of this is on the main branch.
- Use
git statusto see the current state of your files. - Add your changes:
git add README.md secrets_of_the_universe.txt lorem_ipsum.txt - Use
git statusto see the current state of your files. - Commit your changes:
git commit -m "Set up repository and add first files" - Use
git statusto see the current state of your files. - Use
git pushto push your changes to the remote repository.
6. Make some terrible edits
Open up lorem_ipsum.txt and replace the text with this:
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
Save the file, but don’t add or commit!
7. Undo unstaged edits
Run a git status. You should have output something like this:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: lorem_ipsum.txt
no changes added to commit (use "git add" and/or "git commit -a")You can now use git restore filename to throw away the changes since the last commit:
git restore lorem_ipsum.txtThis permanently deletes your changes!
Now check the contents of lorem_ipsum.txt, and the output of git status.
7. Make more terrible edits
- Delete
secrets_of_the_universe.txtandlorem_ipsum.txt - Run
git status; both files should show “deleted”. - Add your edits - this time, using
git add . - Run
git status: what did this do?
Why might the command git add . cause issues?
8. Undo staged edits
- Run
git statusto check on the state of your files - To restore the files:
git restore --staged secrets_of_the_universe.txt lorem_ipsum.txt - Run
git status: what did this do? - Run
git restore secrets_of_the_universe.txt lorem_ipsum.txt
git restore --staged filename.txtwill just undo thegit addcommand, but keep your changesgit restore filename.txtwill undo edits to unstaged files
9. Make even more terrible edits
- Delete
secrets_of_the_universe.txtandlorem_ipsum.txtor replace their content with random letters. - Run
git statusto check on the state of your files - Add your edits:
git add secrets_of_the_universe.txt lorem_ipsum.txt - Commit your edits:
git commit -m "Refining the secrets of the universe" - Push your edits:
git push origin main/git push
10. Undo your last commit!
We have two options here:
- Undo the commit, but keep changes in the working directory so we can have a look to make sure there’s nothing we want to keep - a soft reset
git reset --soft HEAD~1
- Undo the commit and delete any changes since the last commit - a hard reset
git reset --hard HEAD~1
Choose which you want to do!
Use git status before and after, and look to see how your remote repository is.
Hard reset permanently deletes your work!
11. After the last undo, add a new file!
Create a file called changes.md, and fill it with text:
Ch-ch-ch-ch-changes
Turn and face the strange
Ch-ch-changes
Don't want to be a richer man
Ch-ch-ch-ch-changes
Turn and face the strange
Ch-ch-changes
There's gonna have to be a different man
Time may change me
But I can't trace time
Do your usual git cycle: add, commit, and use the status command to check on the progress.
12. Try to push your new changes…
If you run git push origin main you will run into an error:
error: failed to push some refs to 'github.com:murphyqm/teaching_prototyping.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.You have two options:
- “Pull” the changes from the remote to integrate them
- “Force” our changes to override the remote changes
- This is what we’re going to do this time!
git push -f origin main
What does the remote repository look like now?
13. Scratch all of it, I want to go back to a specific point!
Let’s use the command git log!
git log --oneline- specifying one line logs makes it easier to read.- This provides you with a list of commits, and their specific unique hashes/IDs, from most recent to oldest:
2e7f4b6 (HEAD -> main, origin/main) Add in lyrics
94941b5 Add new file
bf351e5 Set up repository and add first files14. Single commit undo with revert
Example:
2e7f4b6 (HEAD -> main, origin/main) Add in lyrics
94941b5 Add new file
bf351e5 Set up repository and add first files- Use
git log --onelineto get a list of commit IDs as above. - Use
git revert <commit-hash>to undo that commit, e.g.git revert 94941b5 - You’ll be asked to create a commit message for this reversion
- Use
git log --onelineagain
33e5056 (HEAD -> main) Revert "Add new file"
2e7f4b6 (origin/main) Add in lyrics
94941b5 Add new file
bf351e5 Set up repository and add first filesHow has this modified your files?
15. Go back in time with reset
Example:
I return the following from git log --oneline:
33e5056 (HEAD -> main) Revert "Add new file"
2e7f4b6 (origin/main) Add in lyrics
94941b5 Add new file
bf351e5 Set up repository and add first filesI want to return the entire state of the repository to 94941b5 Add new file
I run git reset 94941b5
What is going to happen?