Home > Software design >  Is there a way to get back the commits in this case?
Is there a way to get back the commits in this case?

Time:11-23

I created a git bare repo today, and added some dotfiles and configs. The thing that mattered the most was my qtile config, since I worked a lot on it.

After adding everything, I pushed and all was good.

I did this because I was planning to distro hop, and I whipped my disk clean after that (all other important files are saved on the cloud).

Installed new distro (EndeavourOS, before I was using Manjaro) and created a new git bare repo.

This is were things went wrong. It did not allowed me to pull files after adding the remote repo, so I figured, let me just add and push everything I have in this PC (which is not much since it is a fresh install) now and even if it overwrites something, I'll just check git and copy the differences.

Well, I forced pushed the things I added and because I hadn't made a pull before, that commit overwrote the previous commit that I did before in which I added all the configs important to me.

I went to the repo in github and all files are lost, and there is only one commit showing (the one I force pushed).

I know I did several things wrong to put myself in this situation, but I wanted to know if there is any way to recover those files that I previously had in my repo and were overwritten.

CodePudding user response:

GitHub has an events API.

Look for PushEvents, and the "before" field in the payload for such events.

Once you get the commits hash, you should be able to run git fetch origin <sha>, then git checkout <sha>:README.md.

note: the <sha> in git fetch origin <sha> should be the complete 40 characters sha1, not a short sha -- github will treat anything that isn't a 40 characters long hexadecimal string as a reference name.

CodePudding user response:

Unfortunately, once a commit is overwritten in Git, it is typically not possible to recover the lost data. However, There are some potential ways to recover lost data in Git, but they usually involve using specialized tools and techniques, and even then, the chances of success are not guaranteed. Here are a few options you could try:

  • Use the git reflog command to see a list of recent changes to your repository's local history. You may be able to identify the commit that contained your lost files, and then use the git reset command to "rewind" your repository to that commit. Note that this will permanently discard any changes made since that commit, so make sure you don't have any important changes that you want to keep.
  • Use a third-party tool such as Git Extensions or GitForce to try to recover lost data from your repository. These tools often have features that can help you dig deeper into Git's history and try to recover lost data. Note that these tools are not guaranteed to work, and you may need to experiment with different options to see if any of them yield results.

Note: In general, it's important to be cautious when using the git push and git push --force commands, as they can overwrite previous commits in your repository.

  • Related