skip to Main Content

I have been using Git version control system in cooperation with the VSCode 1.51.0. I have following scenario:

  1. I have made several changes and I have created a separate commit for each of them (but I haven’t pushed none of the commits)
  2. I have accidentally invoked the git checkout command and I have switched to another branch.

After that it seems to me that all my unpushed commits have been lost. Please can you tell me whether there is any method to restore them somehow? Thanks in advance for any ideas.

2

Answers


  1. You can try using git stash pop!

    Login or Signup to reply.
  2. Don’t panic, nothing is lost.

    Unlike some version control systems, git is designed to be completely decentralised – pushing to a remote server is completely optional. Your local working copy is a complete clone of the repository on its own, and can be used entirely without any internet access, or even without having a copy anywhere else. Switching branch without pushing is entirely normal and nothing to worry about.

    When you made your new copies, you had a particular local branch checked out, and you added your commits to that branch. All you need to know is the name of that branch, and you will see that all the commits are still there: git log my-branch-name will show them, git checkout my-branch-name will check out the branch including them, and you can push them up to the remote server as normal.

    In the unlikely case that you don’t know the branch name, the commits will still exist somewhere. See How can I recover a lost commit?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search