Live data from Hacker News

How to undo almost anything with Git (2015)

github.blog

21–30 of 84 posts

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

#21

Git is one of those once-a-decade technologies that's compicated, user un-friendly, takes months to master and is absolutely worth it.

It only takes a couple of hours to start being productive while using it though. All the commands you need to know are add, commit, push, pull, branch and checkout. Perhaps add log and reset to that list if you make errors.

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

#22
post #18

Earlier quoted context omitted.

Curious, what do you currently use for version control?

I'm not that poster, but Mercurial is just as powerful and much easier to use.

That'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.

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

#24

> 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?

It means your local master is behind remote origin/master. (i.e. you needed to git pull)

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

#26
post #20
post #19

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.

You 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-fro...

When 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

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

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

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

#28

Git is one of those once-a-decade technologies that's compicated, user un-friendly, takes months to master and is absolutely worth it.

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 realizing that there's pieces of work that each command can do. One tip is to use (even very temporary) branches rather than `stash`es unless it's absolutely short lived. And if you ever get lost, `reflog` is your friend.

For me, I had trouble first using `git` after experience with `cvs` and `subversion`. At some point, everything clicked because I inferred its internal model.

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

#29
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 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.

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

#30

Git is one of those once-a-decade technologies that's compicated, user un-friendly, takes months to master and is absolutely worth it.

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 plumbing.

Oh, and the fact that a ‘git checkout’ is an unrelated concept to ‘svn checkout’ - and how SVN branches are “spatial” compared to git’s “parallel-universes” model is also a source of confusion.

If I could go back in time to 2005 (in addition to getting in super early on bitcoin) I’d beg Linus to not reuse SVN terminology. Personally I’d go with “git switch” instead of checkout, and “timeline” or “line” for short instead of “branch”. The fact that git branches are not 1-connected graph routes is enough reason not to call them “branches”.

I’d also convince him to include a “gitd” mode that would automatically fetch from upstream and automatically add commits when it detects file-move/rename changes. (I understand why git doesn’t have an exact “rename” op, but tooling today still isn’t good enough to detect rename-then-edit or rename-then-split operations without an intermediate rename-only commit. My dream “gitd” would also make auto-commits every 30 seconds just to give me a powerful filesystem undo feature. If you had hours of work that resulted in new files before you staged them and you accidentally did a hard reset then you’re sol - and this has happened to me already :/

Post reply on HN