Why is there no undo / redo in Git?

As far as I know, when you want to undo something in Git, you need to explicitly find the command to undo everything you did and release it. For example, one of the many ways to undo a commit and repeat this to follow the example here ,

$ git commit ... $ git reset --soft HEAD^ $ edit $ git add .... $ git commit -c ORIG_HEAD 

Or, to cancel the craving, you can follow the instructions here ,

 $ git reset --hard 

But these teams do not necessarily work interchangeably. Is there a reason Git doesn't allow undo and redo commands? Is there anything to do with the philosophy behind it? In addition, I do not have much experience with other version control systems, but do any of them offer a simple undo and redo command?

+7
source share
4 answers

There are several problems with this concept:

  • Not all operations are reversible. Sometimes this happens due to the fact that Git does not write enough information to display the previous state - it would be too expensive in general. Sometimes it is like git reset --hard or git clean , which destroy irreparable changes. To cancel them, it must be automatically automatically backed up. Sometimes this happens because the concept of undo is ambiguous - as you yourself indicated, there are many ways to undo a commit.

  • If the operation is reversible, and it is associated with some history, should it be canceled / repeated in the history, or should they disappear? If a commit is canceled by resetting back or by returning (creating another commit to cancel it)?

  • Without writing down the last thing you do, how would you know what happened in the last operation? Say you added a file to the index and created a branch. There is no record of what was first.

Even if everything was clearly defined, this would be an absurd job. How do you decide what constitutes one action? One Git team can do many things. If he cancels one step, is that all? What if you run millions of commands, each of which performs a tiny step, and you want to undo all this? And it must be perfect, completely perfect, because this is the type of function that will be used by inexperienced users who have no idea how to recover from any mistake.

So, just like Git provides you with the tools to work, it gives you the tools to see what you have done and undo everything yourself if necessary.

In addition, as for the “repeat”, as you defined it in your question, he repeats the command, and does not perform the original operation again. When you rewrite a commit, it was different. Repeating the previous command is what was designed for the command line. Git does not need to reinvent it.

+6
source

Actually, your first example can be done with:

 $ git commit ... $ edit $ git add ... $ git commit --amend 

The second example should be more like git reset --hard <hash>

The answer to your question is that it is potentially possible, but yes, it is more a git driving philosophy, which means it has not been done. Theoretically, there is no way to find out if you got into a commit by creating it or deleting another, but using reflog, maybe this is possible ... I have not thought about this before.

I don’t think that “cancel” and “redo” is a very common thing in version control, but correct me if I am wrong.

EDIT: you could install a script that could do what you used after using reflog - not sure if there is enough information, but it might be worth a try.

+4
source

git is actually a lot of small tools that act on the repository, index, and working directory, so it does not have a “main” part for any “undo”.

This suggests that he has various logs, such as reflog , so you can look back at what has been done.

Finally, by convention, many git actions are usually considered “only one way,” that is, you don’t want anyone to see it publicly, so git tries to get you around the block “when in public. If you're still in your local repo, you can back up using various commands, but global cancellation is not a suitable command offer.

+1
source

Along with other comments, I checked git stash . Many times I did some work, corrected something else, then swayed and continued. Or for webapp, start my finished and incomplete work, refresh the browser, do not age, open a new tab, and then compare the two commands back and forth to make sure that the error correction associated with the user interface did not break anything.

0
source

All Articles