-1

I just tried to push my code to GitHub and it reset my code to the first commit:

enter image description here

It even shows the current date. I have no idea what happened, I just pushed my local repo to GitHub.

Git Reflog

1bfaa56 (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from 1bfaa5611e682cc88df5a7533f4fea3b2d22a700 to master
1bfaa56 (HEAD -> master, origin/master) HEAD@{1}: checkout: moving from master to master
1bfaa56 (HEAD -> master, origin/master) HEAD@{2}: checkout: moving from a346040b4c6ee040d5247a1f5d9ab57a0842405f to master
a346040 HEAD@{3}: commit: GitHub, lets go
0c05b31 HEAD@{4}: commit: Added formatting
ec361bc HEAD@{5}: commit: PreStorybook Commit
e609f79 HEAD@{6}: commit: Pre Webraptor deck swiper
4d6c52c HEAD@{7}: commit: Pre REdux toolkit reducer
e9542a2 HEAD@{8}: commit: Pre Rdux commit
2e9f395 HEAD@{9}: commit: Pre Amplify commit
9f29127 HEAD@{10}: commit: Detox added
741e11a HEAD@{11}: commit: Pre Detox Setup commit
1bfaa56 (HEAD -> master, origin/master) HEAD@{12}: checkout: moving from master to 1bfaa5611e682cc88df5a7533f4fea3b2d22a700
1bfaa56 (HEAD -> master, origin/master) HEAD@{13}: commit (initial): First Commit
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Fredyonge
  • 300
  • 1
  • 4
  • 17
  • 1
    What exact commands did you execute? What do you mean by "reset"? Pushing will not change the state of the current branch or any commits. What did you see before you pushed? – Code-Apprentice Apr 16 '21 at 19:17
  • 1
    I assume the picture is of your local repository, not your GitHub repo, correct? Do you have any branches other than `master`? What is the output of `git reflog`? If you had local commits, this output will help us restore your work. – Code-Apprentice Apr 16 '21 at 19:19
  • It resettet everything to the first commit. I used the github desktop app for macOS, createnewRepository and then my path name and the online name – Fredyonge Apr 16 '21 at 19:20
  • 1
    Did you commit any of your work to this repo after you created it? What files are in the directory? Do you see your work in the files? – Code-Apprentice Apr 16 '21 at 19:22
  • Hey , i posted the reflog: "Github, le'ts go" was the last commit, that sounds hopefull, that it shows. I can't see anything of my work, after my first commit – Fredyonge Apr 16 '21 at 19:24
  • Ahhh!! I checked out to a346040 my latest commit and I can see my code again!!! Thanks so much!!!!! – Fredyonge Apr 16 '21 at 19:26
  • Just checking out the commit is not enough. You risk losing your work again if you don't have a branch. See my answer below and read more about "headless mode" in git. – Code-Apprentice Apr 16 '21 at 19:30

1 Answers1

1
1bfaa56 (HEAD -> master, origin/master) HEAD@{12}: checkout: moving from master to 1bfaa5611e682cc88df5a7533f4fea3b2d22a700

This message means that you checked out a commit directly by its hash. This puts you in so-called "detached head" state. You should have seen a warning message about this that tells you all of the caveats about detached head. In short, don't commit anything while you are in detached head state because those commits are not on any branch.

With that said, you can still get your work back. This is the wonderful thing about git: almost everything you do is reversable. Let's look at some other messages:

1bfaa56 (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from 1bfaa5611e682cc88df5a7533f4fea3b2d22a700 to master
1bfaa56 (HEAD -> master, origin/master) HEAD@{1}: checkout: moving from master to master
1bfaa56 (HEAD -> master, origin/master) HEAD@{2}: checkout: moving from a346040b4c6ee040d5247a1f5d9ab57a0842405f to master
a346040 HEAD@{3}: commit: GitHub, lets go

The final line here looks like the most recent commit you made for your work. To get back to that point, you can use git merge:

git merge HEAD@{3}

This assumes you haven't executed any other commands since the last time you ran git reflog. If you have, you should do git reflog and use the correct number inside the {}.

Alternatively, you can use the commit hash:

git merge a346040

Each commit has a unique identifier and which can be used in many git commands.

Finally, push your commits to GitHub:

git push
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    @Fredyonge Note that you can probably also use `git merge a346040` which is safer than `git reset`. – Code-Apprentice Apr 16 '21 at 19:31
  • I used git checkout a346040, prior, is this fine aswell? – Fredyonge Apr 16 '21 at 19:32
  • 1
    @Fredyonge No, that puts you back in the same situation you were in originally that caused you to lose your work in the first place. Carefully read the warning afer you do `git checkout a346040` for a detailed explanation of why. – Code-Apprentice Apr 16 '21 at 19:32
  • 1
    @Fredyonge To clarify, it wasn't `git push` that made your work disappear, exactly. It looks like whatever you did in GitHub executed `git checkout master` before doing `git push`. Since you previously had done `git checkout 1bfaa5611`, the work was not on `master` or any branch for that mater, so a subsequent `git checkout master` lost the commits you made. – Code-Apprentice Apr 16 '21 at 19:35
  • So having done git checkout a346040, what is the correct way to get everything back into shape now? – Fredyonge Apr 16 '21 at 19:38
  • 1
    @Fredyonge `git checkout master` then `git merge a346040`. Alternatively `git merge HEAD@{##}` where you find the correct number in place of `##` from `git reflog`. – Code-Apprentice Apr 16 '21 at 19:39
  • 1
    I pushed again and everything works, thank you so much! It was one of the higher heart rates I have had at this time of the day (10pm in germany). – Fredyonge Apr 16 '21 at 19:45
  • 1
    @Fredyonge Yup, I've had those moments. Git is wonderful because it provides this kind of safety net. To learn more about git, I suggest that you read the first 3 chapters of [Pro Git](https://git-scm.com/book/en/v2). – Code-Apprentice Apr 16 '21 at 19:47