0

I have a serious problem.

Basically my last commit to a project was a month ago and I needed to make a new one today - since then a documentation change has been made on the remote repo. I am relatively new to git and had issues trying to push my commits last time because of a change on the remote and so I thought I should pull these changes first and then push my local changes however un doing this pull my local repo, as well as my entire visual studio project has been reverted back to how it was a month ago - thats a lot of work lost. As far as I can see there is no local trace of the previous file changes

Somebody please tell me there's a way to undo this!

Matthew Swallow
  • 179
  • 1
  • 11
  • Does this answer your question? [Browse orphaned commits in Git](https://stackoverflow.com/questions/2092810/browse-orphaned-commits-in-git) – jamesdlin Mar 31 '23 at 15:33
  • @jamesdlin unfortunately not, since the last push to remote there were no commits made on the local repo, just 1 month worth of edits. Now today when I ran the pull request it reverted my entire project's files back to the state it was at the last push instead of merging the two based on the recent'ness of the changes like I expected it to – Matthew Swallow Mar 31 '23 at 15:43
  • Pulling and rebasing should have tried to preserve your uncommitted edits and given you warnings. If you continued without ever committing anything, then there isn't anything Git can do to help you, and you're at the mercy of filesystem recovery tools. In the future, make frequent commits. You can always squash or remove them later. – jamesdlin Mar 31 '23 at 16:05

1 Answers1

1

There's usually always a way to do undo something like in this git. Without really knowing your commit history/tree it's hard to give an exact list of commands to get you back the point where you were before, but generally:

Use git log to find the commit hash of the latest "safe" commit

git checkout <safe commit hash>

git reset --hard

  • This will not work for me as there were no local commits. Doing the git pull has reversed my whole VS project to the state it was at the last push - there have been no commits since then, just a month worth of editing that's all been deleted by git in an instant – Matthew Swallow Mar 31 '23 at 15:37
  • Something's not lining up with your explanation, if there were no local commits, and the remote commit history differed from your local commit history, then doing a `git pull --rebase` or a `git pull` should have been an additive change, not a subtractive change. In your original statement, you said "my last commit to a project was a month ago" so did you have local commits or not? – Vyacheslav Gorbov Mar 31 '23 at 15:48
  • - Last Commit and push to remote was 1 month ago - Changes have been happening all month with no local commits - The other day I changed some of the Issue Templates leading to Github making a commit on the remote – Matthew Swallow Mar 31 '23 at 16:08
  • @MatthewSwallow You should have received the following message from git if you had saved changes WITHOUT commits locally: "error: Your local changes to the following files would be overwritten by merge: test.txt Please commit your changes or stash them before you merge. Aborting" – Vyacheslav Gorbov Apr 03 '23 at 23:19
  • I just tested the above on the following repo: https://github.com/vyacheslav31/test1 Added changes remotely, made another copy locally, added different changes from remote and tried to do a git pull. Receive the aforementioned error message. Did you simply ignore it, or did you receive a different warning? – Vyacheslav Gorbov Apr 03 '23 at 23:21
  • possibly missed the message as I've been using the git UI through Visual Studio – Matthew Swallow Apr 04 '23 at 16:46