O'Reilly logo

Git Essentials - Second Edition by Ferdinando Santacroce

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required