Git+&+Github+Course +Undoing+Stuff
Git+&+Github+Course +Undoing+Stuff
Time Traveling
Checkout
The git checkout command is like a Git Swiss Army knife.
Many developers think it is overloaded, which is what lead to
the addition of the git switch and git restore commands
Remember, you can use the git log command to view ❯git checkout d8194d6
commit hashes. We just need the first 7 digits of a
commit hash.
Master
Master
bugfix
If we switch to the bugfix branch, HEAD is now
pointing at the bugfix reference.
master
bugfix
❯ git switch bugfix
HEAD
This is all to say that HEAD usually refers
to a branch NOT a specific commit. master
bugfix
HEAD
Back to this
Detached HEAD thing
HEAD
Master
HEAD Master
DETACHED HEAD!
Detached HEAD
Don't panic when this happens! It's not a bad thing!
❯ git checkout <commit-hash>
You have a couple options:
1. Stay in detached HEAD to examine the contents of
the old commit. Poke around, view the files, etc.
2. Leave and go back to wherever you were before -
reattach the HEAD
3. Create a new branch and switch to it. You can now
make and save changes, since HEAD is no longer
detached.
If you checkout an old commit and
decide you want to return to where you ❯git checkout d8194d6
were before....
HEAD Master
DETACHED HEAD!
❯git switch master
Master
RE-ATTACHED HEAD!
HEAD Master
Suppose you want to go back to an old
commit and make some new changes
Master
HEAD~3 HEAD~2 HEAD~1
modified about.html
created lisa.jpg
39c3fdd
0026739
bb43f1f
❯ git reset 0026739
created lisa.jpg
0026739
bb43f1f
❯ git reset 0026739
created lisa.jpg
0026739
bb43f1f
Reset --hard
If you want to undo both the commits AND the actual
changes in your files, you can use the --hard option. ❯ git reset --hard <commit>
modified about.html
created lisa.jpg
39c3fdd
0026739
bb43f1f
❯ git reset --hard 0026739
0026739
bb43f1f
git revert
Yet another similar sounding and
confusing command that has to do
with undoing changes.
Git Revert
git revert is similar to git reset in that they both "undo"
changes, but they accomplish it in different ways.
eliminating commits.
Master
❯git reset HEAD~2
HEAD
Master
❯git revert 51494a6
HEAD
Master
51494a6
Master
HEAD
1 2 3 My Local Repo Master
HEAD 1 2 3
Master
1 2 3 4
HEAD
Master
HEAD
1 2 3 Master My Changes
HEAD 1
Master
I use git reset to remove commits that
1 2 3 4 I already shared with my team!
HEAD
Master
HEAD
1 2 3 Master My Changes
HEAD 1
Master
This makes their lives harder. I altered
1 2 3 4 history that they already have. BAD!
HEAD
Master
HEAD
1 2 3 My Local Repo Master
HEAD 1 2 3
Master
Master
1 2 3 My Changes
HEAD
Master
HEAD
1 2 3
Master
I use git revert to reverse the same
1 2 3 4 commits as before, by ADDING a
new commit to the chain
HEAD
Master
1 2 3 My Changes
HEAD
Master
HEAD
1 2 3
Master
My team can merge in the new
1 2 3 4 "undo" commit without issue.
I didn't alter history.