0

Without path of files, what exactly does

git checkout commit-sha

do ?

Anyone can explain it to me?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Statham
  • 4,000
  • 2
  • 32
  • 45
  • 2
    It writes the tree from commit-sha to the working directory and sets HEAD to commit-sha – William Pursell Oct 17 '19 at 16:48
  • but when i change my files in working dir, git will refuse to checkout sometimes – Statham Oct 17 '19 at 16:50
  • 1
    When it refuses, it warns you that it would otherwise overwrite files in the working dir, and suggests that you commit or stash them. That is appropriate behavior. – William Pursell Oct 17 '19 at 16:56
  • Some says that it will not change the local change, some says that it will change all fils into that particular commit – Statham Oct 17 '19 at 17:07
  • I found that if there are local changes, when it is the last commit id, the checkout will be granted, otherwise will be refused. when there are no local changes, it will always be granted. – Statham Oct 17 '19 at 17:20

1 Answers1

3

Git will attempt to check out the given commit, as what Git calls a detached HEAD.

As with any git checkout, the operation may succeed, or may fail. The main reason for a failure is an attempt to check out some other commit when you have uncommitted work.

Checking out some other commit / other branch when you do have uncommitted work sometimes succeeds. The explanation for this apparent discrepancy is in Checkout another branch when there are uncommitted changes on the current branch.

If the checkout succeeds, you have a detached HEAD, and your index and work-tree have been filled from the target commit. As noted in the linked question and answer, if you have uncommitted changes, they have been preserved: in this case at least some index and work-tree files do not match the selected commit.

torek
  • 448,244
  • 59
  • 642
  • 775