Reachability and undoing commits

Now let's imagine this scenario: we have a new commit on the berries branch, but we realized it is a wrong one, so we want the berries branch to go back where master is. We actually want to discard the last commit on the berries branch.

First, check out the berries branch:

[23] ~/grocery (master)
$ git checkout -
Switched to branch 'berries'

New trick: using the dash (-), you actually are saying to Git: "Move me to the branch I was before switching"; and Git obeys, moving us to the berries branch.

Now a new command, git reset (please don't care about the --hard option for now):

[24] ~/grocery (berries)
$ git reset --hard master
HEAD is now at 0e8b5cf Add an orange

In Git, this is simple as this. The git reset ...

Get Git Essentials - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.