ETC5513: Collaborative and Reproducible Practices
Workshop 7
🔧 Workshop Goals
By the end of this workshop, you’ll be able to:
- Undo commits safely (
revert
,reset
) - Work with old commits using detached HEAD
- Recover lost commits using
reflog
- Modify your last commit with
--amend
- Clean up tracked and untracked files (
git rm
,git clean
)
🚧 Setup
Go to https://github.com and create a new repository.
Name it something likegit-fix-workshop
.
✅ Check Add a README fileClick the green Code button and copy the SSH URL.
In RStudio, open a new RStudio Project:
- Go to File → New Project → Version Control → Git
- Paste in the SSH URL
- Choose a location and click Create Project
Open the Terminal tab in RStudio (or use the Git pane).
You now have a GitHub-connected repo ready to go.Create a new file called
model.R
. Add a few lines of R code that load a library, and draw a plot.
1️⃣ Revert vs Reset
Step 1: Make Three Commits
Add one line at a time and commit after each:
Line 1
Line 2
Line 3
You should now have 3 commits. Check with:
git log --oneline
Step 2: Revert the Most Recent Commit
Let’s safely undo the last thing you committed — no risk of conflicts here.
Run:
git log --oneline
The most recent commit should be the one that added Line 3.
Now run:
git revert HEAD
✅ Git will create a new commit that undoes the last one (removing Line 3).
Check your file and run:
git log --oneline
You should see a new commit at the top with a message like:
Revert "Add line 3"
That’s how you safely undo a recent commit using revert
.
Step 3: Reset the Last Commit
Run:
git reset --soft HEAD~1
Now run:
git status
What do you see? Try committing again if you want.
2️⃣ Detached HEAD and Rescue
Step 1: Checkout an Old Commit
From your log, copy the hash for the first commit.
git checkout <commit-hash>
You’re now in a detached HEAD state.
Try editing model.R
and committing:
git add model.R
git commit -m "Edit in detached HEAD"
Step 2: Create a Branch to Save Your Work
git switch -c experiment
You’re now safe! Run git log
and note the history.
3️⃣ Amend the Last Commit
- Make a new commit.
- Realize you forgot to add a second file.
- Create and add another file (e.g.,
forgotten.txt
) - Run:
git add forgotten.txt
git commit --amend
Did the commit message or hash change?
4️⃣ Reflog to the Rescue
Step 1: Make and Reset a Commit
- Make a quick commit (e.g., change
notes.txt
) - Run:
git reset --hard HEAD~1
The commit is now gone… or is it?
Step 2: Use reflog
git reflog
Find the lost commit hash and restore it:
git checkout <hash>
git switch -c recovered-work
✅ Your work is back!
5️⃣ Cleaning and Removing
Step 1: Track and Remove a File
- Create a file (e.g.,
temp.txt
) - Add and commit it
- Now remove it using:
git rm temp.txt
git commit -m "Remove temp file"
Check git log
— what happened?
Step 2: Remove a File but Keep It Locally
- Create and commit another file (e.g.,
data.csv
) - Run:
git rm --cached data.csv
git commit -m "Stop tracking data"
- The file still exists in your project, but is no longer tracked.
Step 3: Clean Untracked Files
- Create a few files but don’t add them to Git.
- Preview what would be deleted:
git clean -n
- If you’re sure:
git clean -f
✅ These files are now gone — permanently.
✅ Summary Questions
- When would you use
revert
instead ofreset
? - What does
reflog
help you recover? - What’s the difference between
git rm
andgit clean
? - How would you safely stop tracking a file that’s already in the repo?
🎉 Great work! You’ve now practiced some of Git’s most powerful (and dangerous!) commands — safely.