How to switch to a historical branch in Git to view code, then switch back

1. Viewing Historical Version Code

For example, if you want to see what the project looked like at this commit:

git switch --detach bb60a7e1fdb128316ec9cd0c9e50165fa0948d25  

At this point, your files will revert to the state of that historical version.


2. Temporarily Testing an Older Version

Suppose you suspect a bug was introduced later—you can switch to the older version and run the project:

git switch --detach bb60a7e1fdb128316ec9cd0c9e50165fa0948d25  
npm run dev  

Then check whether the issue exists in the older version.


3. Creating a New Branch from an Older Version

If you find that this older version is exactly what you need, you can create a new branch based on it:

git switch -c fix-from-old-version  

This creates a regular, named branch starting from that commit.


Key Point: What Are the Risks of Detached HEAD?

While in detached HEAD state, you can modify code and even make commits:

git add .  
git commit -m "test change"  

However, this commit does not belong to any branch.

If you later switch directly to another branch:

git switch master  

That commit may become difficult to locate—and could even be garbage-collected by Git in the future.

Therefore, if you’ve made valuable changes while in detached HEAD state, immediately create a branch to preserve them:

git switch -c my-new-branch  

How to Return to Your Original Branch

For example, to return to master:

git switch master  

Or to return to main:

git switch main  

To view your current status:

git status  

If you see output like:

HEAD detached at bb60a7e1  

It means you’re currently in detached HEAD state.


One-Sentence Summary

git switch --detach <commit-id>  

means:

Temporarily switch your working directory to a specific historical commit—for inspection, testing, or creating a new branch from that point—but note that this is not a normal branch state, and commits made here risk being lost unless explicitly saved to a branch.