0

So here's an in-depth detail story of my situation timeline regarding a project I'm working on. Bear in mind that everything was done in the same single master branch.

  • Yesterday - Made some commits in the project's remote repository by updating the README file and adding a LICENSE agreement file.
  • Today, at 08:00AM - Decided to work on my project and write some code, didn't pull or fetch anything from the remote repository.
  • 17:34PM - Had 7 commits in my local repository. Decided to push my local commits to the remote repo to call it a day and then in doing so, git warned me that the local and remote repositories had diverged. Because I suck at git, I copied the files I had worked on into another folder (for precaution reasons, didn't want to lose my work) and then typed the following commands to try and solve the problem :
git fetch
git pull
git commit
git reset --hard origin/master
git fetch
git pull

I'm thinking that with all these, the following happened :

  • My local commits went to zero
  • I pulled everything from the remote repository, replacing the work I had done in my local machine
  • Problem seemed to be solved (on the surface at least) because the repositories were in sync again.

I then deleted all the files and pasted in their place, the files with all the code work I had previously copied. The following commands had the purpose of :

  1. Get back the LICENSE file I had deleted
  2. Push everything to the remote repository to finally end this issue
git checkout LICENSE
git add --all
git commit -am "don't know what the hell am I doing"
git push

Now I've just realized that I forgot about the README file. Aside from the LICENSE file, it was the only thing from the remote repository that I actually needed to fetch and I deleted along with all the other files pulled from the remote repository.

Unfortunately, I can't do git checkout README to recover it because I already pushed everything.

Could someone give me some advice on what I'm doing? How can I navigate better with git?

Edric
  • 24,639
  • 13
  • 81
  • 91
Gabriel Pulga
  • 293
  • 1
  • 13
  • https://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide – codemax Aug 07 '20 at 03:14
  • 1
    Provided you haven't run `git gc` (**AND DO NOT RUN `git gc`!!!!!**) you should be able to recover any lost commits by doing a `git checkout ` for each dangling commit. – Dai Aug 07 '20 at 03:14
  • “Could someone give me some advice on what I'm doing? How can I navigate better with git?” Too broad and pretty much meaningless. What’s the actual specific question? – matt Aug 07 '20 at 03:22
  • If you didn't push anything and not committed after messing up, just copy back files to git directory and fetch and merge, then push. If you commited, you still can do fetch and merge and commit with `--amend` option. – Michał Turczyn Aug 07 '20 at 05:22

1 Answers1

0

If you just want to get the README file that was there before :

You added a new commit on top of the previous state of origin/master ; if you want to get the README file from that commit, you can run :

# 'HEAD^' means 'the parent of my current commit' :
git checkout HEAD^ -- README

# or find the sha1 of that previous commit :
# 'git log' on the cli, or any GUI to browse your repo
git log --oneline
# once you know that sha1, you can use it :
git checkout sha1 -- README

One extra thing to know : git has a big undo stack, called the reflog.

If you run git reflog, you will see a history of all the commits you went through, including your 17:34PM.

This history gets reduced when git gc is run (which is why some people warned you about not running it), the default settings will leave your local commits alone for at least 2 weeks.

LeGEC
  • 46,477
  • 5
  • 57
  • 104