git survival tips: how to undo and recover from your mistakes

20
Git Survival Tips Tobias Günther www.git-tower.com How to Undo & Recover from Your Mistakes

Upload: perforce

Post on 18-Jan-2017

189 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 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

Page 2: Git Survival Tips: How to Undo and Recover from Your Mistakes

Mentioned product names and logos are property of their respective owners.

Page 3: Git Survival Tips: How to Undo and Recover from Your Mistakes

Making changes in a complex software project

Page 4: Git Survival Tips: How to Undo and Recover from Your Mistakes

Undoing Local Changes

Page 5: Git Survival Tips: How to Undo and Recover from Your Mistakes

Undoing Local Changes

$ git checkout HEAD <filename> $ git reset --hard HEAD

Discarding uncommitted local changes cannot be undone!

Page 6: Git Survival Tips: How to Undo and Recover from Your Mistakes

Undoing Committed Changes

fix a typo in your last commitrevert a commit

in the middle restore the project at an earlier version

Page 7: Git Survival Tips: How to Undo and Recover from Your Mistakes

Amending Your Last Commit

Mistyped the commit message…

…and/or forgot to add a change.

Page 8: Git Survival Tips: How to Undo and Recover from Your Mistakes

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).

Page 9: Git Survival Tips: How to Undo and Recover from Your Mistakes

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.

Page 10: Git Survival Tips: How to Undo and Recover from Your Mistakes

Undoing Committed Changes

$ git add forgotten_change.txt $ git commit --amend -m “Correct message”

$ git revert e81fc22

$ git reset --hard e81fc22

Page 11: Git Survival Tips: How to Undo and Recover from Your Mistakes

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 —

Page 12: Git Survival Tips: How to Undo and Recover from Your Mistakes

Recovering “Lost” Commitsgit reflog a journal that logs every

movement of the HEAD pointer

Page 13: Git Survival Tips: How to Undo and Recover from Your Mistakes

Dealing with Merge ConflictsOMG! Help!

Page 14: Git Survival Tips: How to Undo and Recover from Your Mistakes

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 —

Page 15: Git Survival Tips: How to Undo and Recover from Your Mistakes

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

Page 16: Git Survival Tips: How to Undo and Recover from Your Mistakes

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.

Page 17: Git Survival Tips: How to Undo and Recover from Your Mistakes

Dealing with Merge Conflicts

Who made the changes?When?On which branches?In which commits?…

— Understanding What Happened —

Page 18: Git Survival Tips: How to Undo and Recover from Your Mistakes

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

Page 19: Git Survival Tips: How to Undo and Recover from Your Mistakes

Dealing with Merge Conflicts— Wrapping Up —

1. Save2. Stage / Add3. Commit = mark as resolved

(might already be done by external tool)

Page 20: Git Survival Tips: How to Undo and Recover from Your Mistakes

Get in Touch!

Learning Platform: www.git-tower.com/learn/Twitter: @gittowerEmail: [email protected]