1

I created my code on local computer.

Once I pushed the code to git server.

git add -A
git commit -m "test"
git push -u ...

After that on the same branch I made lot of changes, and than did commit

git add -A
git commit -m "test"

The commit succeeded, but I instead of push I did

git checkout master

Then I realized I didn't push the code, so I did

git checkout mybranch

I realized again that the checkout I did was from the git server, and I didn't see code of mine, even I did commit.

I know that commit is kept on my local computer, but is my latest changes gone?

How can I restore the latest changes of mine?

Thank you in advanced.

Eitan
  • 1,286
  • 2
  • 16
  • 55
  • Committing does not lose any changes. If your working directory looks different after switching back to your feature branch, it might instead imply that you somehow didn't commit every file, and that those files got wiped out when switching branches. – Tim Biegeleisen Apr 09 '21 at 07:04

1 Answers1

1

You should be able to git switch (better than git checkout) your branch and see your code.

But if not, check git reflog, and look for a recent commit SHA: try and git switch --detach <SHA> to inspect that commit, making sure your changes are there.

The OP Eitan reports in the comments having succeeded, using git log --pretty:

I found the date+time, and do checkout.

 git reflog --pretty=format:"%H %an %ad"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you very much . I did already checkout and not switch ... Hope it is not a dengarous thing to do. I don't want my code will gone. So after the commands you advised, I can do push, or should I run also other git commands before pushing? – Eitan Apr 09 '21 at 07:13
  • @Eitan First, check if your code is there. If it is, reset your branch to it: `git switch -C yourBranch `. Finally, git push. – VonC Apr 09 '21 at 07:16
  • The code isn't there. I will check out your advise after I backup all the folder of project – Eitan Apr 09 '21 at 07:18
  • @Eitan If you have done a git commit, you should see that commit in `git reflog` – VonC Apr 09 '21 at 07:19
  • I am not at work right now. I will be on sunday. Than I figure out and let you know. – Eitan Apr 09 '21 at 07:24
  • No I didn't, unfortunely. I ran also "git log" and the latest commit is more than a month. – Eitan Apr 11 '21 at 05:26
  • I succeed. git reflog-pretty : I found the date+time, and do checkout. https://stackoverflow.com/questions/1441010/the-shortest-possible-output-from-git-log-containing-author-and-date – Eitan Apr 11 '21 at 05:48
  • @Eitan Great! I have included your comment in the answer for more visibility. – VonC Apr 11 '21 at 11:18
  • The full command is git logref pretty=format:"%h%x09%an%x09%ad%x09%s". Should be logref. After that git checkout . That will show the exact command on console how to checkout. – Eitan Apr 11 '21 at 14:55
  • @Eitan Do you mean git reflog? (logref does not exist, unless is is an alias) – VonC Apr 11 '21 at 16:13
  • @Eitan OK but `git reflog pretty` is not an option I am familiar with (I don't see it in https://git-scm.com/docs/git-reflog) – VonC Apr 12 '21 at 17:52
  • That's what I did. git reflog pretty. I can send the exact syntax on Sunday (sorry, again. In Israel there is holiday). There is a link. You may try: https://stackoverflow.com/questions/17369254/is-there-a-way-to-cause-git-reflog-to-show-a-date-alongside-each-entry – Eitan Apr 13 '21 at 16:21
  • Try git reflog --pretty=format:"%H %an %ad". That may work. – Eitan Apr 13 '21 at 16:53