enchance
enchance

Reputation: 30521

Clarifications when using git to go back a few commits

Can anyone clarify some things things for me. If I go back a previous commit using

git checkout HASH
  1. Running git branch shows (no branch). What does this mean?
  2. If I edit a file then commit. Will it create a new branch or will it simply update the entire branch so when I go back to the current commit the file will also be updated?
  3. Related to #2, is the purpose of checking out a previous commit so you can make a branch out of it or something more? I'm guessing something more but I can't see it since my project isn't all that complex.

Upvotes: 2

Views: 241

Answers (2)

jmlane
jmlane

Reputation: 2198

  1. It means you have a detached HEAD ref pointing at the commit you checked out. This means any commits going ahead will not be associated with a branch and will only be accessible by commit SHA once you move HEAD off the detached tree.

  2. No branch is created or updated. You will create a commit tree that starts at your checked out commit, however it isn't part of any branch until you tell git to make this tree part of a branch.

  3. It is intended to do manipulations on that commit that you may not wish to make a part of any branch in your repository. This could be for throw-away work. You could always make a new branch from a previous commit should you wish to do so.

git-checkout man page provides a good explanation of these issues and would clarify your understanding of checkout being used in these various ways.

Upvotes: 1

VonC
VonC

Reputation: 1329672

  1. It means you are in a DETACHED HEAD mode
  2. No, your commit isn't referenced by any branch: no HEAD of any branch referenced your new commit (hence the "detached head" part).
    See also "HEAD and ORIG_HEAD in Git"
  3. git checkout -b aNewBranch is enough to create a new branch, which will reference your current commit (which will no longer be a "detached HEAD").

enter image description here

Upvotes: 0

Related Questions