Live data from Hacker News

How to undo almost anything with Git (2015)

github.blog

51–60 of 84 posts

Re: How to undo almost anything with Git (2015)

#51

Earlier quoted context omitted.

Git shouldn't take months to master if you can understand it conceptually rather than by use-cases. If instead of trying to learn commands to affect the working directory, staged files, or history, you learn how git organizes historical tree of commit hashes, then base what each operation does, it becomes much clearer. It's similar to trying to learn strings of shell pipe commands to get things done rather than reali…

git’s problem to newcomers - even people without any prior experience with diff-paradigm systems like SVN and TFS - is the idea of a commit representing a snapshot of a file system is difficult to comprehend because it feels so crazy and impractical (especially as CS101 makes a huge deal about computational complexity). The fact that git internally is actually quite efficient is buried in heavy articles about git’s p…

When I used to use NetBeans the main feature I loved was the history feature.

Basically every save to every file was stored and diffable in a kind of self contained repo.

I've missed this feature in every other editor since.

Re: How to undo almost anything with Git (2015)

#52
post #27

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/ )

If 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 in…

Everything apart from the initial reset command in that ‘recipe’ is stuff that a command line git user would be doing all the time anyway. There’s not really anything to remember here, other than that ‘git reset’ lets me undo commits that I haven’t pushed.

Re: How to undo almost anything with Git (2015)

#53
post #32

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/…

"understand the data structures that make git work" - insert relevant xkcd.

Re: How to undo almost anything with Git (2015)

#54

Earlier quoted context omitted.

If 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 in…

I know you're trying to sell your SyntEvo(R) SmartGit(TM), but the last thing I'd want while already trying to recover from a mistake is having a third party tool do some opaque magic on the repo, in the same way that I wouldn't just copy-paste a recipe without understanding how it works. OTOH if a common operation can be performed reliably I'd much rather it be upstreamed.

> I know you're trying to sell your SyntEvo(R) [sic] SmartGit(TM)

That is a remarkably uncharitable comment.

If you have any evidence that I am a shill for syntevo, and not just a happy customer, you should state it.

Otherwise you owe me an apology.

Re: How to undo almost anything with Git (2015)

#55
post #27

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/ )

If 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 in…

There are other ways to do that, too. For example, Git stash is my blind spot and I don't really use it.

A conceptually simple way (for me) is to: checkout "good" branch, cherry-pick the "wrong" commit, then checkout the "bad" branch and remove the "wrong" commit ("git reset --hard HEAD^").

Mixing and matching simple commands mechanistically means everything can in fact be remembered.

YMMV.

Re: How to undo almost anything with Git (2015)

#56
post #27

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/ )

If 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 in…

If you're trying to memorize that list of commands without understanding what is happening under the hood you have a snowflakes chance in hell. If on the other hand you've read (and understood!) the git reset manpage it's hardly magic.

Git reset changes the ref HEAD is pointing at. So in this case HEAD is pointing to the branch you've accidentally committed to. Running `git reset HEAD- --soft` simply moves the branch to point to one commit previous. The soft flag tells git reset to not touch any actual files while doing this, so your changes stay untouched. This is really the most complex part. After this all that's happening is re-committing the same changes to the correct branch. The git stash is even optional if the changes don't conflict with any changes done by the checkout.

You're right, memorizing a list of commands like that is a fools errand. What's not a fools errand is understanding the underlying data structure of git and how various commands act on it. Which is all documented in the official docs by the way, using a writing style that's rather readable in my opinion. Switching to a GUI wholesale is throwing out the baby with the bathwater. Using a GUI tool still requires knowing how git works to be effective.

Of course you might still argue that it's too much typing, which is probably true. Good thing git aliases got invented so even though I can recall the entire command list you mentioned from memory I'd still in practice simply run "git uncommit" since I've had that alias defined for about forever now.

This comment isn't aimed at anyone in particular and especially not at the parent comment. The parent comment just hit my grumpy button I think ;-)

Re: How to undo almost anything with Git (2015)

#57

Earlier quoted context omitted.

git’s problem to newcomers - even people without any prior experience with diff-paradigm systems like SVN and TFS - is the idea of a commit representing a snapshot of a file system is difficult to comprehend because it feels so crazy and impractical (especially as CS101 makes a huge deal about computational complexity). The fact that git internally is actually quite efficient is buried in heavy articles about git’s p…

When I used to use NetBeans the main feature I loved was the history feature. Basically every save to every file was stored and diffable in a kind of self contained repo. I've missed this feature in every other editor since.

IntelliJ IDEA has that feature (local history)

Re: How to undo almost anything with Git (2015)

#58
post #44
post #32

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/…

Here also my 'git from scratch' TODO for the new year list : https://wyag.thb.lt/

This looks great!

Re: How to undo almost anything with Git (2015)

#59

Earlier quoted context omitted.

If 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 in…

There are other ways to do that, too. For example, Git stash is my blind spot and I don't really use it. A conceptually simple way (for me) is to: checkout "good" branch, cherry-pick the "wrong" commit, then checkout the "bad" branch and remove the "wrong" commit ("git reset --hard HEAD^"). Mixing and matching simple commands mechanistically means everything can in fact be remembered. YMMV.

You might want to try using rebase for this use case too, Saves a couple checkout steps. Just make sure to specify the right commit range ;)
Post reply on HN