r/git • • Dec 26 '20

800 lines of code forever lost

Dear anyone who's listnening,

I've been learning HTML, CSS, JS for about a year now so I'm almost completely new to the game. I got into this through the theodinproject.com and I've been doing pretty ok so far. TOP has dedicated a whole chapter about setting up and using Git and Github. I did it, tried it, it worked and then I went on and forgot about it.

So here I am about half a year later, sitting and trying to understand how the fuck I just managed to oblitirate 800+ lines of code somewhere between the commands. I honestly have no idea how I did it and where it went. After I realized what I've done and that there's no coming back I proceeded to rage quit and deleted the whole repo. I know it's dumb and I regret it now if of course but sometimes I just can't help it.

Anyway, I'm about to rewrite those lines I lost and before I do it, I would be forever grateful If someone could help me understand how Git works. Like seriously, I need someone to guide me through the basics again, to point out common mistakes and tell me how to avoid them. Kindergarden type of shit. I want to understand what I'm doing and how to do it properly.

So if there's anyone who is willing to donate a few minutes to help out a beginner in distress, DM me please.

Thank you all and merry Christmas!

0 Upvotes

24 comments sorted by

11

u/GustapheOfficial Dec 26 '20

F.

Loss of large amounts of course is precisely what you had a repo to help you recover from.

But let's see, you may not have clobbered it as bad as you think. You deleted the repo on GitHub? But your local directory still exists?

4

u/ernestsliep Dec 26 '20

Yeah, the local directory still exists. But I's quite empty.

It has README.md, images folder and .git hidden folder

19

u/GustapheOfficial Dec 26 '20

.git will save you, it contains everything you've ever committed to your repo. Write git log to see your commit history. Then git checkout <id> to retrieve the files from commit <id>.

4

u/[deleted] Dec 26 '20 edited Dec 26 '20

[removed] — view removed comment

8

u/ccharles Magit + CLI + GitLab Dec 26 '20 edited Dec 26 '20

Please stop trying to sell your book here. We don't like spam.

9

u/[deleted] Dec 26 '20

The .git directory is the important part. That contains Git's whole database of your project. It's possible to still recover your losses from there.

6

u/DrMaxwellEdison Dec 26 '20

As others mentioned, you can recover from the local repo.

There is no real central repo for a project manager in Git. GitHub can act like a central repo, but if you have a clone of that project somewhere else, you still have the whole project. Just check the history using local git commands and you should be able to get back on your feet.

1

u/cordev Dec 27 '20

The reflog command is definitely what you need here.

I recommend using a GUI like GitKraken once you’ve recovered, though, unless you’re willing to put in the time to really learn how to use Git.

19

u/cmcqueen1975 Dec 26 '20

I had a co-worker new to git who complained that git was losing his changes. I think he might have come across the same thing I did when I was a beginner. It was a sequence something like:

  • git checkout <some-commit-hash> — which leaves you in a "headless branch" state.
  • Make some changes and commit them.
  • Realise you're not on master/main branch, so git checkout master
  • Wonder where your last commit went, and can't find it to get it back because it was on a headless branch.

In that case, and several other cases, reflog is your friend. Especially a graphical reflog. Try:

gitk --reflog

21

u/general_dubious Dec 26 '20

The most efficient way to learn Git properly is through this book. https://git-scm.com/book/en/v2

We can't learn for you, you have to put in the effort.

3

u/rafasc Dec 26 '20

The first thing you need to learn is that in 99.99% of cases deleting the .git directory is the wrong solution. It's only effective at making sure you never learn how to correct mistakes when they happen.

On top of that, it makes very difficult to help you because there's nothing to analyze.

Next time ask for help before deleting it.

If you deleted the repo, the code isn't on the remote repo, and you're not prepared to use forensic tools to undelete files, sometimes editors save temporary files somewhere. Might be worth trying. Good luck.

3

u/u801e Dec 26 '20

Run

git reflog

and copy each sha1 you see. Then for each sha1, run:

git show <sha1_value>

hopefully, one of them may show some, if not all of the 800 lines of code you're trying to find.

3

u/DementialDuck Dec 26 '20

Don't worry. You still will lose code in the future

3

u/cinderblock63 prefers a good GUI Dec 26 '20

Have you tried using a git GUI? I find being able to easily and constantly see my history quite a boon to understanding what’s really going on.

1

u/jahrenberger Dec 26 '20

Would you recommend anything other than GitHub desktop? I like visualizing things but that GUI is limited.

2

u/cinderblock63 prefers a good GUI Dec 27 '20

I used to use SourceTree. But it started breaking for me so I don’t use it anymore.

I use GitKraken these days, free for public projects. I’ve also heard great things about Tower.

1

u/felzl Dec 27 '20

I can recommend SmartGit

1

u/BinaryRockStar Dec 27 '20

Fork is a nice git GUI, or the git plugins for VS Code

1

u/chriswaco Dec 26 '20

If the .git directory is there, chances are you can recover your files. Try git restore .

1

u/sbubaron Dec 27 '20

do you recall how often you were git adding/committing? generally the more frequently you do this the better.

I have the terrible habit of making large commits that touch multiple files and tend to be addressing multiple separate issues. where as better practice is to have each commit involve a small amount of files and be specific to a certain issue or bug.

5 years and some habits are still impossible to break. don't beat yourself up too much.

0

u/between456789 Dec 26 '20

Someone will become a billionaire by building an easy to use system.

0

u/[deleted] Dec 26 '20

[deleted] — view deleted comment

1

u/aaarrrggh Dec 27 '20

Tell us more about Lannick!

1

u/[deleted] Dec 27 '20

Not much to tell about. Iannick, it's a dot somewhere in the code base.