Skip to main content

View History and Continue Development from Old Commits

When to Use

When you want to temporarily view old code, compare historical commits, or create a new branch from an old commit to continue development, this set of commands is the best fit.

Check Current State First

git status
git log --oneline --decorate -10
  • Make sure the working directory is clean first to avoid being blocked by local changes when switching commits.
  • Use git log --oneline --decorate -10 to confirm the commit hash you want to view.
ScenarioRecommended CommandResult
View a historical commit without creating a branchgit switch --detach <commit>Enter detached HEAD state
Create a new branch from an old commitgit switch -c <new-branch> <commit>Continue development at the specified commit
View a single file from an old commit without switchinggit show <commit>:path/to/fileDirectly output the file content at that version
Return to the previous branchgit switch -Go back to the last branch

Common Scenarios

Just Want to Look at Old Code

git switch --detach a1b2c3d

Go back when done:

git switch -

Create a Hotfix Branch from a Historical Commit

git switch -c hotfix/from-old a1b2c3d

View an Old File Without Moving HEAD

git show a1b2c3d:path/to/file

Compare Differences Between Two Versions

git diff old-commit..new-commit

Legacy Syntax

Older tutorials often write:

git checkout <commit>
git checkout -b <new-branch> <commit>

These still work, but newer Git versions recommend separating "switch branch" from "restore file/view version" semantics, preferring git switch and git restore.

Risks and Boundaries

  • git switch --detach <commit> enters detached HEAD state -- new commits won't automatically attach to a regular branch.
  • If you continue committing in detached HEAD state, remember to use git switch -c <branch> to attach it to a new branch as soon as possible.
  • These operations are for viewing and switching only -- they don't rewrite existing history.
  • If your working directory isn't clean, consider committing, stashing, or using stash before switching to an old commit.
git log --oneline --decorate -10
git switch --detach <commit>
git switch -c <new-branch> <commit>
git show <commit>:path/to/file