I was working on my Spring Boot app when I accidentally moved the folder that contained the Git repository into another directory. When I tried to load it in Spring Tools Suite (STS), the IDE — due to its workspace cache — treated it as a new project. Not realizing this, I deleted the “new” project from the workspace, and all the changes I had made were gone.
I tried pulling it from the remote repository, but the local folder was missing.
So, I decided to restore the files from an earlier commit in my GitHub repository — before I made the mistake.
This is how I did it!
Step 1: Make sure you’re on the main branch
git checkout master
(or git checkout main depending on your branch name)
Step 2: Create a new branch to restore from old commit
This protects your cu…
I was working on my Spring Boot app when I accidentally moved the folder that contained the Git repository into another directory. When I tried to load it in Spring Tools Suite (STS), the IDE — due to its workspace cache — treated it as a new project. Not realizing this, I deleted the “new” project from the workspace, and all the changes I had made were gone.
I tried pulling it from the remote repository, but the local folder was missing.
So, I decided to restore the files from an earlier commit in my GitHub repository — before I made the mistake.
This is how I did it!
Step 1: Make sure you’re on the main branch
git checkout master
(or git checkout main depending on your branch name)
Step 2: Create a new branch to restore from old commit
This protects your current branch from overwriting.
git checkout -b restore-old-version
Step 3: Restore specific files or folders from a certain commit
Example (restoring only src/ folder):
git restore --source <commit-id> -- src/
This brings the src/ files from that commit into your working directory.
Step 4: Commit the restored files
git add src/
git commit -m "Restore src folder from commit <commit-id>"
Now you’ve captured that state safely in a separate branch.
Step 5: Compare or merge into main
To see what changed before merging:
git diff master
If it looks good:
git checkout master
git merge restore-old-version
Now the restored files are merged into your main branch.
Common Mistakes to Avoid
- Using git checkout directly Puts you in detached HEAD → changes not saved to any branch
- Skipping git add/commit after restore You’ll lose the restored files when switching branches
- Running restore from the wrong branch May restore files to the wrong context or overwrite changes
(Quick Restore without Branch)
If you’re sure you just want to bring back files directly into your current branch:
git restore --source <commit-id> -- src/
git add src/
git commit -m "Restore deleted src folder from <commit-id>"
Simpler, but not reversible as cleanly as using a temporary restore branch.