Yeah learned this the hard way.

  • ranzispa@mander.xyz
    link
    fedilink
    arrow-up
    1
    ·
    6 days ago

    The main reason I use git is it allows me to make mistakes without hard consequences. Any fuckup is just one reset away from being fixed. I like to: I have to fix this thing. While attempting to fix it I discover there is another thing that needs fixing on which everything revolves. I fix the second thing and commit. I’m now free to fuck around the code all I want and I’m sure I won’t lose that fix.

    For this I really like to use --fixup when I find out the change was not completely right or does not fit well with some other changes I need to do. I really like git absorb which automates this a bit.

  • Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    94
    ·
    19 days ago

    I’ve had juniors who didn’t believe this, so just to say it: If you know what you’re doing, practically any Git problem is recoverable.

    The one major exception is if you delete your local changes before committing them.

    • BootLoop@sh.itjust.works
      link
      fedilink
      arrow-up
      3
      ·
      18 days ago

      Does deleting the repo off disk and redownloading from remote considered recovering? If so I’m a git expert.

          • toynbee@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            18 days ago

            I mostly only use git at work and I guess I’ve never had the same work machine long enough to worry about this. It helps that each of our repos is fairly small. At least the ones I touch.

    • sucoiri@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      18 days ago

      Fun fact! If you added a file but never committed it you can still recover it with git fsck --lost-and-found. The changes were still added to the git object store, and by grepping through the dangling blobs you can find the object containing the changes you care about.

  • QuizzaciousOtter@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    37
    ·
    edit-2
    19 days ago
    1. Use git for any code you write. Yes, even a simple script.
    2. Commit and push often. More often than you think is reasonable. You can always rebase / fixup / squash / edit but you can’t recover what you didn’t commit.
    3. ???
    4. Profit.

    Seriously, once you commited something to the repo it’s hard to lose it. Unless you delete .git. But a this point frequent pushing has your back.

    I know git can be hard to grasp in the beginning. It was hard for me too. I highly encourage everyone to put in the effort to understand it. But if you don’t want to do that right now just use it. Just commit and push. It will pay off.

    • Rooster326@programming.dev
      link
      fedilink
      arrow-up
      6
      ·
      edit-2
      18 days ago
      1. (3) Get annoyed by constantly increasing Code Coverage Requirements on untestable (often legacy) code. Even afding comments requires code coverage go up! Line must always go up!
      2. Change step 2 to “Commit and push ONLY when absolutely necessary. Because adding comments often requires a rewrite of untestable code.”
      3. Go back to Step 2 and wait for a major bug.
      • fibojoly@sh.itjust.works
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        18 days ago

        Our company “architects” decided we needed 80% coverage minimum. On a mostly .Net 4.8 codebase. Hundreds of programs used in prod, with barely any tests in sight.
        It’s a fucking nightmare.

        • Metju@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          17 days ago

          Ah, the classical “just introduce tests in a legacy codebase”, what can go wrong?

          My condolences, it’s always a BITCH to handle

          • fibojoly@sh.itjust.works
            link
            fedilink
            arrow-up
            2
            ·
            15 days ago

            Fixing that bug you just found in prod : half a day. Adding enough tests so the app is now at 80% coverage : a whole fucking week. My student colleague was not impressed. I was like “yup, that’s what jobs are like”.

      • ExperiencedWinter@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        18 days ago

        Why would you care about code coverage requirements on a branch that is currently in development? My work in progress commits might not build because they don’t even compile, let alone have passing tests. The only time I care about the build passing and meeting requirements is when I’m ready to create the pull request.

        Also code coverage is a useless metric, but that’s a different story.

      • QuizzaciousOtter@lemmy.dbzer0.com
        link
        fedilink
        arrow-up
        1
        ·
        18 days ago

        3 is not related to using git in any way. I’m not really sure what you mean in 4. I didn’t mean making a lot of changes, I meant that you should not wait with committing until you have a finished feature / fix / whatever. Commit after each refactor, commit after adding a new testable unit. It’s always better to have more checkpoints. If your team does code review, they will appreciate atomic commits too.

  • marcos@lemmy.world
    link
    fedilink
    arrow-up
    18
    ·
    19 days ago

    You know… A version control system… That class of software that makes it possible for you to recover from any error you commit.

  • csm10495@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    18
    ·
    19 days ago

    Special shout out to the person who committed a gigabyte memory dump a few years ago. Even with a shallow clone, it’s pretty darn slow now.

    We can’t rewrite history to remove it since other things rely on the commit IDs not changing.

    Oh well.

    • Michal@programming.dev
      link
      fedilink
      arrow-up
      10
      ·
      19 days ago

      Sounds like a flawed workflow, if this didn’t go through at least code review. Was it committed directly to master?

      Curious to know what kind of system relies on hashed not changing? Technically the hashes don’t change, but a new set of commits is made. The history diverges, and you can still keep the old master if you need it for some time, even cherry pick patches to it…

    • SleeplessCityLights@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      18 days ago

      The guy at work who managed git before me, well didn’t quite have the knowledge I do and was not using LFS. In one of the main repos a 200mb binary was pushed 80+ times. This is not the only file that this happened to. Even if you do a shallow clone, you still need to add the commit depth eventually. It’s a nightmare.

      • einkorn@feddit.org
        link
        fedilink
        arrow-up
        4
        ·
        19 days ago

        They most likely did in a later commit. However the commit adding it can not be removed as OP said. So in order for git to be able to work properly the file is still in the repository in case someone wants to check out a commit where it still was present.

  • cmhe@lemmy.world
    link
    fedilink
    arrow-up
    16
    ·
    edit-2
    18 days ago

    Isn’t it the exact opposite?

    I learned that you can never make a mistake if you aren’t using git, or any other way for having access to old versions.

    With git it is really easy to get back to an old version, or bisect commits to figure out what exact change was the mistake.

    The only way I understand this joke is more about not wanting to be caught making a mistake, because that is pretty easy. In other methods figuring out who did the mistake might be impossible.

    • AeonFelis@lemmy.world
      link
      fedilink
      arrow-up
      9
      ·
      edit-2
      18 days ago

      This is not about mistakes in the Git-managed code. This is about mistakes in the Git commands themselves. Anything that involves merging/rebasing/conflict resolution can potentially be botched. These mistakes are usually fixable, but:

      1. Fixing it requires some Git proficiency behind the level of the common Git user.
      2. If you don’t catch it in time, and only find the mistake when it’s deep in your layers of Git history - well, good luck.
      • LePoisson@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        18 days ago

        Went to tech elevator boot camp, was a decent experience even if I don’t find myself doing exactly what I was expecting to do. Life is that way though.

        Anyways, my first week I fucked some git stuff up so bad I became the go to guy when anyone had any git issues because I had to learn so much to undo my egregious error. I don’t remember now exactly what it was but it took some serious git magic to sort.

        Just saying that point 1 is very true. And yeah don’t make mistakes in git.

      • offspec@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        18 days ago

        Short of manually deleting .git you can always find any commit, you can walk backwards through your reference lof if it comes to it, the only real risk is throwing out unstaged changes.

      • cmhe@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        18 days ago

        Hmm… I am using git for maybe 15 years… Maybe I’m just too familiar with it… and have forgotten my initial struggles… To me using git comes natural… And I normally pay a lot of attention to every single commit, since I started working on patches for the Linux kernel. I often rebase and reorder commits many times, before pushing/merging them into a branch where continuity matters.

    • fibojoly@sh.itjust.works
      link
      fedilink
      arrow-up
      3
      ·
      18 days ago

      You’re confusing errors in your code, and errors while doing some git command you thought you knew. Lucky you, it’s clearly never happened to you because you don’t mess around with it.

  • Eiri@lemmy.ca
    link
    fedilink
    arrow-up
    12
    ·
    19 days ago

    As long as you never touch the rebase button, you’ll be fine. Probably.

    • GissaMittJobb@lemmy.ml
      link
      fedilink
      arrow-up
      12
      ·
      19 days ago

      Don’t be afraid of rebases, they are an essential tool in Git.

      This particular fear can only be addressed by understanding.

      • Eiri@lemmy.ca
        link
        fedilink
        arrow-up
        1
        ·
        17 days ago

        I don’t understand it. Every time I see something about a rebase it’s some purist telling me it’s “cleaner”. Never got it to do what it says on the tin, and never hit a situation that I couldn’t solve using more straightforward tools like merge.

        • GissaMittJobb@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          17 days ago

          What’s your mental model for a Git commit, and a Git branch?

          Once I properly learned those two concepts, understanding rebases became a lot easier.

          I’ll try to explain it to the best of my abilities.

          • Think of a commit being a patch - a description of how to take a particular file from one state to another
          • A branch is a list of patches to be applied in order, from the point where the branch was created until the last commit on the branch

          When you rebase a particular branch, what you’re essentially doing is taking all of the commits that are currently on your branch, checking out the other branch, and then applying each of the patches in order on that new branch.

          A rebase can be cleanly applied if the premise for each commit has not changed when applied, but if the premise has changed, you get a conflict to be resolved before being able to continue the rebase.

          I mentally model a rebase a bit as a fast version of how it would look like to build the branch I was on, but on top of the branch I’m rebasing on.

          • Eiri@lemmy.ca
            link
            fedilink
            arrow-up
            1
            ·
            17 days ago

            That’s a good explanation of what it’s supposed to do. That was how I understood it as well.

            But anytime I’ve tried it, I’ve ended up with conflicts where there shouldn’t be (like, I already solved that conflict when I merged earlier) and/or completely undesirable results in the end (for instance, some of my changes are just NOT in the result).

            So I just gave up on the whole feature. Simpler to just merge the source branch into mine.

            • GissaMittJobb@lemmy.ml
              link
              fedilink
              arrow-up
              1
              ·
              17 days ago

              Depending on how structured your commits have been, it can either be very difficult to get a rebase through or a complete breeze. There are some features to make it easier - rerere being the main one I’m thinking about.

    • Michal@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      19 days ago

      Even if you rebase you can still recover the original commits until they are garbage collected. You are generally safe as long as the .git directory isn’t deleted, in which case your whole history is gone anyway.

  • elbiter@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    18 days ago

    I just commit and push in the same line, all the time. Whenever I have to undo a change, I go to GitHub/Gitlab browse for the previous version and paste from there.

    I definitely know is not the intended way to do this, but it works for me and has saved my ass several times.

  • buddascrayon@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    19 days ago

    “Shit, does this mean I have to re-install the operating system again. Fuck me!!!”

    I have uttered this sentence too many times to count.