from Hacker News

How to undo almost anything with Git (2015)

by ljiljana on 12/23/19, 8:36 PM with 79 comments

  • by nneonneo on 12/24/19, 7:09 AM

    Also worth noting: you can abort and undo many kinds of in-progress operations with “git <operation> —abort”. Works for merge, rebase, and a few other things, and can seriously save your bacon when you do a “git merge <badbranch>” and are suddenly confronted with a bazillion merge conflicts.

    Also, if you’re in a panic because something went screwy, check “git status” and read every line carefully. status tells you a lot more than you might expect - what branch you’re on, if you’re even on a branch, what merge/rebase/commit operation you’re in the middle of (if any), and even how to go forward with or back out of the current operation.

    Finally, commit regularly and often! reflog and rebase mean that you can always maintain a clean history if you want, while committing makes sure that your changes are properly tracked and saved by git so you can rewind when needed. Once you get comfortable with it, git lets you really practice fearless experimentation, which unlocked a whole new level of productivity for me.

  • by onekorg on 12/24/19, 6:12 AM

    The thing that made git click for me was to understand the data structures that make git work.

    There's beauty and elegance in the implementation details of git. You can do and undo with confidence once you can translate the changes you want to make to git object transformations.

    To get an overview of the concepts behind git I recommend this article by one of the GitHub founders: http://tom.preston-werner.com/2009/05/19/the-git-parable.htm...

    To understand the data structures I suggest: https://codewords.recurse.com/issues/two/git-from-the-inside...

  • by godot on 12/24/19, 5:06 AM

    I'm actually somewhat surprised that this wasn't mentioned in the comments yet: https://ohshitgit.com/ Which is a page that covers common mistakes using git and quick ways to fix your mistakes. I've found myself making a mistake mentioned on this page with regularity and always refer to the site to help myself fix it quickly.

    (It looks like they have a swearing-safe version now at https://dangitgit.com/)

  • by 3pt14159 on 12/23/19, 10:35 PM

    It has always bugged me that

        git commit --amend 
    
    Doesn't automatically resign (or fail to resign if the key isn't available) a signed git commit message. Anytime there is a non-verified git commit message in my history you can be sure it's because I was a dummy on the original message.
  • by kbenson on 12/24/19, 2:08 AM

    This flowchart[1] has been useful to me a few times when I've been lost on how to fix something. I usually keep it as a bookmark, but I don't have it saved on this computer, so I just did a google image search and was able to fine it fairly quickly. A google image search for "git solution workflow" or "git fix flowchart" finds it right away, in case this tickles your brain in the future and you want to find it again, and vaguely remember it's easy to find through image search.

    Edit: Updated the link to go to the site referenced in the image. Might as well send you to who developed it so they get some credit.

    1: http://justinhileman.info/article/git-pretty/

  • by RickJWagner on 12/24/19, 1:23 AM

    Git is one of those once-a-decade technologies that's compicated, user un-friendly, takes months to master and is absolutely worth it.
  • by deft on 12/23/19, 10:39 PM

    This page is from 2015 yet hasn't once came up on my searches to do a bunch of these things. Thanks for resharing.
  • by dang on 12/23/19, 9:31 PM

  • by chx on 12/24/19, 12:39 PM

    Except you can't undo git reset --hard unless of course you build a safety net. https://gist.github.com/chx/3a694c2a077451e3d446f85546bb9278
  • by saboot on 12/24/19, 4:11 AM

    For several months now I've been wondering how to get rid of a 700 MB data file that was accidentally committed to our shared git repo. Now everyone has it, and a clean pull takes forever. Appreciate any thoughts.
  • by sadness2 on 12/24/19, 8:28 AM

    Not reading. Nothing is ever as good as this flow chart: http://sethrobertson.github.io/GitFixUm/fixup.html
  • by SlowRobotAhead on 12/24/19, 4:56 AM

    > You started a new branch feature based on master, but master was pretty far behind origin/master

    I’m bad at git. So... wait what now?

    When is Master not Origin/Master? I don’t understand what is going on there. Explain?

  • by juliangamble on 12/23/19, 10:38 PM

    It's missing git revert merges

      git revert -m 1 88113a64a21bf8a51409ee2a1321442fd08db705
  • by amelius on 12/24/19, 10:19 AM

    But can it also undo the undo?

    Anyway, shouldn't git just come with a "undo" command?

  • by jldugger on 12/23/19, 11:02 PM

    git-extras has a tool called `git undo`. It's just a fancy wrapper around `git reset --soft HEAD` though.
  • by crazypython on 12/24/19, 12:52 AM

    I use GitUp.
  • by m4r35n357 on 12/23/19, 10:50 PM

    TLDR: Reflog. That is all.