What Are Some Actions In Git That Are Difficult/Impossible to Undo?

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?


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


    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.


    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, ie 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.

    链接地址: http://www.djcxy.com/p/37810.html

    上一篇: 很难不按预期工作

    下一篇: Git中有哪些操作很难/无法撤消?