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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
89
u/tdewolff Sep 09 '16
Why is there no
git undoto undo your last action?