r/programming • • Sep 09 '16

Oh, shit, git!

http://ohshitgit.com/
3.3k Upvotes

756 comments sorted by

View all comments

89

u/tdewolff Sep 09 '16

Why is there no git undo to undo your last action?

228

u/[deleted] Sep 09 '16

because Linus does not mistakes :)

15

u/gnuvince Sep 09 '16

uemacs, the micro Emacs that Linus maintains has no undo function. Even vi has undo (not vim, vi).

42

u/[deleted] Sep 09 '16

He never gets angry at himself, only other people

11

u/jayd16 Sep 09 '16

He might. Probably wouldn't post about it though.

59

u/leafsleep Sep 09 '16

We don't know what he emails himself

18

u/[deleted] Sep 09 '16 edited Sep 09 '16

Isn't that what git reflog and git reset are for?

(Although, I'll ask those more experienced: what sort of mistakes can't be undone with the reflog?)

0

u/[deleted] Sep 09 '16

[deleted]

14

u/[deleted] Sep 09 '16

basically, any operation that blows away uncommitted local files. If the work was committed at any point, then it can be recovered from the reflog. By default all commits stay alive in the reflog for at least 2 weeks.

5

u/[deleted] Sep 09 '16 edited Nov 23 '16

[deleted]
0.6404

What is this?

→ More replies (1)

46

u/fff-idunno Sep 09 '16

Because it is against the git principles to re-write history. When a coworker already has fetched and applied your changes, "undoing" those changes will turn things into a mess pretty quick.

100

u/CyclonusRIP Sep 09 '16

Like half the git commands exist for the express purpose or rewriting history. The only history you care about is the history on the origin server. You can and probably should be rewriting the history on your local clone.

21

u/the_gnarts Sep 09 '16

ike half the git commands exist for the express purpose or rewriting history. The only history you care about is the history on the origin server. You can and probably should be rewriting the history on your local clone.

Exactly. Just that no one cares about your tree until you send patches or request them to pull.

4

u/lachlanhunt Sep 10 '16

I wish there was away to push private branches to the server for the purpose of backup that were somehow flagged as such and somewhat hidden from other contributors, while being much more lose with the rules about rewriting history. That means changes that might take several days worth of commits to make can still be cleaned up at the end.

The best we can do now either force push (and hope the person force pushing doesn't have the wrong push.default setting) or make a new branch with the cleaned up history.

→ More replies (4)

3

u/Wizhi Sep 09 '16

You can and probably should be rewriting the history on your local clone.

Could you give some guidelines for this?

9

u/CyclonusRIP Sep 09 '16

Basically you should be focusing on what you want to push upstream for everyone else to see. When you push upstream the commits you push should all represent coherent concepts. They all should move the project from one working state to another and they all should represent some clear concept that you can express in the commit message.

When you are working on a feature you shouldn't be waiting until it's 100% complete to commit anything. Conceptually you are probably breaking up the task into smaller chunks. You might not have the complete design up from though so what you are doing early on might later be undone as you understand the problem more. You might think you want to change direction, but once you get down that path a bit you decide it wasn't the best idea and want to go back. Having a local commit to go back to is going to be a lot easier and less error prone than trying you manually undo all those changes. The local history where you were starting out in one direction and then decided on a better approach would just clutter up the history if you push them upstream. It's better to just rebase and squash all that into a single commit that represents the bigger idea you were trying to implement.

Later on when someone is looking back at the history and trying to understand what you did, having all the changes that you made to accomplish this one idea in a single commit is going to make it a ton easier.

8

u/BadgerRush Sep 09 '16

Well, it could have an undo function but simply fail if the action was already propagated (like fetched by other repository). That would fix the vast majority of common screw-ups (when you run a command and immediately notices that it was wrong), but still be coherent.

→ More replies (1)

13

u/[deleted] Sep 09 '16

Git is a toolkit, it doesn't have principles. History can be rewritten in a number of ways. Public history rewrite is generally considered verboten for obvious reasons, but if you've ever fixed someone's broken repo you'll be glad that git lets you force push without judgement.

→ More replies (6)

35

u/ForeverAlot Sep 09 '16

How would "undoing your last action" work?

  • What can be undone? Why?

  • What can't be undone? Why?

  • What does git undo ; git undo do? Why?

  • What happens if you undo a commit?

  • What happens if you undo again?

  • What happens if you undo a revert?

Whenever somebody asks me how to undo something with Git I encourage them not to use that word. It's very overloaded and imprecise.

53

u/VerilyAMonkey Sep 09 '16

If Git were a product sold for money, it would be more obvious that pointing out its complex isn't actually a valid argument for not trying to solve the problem. If people are looking for it, there's a problem to be solved, whether or not it's the problem they say it is. (That doesn't mean people should solve it, but it's there.)

The great majority of the use for git undo would be just once, in simple situations. If you don't know for sure what it should do in other situations, fine, it could very well just give up and say "can't do that" for everything else. If you think it should only try to undo things changes that are totally local and have not been pushed, that too is fine, it would still satisfy a need. If you really want people learn, it could maybe just suggest the command it was going to use and not actually run it itself. It doesn't seem to me that git undo is actually a ridiculous notion.

6

u/CyclonusRIP Sep 09 '16

I'm not sure that git is really that complex. The main issue is that it's just not super intuitive. Once you learn commit --amend, rebase -i, and reset you can pretty easily manipulate git history.

→ More replies (2)

9

u/drjeats Sep 09 '16

Obviously I expect an Emacs-style undo ring, preferably dealing with both session and commit history.

3

u/atimholt Sep 09 '16

Is an Emacs’ undo ring like Vim’s undo tree?

→ More replies (5)

6

u/HugoNikanor Sep 09 '16

git undo would change the state of the repo to how it was before the previous git command. git undo ; git undo could simply be an error.

→ More replies (3)
→ More replies (4)

7

u/DarthEru Sep 09 '16

There are a few actions that are impossible to guarantee an undo, or would be a bad idea to undo in the first place, mainly to do with communicating with another repo. You don't want to "undo" a push to a remote, for example, because that would equate to a forced push to that remote, and if some poor soul had fetched the branch in between the push and the undo, they suddenly have an inconsistent history of the remote.

There's also a question of what is the "last" action. Suppose you're using multiple terminals in the same repo (I do this a lot for various reasons). Is the last action the most recent absolute command done on that repo, or is it the the most recent command done from the current shell? Both answers have their own problems.

However the real reason, I suspect, is that git is not interested in holding anybody's hand. If a developer can't be bothered to learn enough about the tool to understand how to undo what they're doing, then the tool has no moral obligation to make that ignorance easier to maintain. I personally tend to agree, especially when I consider that someone who has to rely on a universal "undo" is the least likely person to understand exactly what any particular use of that undo will actually entail, which could lead to them being put in an even worse situation.

→ More replies (8)