0

I have been working on a project using GitHub. I have been working in a local repository (on my desktop, a mac), using the command line to commit changes, pull, push, etc. I had been experimenting with some code in the local copy of my repository for a few days and had not committed anything for a while because I had moved some files to and from different folders in order to test some things. I was finally ready to try to commit all of my changes, so I created a new branch from the terminal. I then switched BACK to my master branch from that branch, and poof my local copy of the repository became the master that shows on the GitHub website (origin, I believe it is called).

DISCLAIMER: this is my first time using GitHub. I guess my problem is I failed to realize how synced up a local copy of a repository is to the cloud. I had modified my local repository quite a bit, moving around files as I mentioned above and creating a Python virtual environment for certain modules. I had assumed that all of these changes were safe on my computer until I pushed them to GitHub. Anyways, I lost a lot of work I had done and really would appreciate anybody who can help me get it back.

3 Answers3

1

You didn’t lose anything that you committed. Nothing in git is ever lost.

You need to think in terms of commits, not in terms of what you can see. The commits are invisible, hidden inside the git repo. Even if you see nothing at all, the commits are there. And every commit contains all your work at the time the commit was made.

So there’s no problem here. If you committed your work on a branch and you wish to see that work, switch to that branch.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You can use this if you have deleted multiple files locally but not committed, you can force checkout and all the deleted files before the last commit will be recovered

       git checkout -f HEAD .
0

There are several ways to restore the last commit. With git log (or with less information git log --oneline) you can see all commits with commit-hash.

The commit-hash (e.g. cdb72rr) is now important:

If you want to return to a previous commit, just do git checkout <commit-hash> . (don‘t forget the . at the end, very important. This will apply changes to the whole tree.), then you will have the state of this commit, and can push this to a new commit.

If you want to revert the last commit just do git revert <commit-hash>, then you can push this new commit, which undid your previous commit. git revert simply creates a new commit that is the opposite of an existing commit.

If you take git reset <commit-hash> you have the same effect like git revert, but git reset erases your git history instead of making a new commit. git revert creates a new commit with the changes that are rolled back. So only use git reset consciously, can remove entire git history!

If you haven't made a commit since deleting, look here Restoring deleted files in Git

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • Unfortunately, those didn't work. I was using Sublime Text to edit files locally in my repository. I made my last commit and THEN did this: in my master branch, I created a file. Then, I switched to a new branch from which I was going to commit that change (I did not not want to commit to my master branch). I executed 'git pull' from THAT branch (I then realized that is pointless if it is not the master). I then switched back to my master before committing anything, but my master had now changed to its previous commit: the one before I had created my new file. I cannot find that file now. – Bennett Kahn May 23 '20 at 05:57
  • You have create *a file* on master, then create a new branch (`git branch new-branch`), `pull` in this branch and then switched back to master and the file was away, right? – SwissCodeMen May 23 '20 at 06:15
  • I actually used the command 'git checkout -b ' to switch to a new branch from my master. Once in that branch, I did a 'pull' (which I do not think it did anything as I got the following message: 'There is no tracking information for the current branch' ). I then switched back to master and the file (and all other edits I had made since last committing in my master) were all gone. Again, I between when I created the file and when I lost it, I unfortunately never committed any changes. – Bennett Kahn May 23 '20 at 06:57
  • then look at this [question](https://stackoverflow.com/questions/1223354/undo-git-pull-how-to-bring-repos-to-old-state). Try `git merge --abort`. – SwissCodeMen May 23 '20 at 07:04