We can’t avoid mistakes altogether, but the right tools can make sure they don't hurt us too much. Git offers many ways to undo, revert, recover and fix your mistakes. In this session, we'll look at the bad things that can happen to you—and how Git can save your neck. If you’d like to gain more confidence in your day-to-day operations when working with code, I’ll show you how to deal with mistakes in real-world scenarios with Git's various “undo features,” using both the command line and the popular GUI, Tower.
5. Undoing Local Changes
$ git checkout HEAD <filename>
$ git reset --hard HEAD
Discarding uncommitted local changes cannot be undone!
6. Undoing Committed Changes
fix a typo in your last commit
revert a commit
in the middle restore the project
at an earlier version
7. Amending Your Last Commit
Mistyped the
commit message…
…and/or forgot to
add a change.
8. Reverting Commits
Modifies “index.html” in the “opposite” way:
(old) <div>About</div>
(new) <div>About This Project</div>
Modified “index.html”:
(old) <div>About This Project</div>
(new) <div>About</div>
Reverting Commit
“git revert” creates a new commit (C4) that
reverts the effects of a specified commit (C2).
9. Resetting HEAD (aka “Rollback”)
master HEAD
master HEAD
Before reset
After reset
“git reset” sets your HEAD pointer (and thereby also your
working copy) to an older revision. Commits that came after
this revision appear to be undone.
11. Recovering “Lost” Commits
master HEAD
master HEAD1. You think you want to reset.
2. You reset.
3. You notice it was a bad idea.
4. You panic.
— An Example —
14. Dealing with Merge Conflicts
don’t panic!
1. Git deals with most of the stuff.
2. You only ever handicap yourself (not your team).
3. You can always undo and start fresh.
— You Cannot Break Anything —
15. Dealing with Merge Conflicts
— How a Conflict Happens —
Modifies line 16 in “about.html”:
(old) <div>About</div>
(new) <div>About Us</div>
Modifies the same line in in the same file:
(old) <div>About</div>
(new) <div>About Tower</div>
contact-form
master HEAD
merge conflict
16. Dealing with Merge Conflicts
— What a Conflict Really Is —
1.Two contradictory versions
(“A” and “B”) of a file.
2.…just funny letters in a
text file.
17. Dealing with Merge Conflicts
Who made the changes?
When?
On which branches?
In which commits?
…
— Understanding What Happened —
18. Dealing with Merge Conflicts
— Solving a Conflict —
(a) Use your editor / IDE to modify the
file by hand.
(b) Use a Merge Tool.
(c) Use Tower / Git in easy cases.
= deciding, how it should finally look
19. Dealing with Merge Conflicts
— Wrapping Up —
1. Save
2. Stage / Add
3. Commit = mark as resolved
(might already be done
by external tool)
20. Get in Touch!
Learning Platform: www.git-tower.com/learn/
Twitter: @gittower
Email: support@git-tower.com
Editor's Notes
Discard all changes in a file [about.html]
…works also with deleted files! [error.html]
Discard chunks [index.html]
Discard lines [index.html]
Discard all changes in Working Copy