Git Survival Tips: How to Undo and Recover from Your Mistakes
Transcript of Git Survival Tips: How to Undo and Recover from Your Mistakes
Git Survival Tips
Tobias Güntherwww.git-tower.com
How to Undo & Recover from Your Mistakes
Mentioned product names and logos are property of their respective owners.
Making changes in a complex software project
Undoing Local Changes
Undoing Local Changes
$ git checkout HEAD <filename> $ git reset --hard HEAD
Discarding uncommitted local changes cannot be undone!
Undoing Committed Changes
fix a typo in your last commitrevert a commit
in the middle restore the project at an earlier version
Amending Your Last Commit
Mistyped the commit message…
…and/or forgot to add a change.
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) thatreverts the effects of a specified commit (C2).
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.
Undoing Committed Changes
$ git add forgotten_change.txt $ git commit --amend -m “Correct message”
$ git revert e81fc22
$ git reset --hard e81fc22
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 —
Recovering “Lost” Commitsgit reflog a journal that logs every
movement of the HEAD pointer
Dealing with Merge ConflictsOMG! Help!
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 —
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
Dealing with Merge Conflicts— What a Conflict Really Is —
1.Two contradictory versions(“A” and “B”) of a file.
2.…just funny letters in atext file.
Dealing with Merge Conflicts
Who made the changes?When?On which branches?In which commits?…
— Understanding What Happened —
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
Dealing with Merge Conflicts— Wrapping Up —
1. Save2. Stage / Add3. Commit = mark as resolved
(might already be done by external tool)
Get in Touch!
Learning Platform: www.git-tower.com/learn/Twitter: @gittowerEmail: [email protected]