Posted almost 3 years ago. Visible to the public.

Git basics: checkout vs. reset

Today I got a better understanding of how git works, in particular what git checkout and git reset do.

Basic facts

  • A commit holds a certain state of a directory and a pointer to its antecedent commit.
  • A commit is identified by a so-called ref looking something like 7153617ff70e716e229a823cdd205ebb13fa314d.
  • HEAD is a pointer that is always pointing at the commit you are currently working on. Usually, it is pointing to a branch which is pointing to that commit.
  • Branches are nothing but pointers to commits. You are 'on a branch' when HEAD is pointing to a branch.

checkout

git checkout <commit> <paths> tells git to replace the current state of paths with their state in the given commit.

  • paths can be files or directories.
  • If no branch (or commit hash, see basic facts) is given, git assumes the HEAD commit.
    • –> git checkout <path> restores path from your last commit. It is a 'filesystem-undo'.
  • If no path is given, git moves HEAD to the given commit (thereby changing the commit you're sitting and working on).
    • –> git checkout branch means switching branches.
  • Example: git checkout HEAD~2 app/models/foo.rb drops all modifications of foo.rb and replaces the file with its version two commits ago.

reset

git reset <commit> re-sets the current pointer to the given commit.

  • If you are on a branch (you should usually be), HEAD and this branch are moved to commit.
  • If you are in detached HEAD state, git reset does only move HEAD. To reset a branch, first check it out.
  • Example: You are currently working upon commit 123abc. After resetting to a previous commit xyz789 (e.g. with git reset HEAD~2), you have no easy access to commit 123abc anymore, because HEAD and the branch are both pointing to xyz789.
    To move the branch pointer 'back to the front', you can't use git checkout, as it only moves HEAD. You have to reset your branch to that commit: git reset 123abc. (If you didn't save the first commit's hash (123abc), git reflog will help you finding it.)
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Author of this card:

Avatar
Dominik Schöler
Last edit:
6 months ago
by Pascal Roth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code

License for source code

All source code included in the card Git basics: checkout vs. reset is licensed under the license stated below. This includes both code snippets embedded in the card text and code that is included as a file attachment. Excepted from this license are code snippets that are explicitely marked as citations from another source.

The MIT License (MIT)

Copyright (c) 2012-2015 makandra GmbH

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
Posted by Dominik Schöler to makandropedia