7

One of the strengths of Git is that, because it operates with pointers, it is relatively easy to undo a wide array of tasks including deleting a commit or commits or creating and deleting remote branches. In many cases, all you really have to do is correctly reset the current branch's HEAD pointer to the desired location and voila, step undone. This encompasses a pretty wide array of cases.

Other than deleting an entire repository or a bad push, what's the most non-trivial action that either cannot be undone or is extremely difficult to undo in a standard Git repository?

Community
  • 1
  • 1
joshin4colours
  • 1,420
  • 3
  • 15
  • 24
  • 4
    I don't understand the point of this question. – Mat May 01 '13 at 19:27
  • 1
    I *do* understand the point of this question and similar ones. In essence, it's asking, "Where be dragons?" This question is far from "not constructive", but it's a better fit for Programmers Stack Exchange. When starting out with a new technology, developers often want to know where the dragons, pitfalls, and booby traps are. – Gregory Higley Apr 11 '14 at 22:49

4 Answers4

4

By far the most common "difficult to undo" error I come across amongst those new to git are abuses of git stash, because git stash pop isn't always reversible with git stash. Consider:

git init /tmp/trash && cd /tmp/trash     # make a new repo
echo A > A                               # create a file
git add A
git commit -m "Initial commit"           # add and commit it
echo B > A                               # ... now change it
git stash                                # ... and stash it
echo C > A                               # and change it and commit it again
git add A
git commit -m "Another commit"
git stash pop                            # now pop the stash

The pop will attempt to automerge A and will throw a conflict, but you can't just back out of the pop by hitting git stash again. This is a fairly trivial example, but it's easy to get into a nasty spot if you're stashing large sets of changes often, and switch divergent branches frequently, popping along the way. I believe git stash drop is also permanent, i.e. there's no safety net if you drop the wrong stash.

In general, use stash for what it was designed to do: Stash a dirty working index to hop over to another branch, fix a bug, commit, push, and hop back to your original state. If you try to use it to manage a lot of changes across many branches, it will inevitably bite you without a great deal of care.

Christopher
  • 36,834
  • 9
  • 72
  • 91
  • It's an old entry, but in this case you can simply `reset --hard`. `stash pop` won't drop stash entry automatically in case. – MirMasej May 06 '16 at 09:09
3
  • git clean deletes untracked files. They can't be restored with git.

  • Merging with an dirty working tree can result in something that is hard to restore.

So in short, things that aren't tracked by git can't be restored by git. Everything else is restorable.

Ikke
  • 90,705
  • 23
  • 91
  • 118
  • Actually, not everything tracked by git can be restored. Deleting a branch appears to be permanent. There are even non-obvious ways for branch deletion to occur, e.g. `git push --mirror REMOTE`; when your local does not have branches that are on the remote, the remote branch is removed non-interactively. ([Cf. the excerpt from git help pages](https://git-scm.com/docs/git-push#_options_a_id_options_a): "--mirror [...] Newly created local refs will be pushed to the remote end, locally updated refs will be force updated on the remote end, and **deleted refs will be removed** from the remote end") – Kay V Nov 15 '16 at 04:08
3

An example of a difficult to undo action would be:

git branch -D some-unmerged-branch-such-as-master
git gc --prune=now --aggressive

The first removes the reference to some commits. The second deletes all commits without references.

I hope this question is just a matter of idle curiosity, rather than you wanting to trash somebody's repo, but due to the redundancy inherent in distributed version control, I'm not too worried.

Neil Forrester
  • 4,574
  • 28
  • 31
0

In my experience the most common irreversible action that people perform is git reset --hard. When many people write git reset, --hard seems to come too naturally when they really only needed a reset, perhaps with --keep.

CB Bailey
  • 648,528
  • 94
  • 608
  • 638