19 ms·
How to undo almost anything with Git (2015)
- dang 7y agoDiscussed at the time: https://news.ycombinator.com/item?id=9679367 https://news.ycombinator.com/item?id=9679367
- noir_lord 7y agoManaged to miss this first time, this is cool. Also Merry Christmas and Happy New Year.
- 3pt14159 7y agoIt 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.
- juliangamble 7y agoIt's missing git revert merges git revert -m 1 88113a64a21bf8a51409ee2a1321442fd08db705
- chrisweekly 7y agoPSA: You can safely refer to those long "commit-ish" identifiers using just the first 7 or 8 chars (eg `88113a64`) without real risk of collision. Let alone in examples / gists!
- lpghatguy 7y agoGit will even warn you if the hash prefix you provide is ambiguous! I had a collision the other day on a mediums-sized when using a 6 character hash and was surprised. Git let me know and told me which objects collided.
- maksimum 7y agoWow, that's super unlikely. How many commits did the repo have? According to [1] using 3 letters you'd need at least 72 commits to have at least 50% chance to observe a clash. Using 6 letters you'd need at least 3977 commits, lol. [1] https://en.wikipedia.org/wiki/Birthday_problem#The_generalized_birthday_problem https://en.wikipedia.org/wiki/Birthday_problem#The_generaliz...
- gdxhyrd 7y agoHow is that unlikely? 4k commits is nothing. Any project with a dozen developers is going to reach that easily.
- mikestew 7y agoI’ve been using git for going on ten years. You don’t think I can pull off just one commit/day? I use six character hashes, I don’t recall that I’ve had a collision yet. But I guess I’m “due”. :-)
- deft 7y agoThis page is from 2015 yet hasn't once came up on my searches to do a bunch of these things. Thanks for resharing.
- m4r35n357 7y agoTLDR: Reflog. That is all.
- jldugger 7y agogit-extras has a tool called `git undo`. It's just a fancy wrapper around `git reset --soft HEAD` though.
- UweSchmidt 7y agoWell you never know what these wrappers and aliases do, and ultimately need to understand what goes on under the hood. Intellij has a "shelve" command, I decided to stick with "git stash". Magit has the "i", that does "ignore" but also cleans up already committed files, something like "git rm -r --cached ."
- crazypython 7y agoI use GitUp.
- RickJWagner 7y agoGit is one of those once-a-decade technologies that's compicated, user un-friendly, takes months to master and is absolutely worth it.
- mixmastamyk 7y agoWhy is it worth it? I've been happier with other vcs.
- bransonf 7y agoCurious, what do you currently use for version control?
- coryrc 7y agoI'm not that poster, but Mercurial is just as powerful and much easier to use.
- Pinckney 7y agoThat's what everyone says, but for me, coming from a rebase heavy git workflow, I've found Mercurial far more difficult to learn. For example, with Mercurial, there's at least four different ways to do a rebase-ish thing: transplant, graft, rebase, and rebase (w/ evolve enabled). It's not obvious which a newbie should pick (rebase+evolve... I think?). Likewise, Mercurial has purge and strip which both delete commits in different ways. Git has multiple ways to do the same thing, but at least it's simple and consistent when you lift the hood. Undoing any sort of rebase-ish operation in Mercurial also seems difficult and janky. It seems to take multiple steps, and involves unbundling some sort of patch file stored underneath your home directory. Whereas in git, you just update a pointer: `git reset --hard $BRANCH@{1}`. Git's reflog is such a fantastic safety net. Doing any sort of history rewriting in Mercurial feels very dangerous, in comparison.
- quleap 7y agoYou surely don't know much about Mercurial. "purge" has NOTHING to do with commits. "purge" only removes file not tracked by Mercurial. "graft" replaced "transplant". "graft" copies commits while "rebase" moves commits. These commands all have very clear purposes, unlike those in Git. It's just that you haven't really bothered to spend a few minutes on learning them.
- kbenson 7y agoThis 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/ http://justinhileman.info/article/git-pretty/
- thefunnyman 7y agoWhy does the rebase command here include the --force flag? I don't think it's necessary.
- saboot 7y agoFor 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.
- Trisell 7y agoYou should be able to run a command that cleans the git tree of the file in all commits. I’ve had to do it because of committed passwords in files. https://stackoverflow.com/questions/35115585/remove-file-from-all-commits https://stackoverflow.com/questions/35115585/remove-file-fro...
- karmakaze 7y agoWhen using `git filter-branch` it's much faster (up to 100x) to use the `--index-filter` option rather than `--tree-filter` as it only updates the git history and not the working directory. Docs and examples at https://git-scm.com/docs/git-filter-branch https://git-scm.com/docs/git-filter-branch
- james_s_tayler 7y agoLike others comment it's possible to scrub it from the repo entirely but this will change every commit hash (I'm guessing back to the commit where the file first appeared). It's probably something you need to coordinate carefully among all the teams. Have no open branches, scrub it, force push that then people can resume work.
- SlowRobotAhead 7y ago> 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?
- godot 7y agoIt means your local master is behind remote origin/master. (i.e. you needed to git pull)
- SlowRobotAhead 7y agoOHH... ok! That makes sense. My last two projects I’ve been the only one committing so this pretty much can’t happen. Thanks.
- deleted 7y ago[deleted]
- godot 7y agoI'm actually somewhat surprised that this wasn't mentioned in the comments yet: https://ohshitgit.com/ 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/ https://dangitgit.com/)
- Stratoscope 7y agoIf you have to use the command line, those "gosh darn it git" sites have some good recipes. Because if the problem is "I accidentally committed to the wrong branch!" who is going to remember all this off the top of their head: # undo the last commit, but leave the changes available git reset HEAD~ --soft git stash # move to the correct branch git checkout name-of-the-correct-branch git stash pop git add . # or add individual files git commit -m "your message here"; # now your changes are on the correct branch Or you can use a powerful GUI like SmartGit. Then you don't have to memorize anything or look up a recipe. For the situation above, you simply drag your branch markers to where you want them in the log. Done! Similarly, for situations where you would look up hashes in the reflog, just click the Recyclable Commits checkbox, and everything in the reflog shows up as ordinary commits in the same commit tree as everything else. You can even see diffs between the reflog commits and your regular commits without having to do any temporary checkouts. I know many developers like the command line and don't want to consider using a GUI. But I encourage you to give SmartGit a try. It works in conjunction with the command line, so you you're not locked into the GUI, you can use either one whenever you want.
- maksimum 7y ago> It works in conjunction with the command line Do you know of git GUIs that explicitly maintain a bijection between the GUI and the underlying command history? It'd be cool to use the GUI and see the command history, or use the CLI and see updates in the GUI.
- 7y ago
- onekorg 7y agoThe 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.html 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-out https://codewords.recurse.com/issues/two/git-from-the-inside...
- hrgiger 7y agoHere also my 'git from scratch' TODO for the new year list : https://wyag.thb.lt/ https://wyag.thb.lt/
- anongraddebt 7y agoThis looks great!
- de_watcher 7y ago"understand the data structures that make git work" - insert relevant xkcd.
- tomxor 7y ago> The thing that made git click for me was to understand the data structures that make git work. I cannot agree with this more! The data structure makes git git, there are some questionable CLI design choices but they all melt away once you get the data structure. In relation to trying to manipulate commits and "undoing things", everything will be significantly clearer the faster you can move away from thinking of undo like a one dimensional word processor function... Instead start to think of git as an immutable graph of commit objects you are able to traverse, reference and use in anyway - at that point "undo" seems like a completely inadequate description.
- nneonneo 7y agoAlso 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.
- Doxin 7y ago> check “git status” and read every line carefully. Seconded, but I'd go even further. Run and read git status before and after running any git command until you get comfortable enough with git to where you can predict the output without running it. Additionally you can go get git prompt[1] installed so you always know your git status at a glance. Saves a lot of typing in any case. As a last suggestion I'd say go read git-scm.com[2]. It has the excellently written man pages for looking up what a command does and which flags it takes. It also has an amazingly written free ebook explaining git top-to-bottom. After reading that you'll be a git wizard compared to your peers. [1] https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh https://github.com/git/git/blob/master/contrib/completion/gi... [2] https://git-scm.com/ https://git-scm.com/
- tduberne 7y agoI totally second reading the book front to back. It is not that long and fairly well written, and can save so much time and frustration in the long run. Giving the link is the first thing I do when helping someone with git. Unfortunately so many people put that aside and never read it, and then come back to me a few weeks later because they messed everything up again, and still do not understand what I mean with "index". Git is very powerful but a bit awkward, so I consider knowing the underlying concepts necessary to use it properly.
- sadness2 7y agoNot reading. Nothing is ever as good as this flow chart: http://sethrobertson.github.io/GitFixUm/fixup.html http://sethrobertson.github.io/GitFixUm/fixup.html
- amelius 7y agoBut can it also undo the undo? Anyway, shouldn't git just come with a "undo" command?
- fiddlerwoaroof 7y agoYes
- chx 7y agoExcept you can't undo git reset --hard unless of course you build a safety net. https://gist.github.com/chx/3a694c2a077451e3d446f85546bb9278 https://gist.github.com/chx/3a694c2a077451e3d446f85546bb9278
- think29 7y agoThink you can't dunk? Think again… http://bit.ly/2lYnwSI http://bit.ly/2lYnwSI