Search code examples
gitgithubbranchcommitgit-remote

Git checkout erased newest commit


Recently I made a ton of changes to my code and committed using the following commands.

git add .
git commit -m "example message"
git push origin development

When it didn't work, it said it was because it couldn't find a branch named development. I though this was because it didn't have the right credentials, so I did gh auth login and re-authenticated. I then realized I was still pushing to the main branch, and I so I switched to development using the following.

git checkout development

I'm under the impression that this is the correct command to switch branches, but I'm not sure. The problem is that somehow, after I repeated the first code block, all the changes I made to my code disappeared. Why did everything just revert back to an old version and how can I get the new code back?

Although I am very inexperienced in Git, I'm pretty that since I didn't actually delete any files, there should be a backup somewhere, I'm just not sure how to access it. Writing all my code again would extremely annoying.

I already tried to do the following command

git log --oneline

And it gave me this (messages changed).

3e3caf2 (HEAD -> development, origin/development) backend function to get data
1fcd9d4 added match
66b9527 (origin/dev, dev) removed cache
14b5cf7 initial commit
7e7a826 initial commit

Even though I committed my changes, the newest commit only shows the branch my code was reverted to. What can I do to get my newest commit back?


Solution

  • As far as I understand your question, and as topsail mentioned in his answer, your changes should be present on the main branch

    The way you can check on it is by running

    git log --oneline main
    

    And you would get the commit hash from that commit you added on main branch, you can then just run a cherry-pick of that commit hash to your development branch. So, you would do this command while you're checked out on the development branch:

    git cherry-pick {commit_hash}
    

    And then, push the development branch as you intended.