How to delete 16 days of commits 101 🤯:

First of all, me and my class (computer science in college) were working on a project for around 12 weeks, our “client” is one of our teacher and we literally just finished today to work on the project since our degree terminal projects are starting next week.

So now there's this guy in our class who kinda has the reputation to be stuborn and clumsy; he’s going to do his assigned task, commit, push it and put his task into QA (which is just peer evaluation and testing nothing really complex) and then when we try his functionality and finds out it isn’t working, we tell him and the only thing he always answers is : “but it works on my machine” and then we will need to explicitly ask him to be sure he has all the latest changes (database and codebase) and to see if it still works on his side since it doesn’t work for anyone else.

This actually happened quite a lot in these 12 weeks and you can definitely imagine that of course it would definitely not happen again today when we thought we were finally done with this project…

So another teacher gave us an assignment to create a development environment for our big project so we could try out Docker instead of virtual machines, he made GitHub Classroom repos with a minified version of our project and up to this point everything is fine and clear. That is until 3 hours ago, that our little clumsy friend somehow pushed his Docker related files on the main project, maybe he was trying his Docker setup on the real project no big deal you know EXCEPT IF HE HADN’T NOT PULLED SINCE 16 DAYS 😤.

He was doing maintenance on another project so I can maybe understand but gosh how did he not see the big warning of Git that he wasn’t up to date with master ? And yes we only have a master branch bear with us but hopefully we were able to create a new branch with the up to date project and then merge master.

A couple of us had a gut feeling that this guy would do something that would break the whole project right before we ended, turns out we were right 😅

  • 5
    Just use git branch and spawn of based on the last healthy commit.

    # git branch <branch-name> <commit>

    For example:
    # git branch develop 04c65f3

    Then Git push -f back on top of master, both problems fixed.

    You have a second branch, and a master that isn't destroyed 😅
  • 4
    You never lose anything in git.
  • 9
    @theabbie that's not true at all
  • 2
    @phat-lasagna You can lose code in an irreversible way in git?
  • 4
    Push your local master to a new branch and go from there. Afterwards configure the new/fixed master-branch as protected.

    Also checkout ohshitgit.com
  • 4
    @theabbie yes but you have to tinker with your ref log
  • 3
    @theabbie yes. Force push an old branch when one has a local instance of it and no other branches with history exists and its gone.

    But its hard to accidentally fo it if you are multiple people involved snd if they keep their own local branches around.
  • 4
    @Voxera Force push is like code suicide, Fortunately, I won't lose any code because of any fuck-up and my stupidity.
  • 0
    @Voxera can't we use reflog on whatever branch got force pushed?
    Some online systems like bitbucket even visualize it. Do I miss something?
  • 1
    @foox I am not sure. Force push rewrites the branch but there might be safeguards for that, i have never had to check for them fortunately.
  • 1
    @theabbie there is one acceptable case in my book.

    If you have pushed a branch and need to make changes to replace something AND your the only one using the branch.

    This case is to clean up commits since for history’s sake you do not need all the bug fixes from CR/PR.

    But I know some prefer to keep the full history anyway and some are adamant that any non vital commits are squashed before merge to main.

    I do not think there will ever be a real consensus on that ;)
  • 1
    @Voxera @foox as long as you have your local rep. The ref log has still all commits even after a force push and 2000 hard resets.
  • 1
    @KDSBest sure, as long as there are extra repos.

    But if you force push to the main and have no other branch with history and no extra repo, the I think you are out if luck.

    But I takes some dedicated focus to make so many mistakes ;)
  • 0
    @Voxera not extra, your local, which you used to force push is enough. I gave git workshops about the deepest internals of git.
    You will never lose anything you ever committed from your local repository. The only way is:
    A) you tinker with your reflog
    B) you got a detached head and you forcefully let git clean them up

    When you commut something hard rebase to the commit before that and force push to any repository you want. The history is destroyed on the remote repos. But on your local one in your ref log the commit you deleted is still in the ref log and will stay there for eternity!

    A branch is just a link to a commit so no need for extra branches also!
  • 0
    @KDSBest Ok, I am happy I have so far never had to dig that deep into git :)
Add Comment