0

Sometimes I just make a mistake and do something I did not want with Git:

  • committing bad code
  • creating a branch with a bad name
  • committing after fixing merge conflicts during git rebase instead of just adding files and running git rebase --continue (like here)
  • other possible misuses

Is it possible to put git repository (.git directory) under version control itself, and then write a script to make a command like "git undo" possible? Has anybody done it before? Or is this bad idea and there is something simpler?

Community
  • 1
  • 1
Bunyk
  • 7,635
  • 8
  • 47
  • 79
  • 1
    If its your most recent commit, then you can just **amend** your next commit and modify the name/details and overwrite the content that was committed. In other cases where the commit is older, I've faced this sort of problem before, but never really found a way to edit it. In a sense there's a reason why its locked, if you edit the code of a commit from 2 month ago, its going to affect every single commit that happened after that. Other than causing confusion if the project is shared with other developers. – Mirodinho Jun 01 '15 at 13:23
  • 1
    Just fix your commits or branch names?! – ckruczek Jun 01 '15 at 13:24

3 Answers3

1

This is great resource for learning git. Here's a few useful undo commands:

One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to try that commit again, you can run commit with the --amend option:

git commit --amend

This command takes your staging area and uses it for the commit. If you’ve made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same, and all you’ll change is your commit message.

Source: https://git-scm.com/book/en/v2/Git-Basics-Undoing-Things

U r s u s
  • 6,680
  • 12
  • 50
  • 88
1

Sometimes I just make a mistake and do something I did not want with Git:

  • committing bad code

You can easily undo the last commit using git-reset but it's not recommended to do this with published code.

git reset --soft HEAD^

(Note: You can use git commit --amend as mentioned in another answer to reset and commit at once.)

You can create a new commit that undoes the previous one and it works well with already published code.

git revert
  • creating a branch with a bad name

You can undo creating a local branch by simply deleting it.

git branch -D <name>

You can likewise undo a remote branch by deleting it.

git push <remote> :<name>

Is it possible to put git repository (.git directory) under version control itself, and then write a script to make a command like "git undo" possible? Has anybody done it before? Or is this bad idea and there is something simpler?

There is no need to have your .git in version control, it has its own intrinsic version control mechanisms as described above. When things get worse, you can always track reference changes using a low level tool called git-reflog.

You might think about a possibility of a global git-undo command. But in Git it is pretty clear which actions are local and which are remote. Once you publish your changes, you most often do not want to break continuity of commits. So a theoretical user-friendly history-safe undo command would have to take this into account and actually would have to follow a specific workflow. There is definitely a lot of space for workflow specific tools on top of Git but the tool itself is pretty much workflow neutral.

Pavel Šimerda
  • 5,783
  • 1
  • 31
  • 31
-1

The Question is very interesting but I think there is no solution to your problem.

1) It is not possible to track the .git directory itself (at least with git). And tracking git with some other version-control is not practical at all. And even if you could when should you stop tracking your version-control-tracking.

Should you track the tracking of the original track? This creates an endless track of tracks.

2) git is incredibly great with handling "errors" if you have done them locally. It takes some time to remember all the commands to undo and alter things afterwards, but if you have mastered them you will become way faster. At least faster then with a version-control tracking your version-control.

JDurstberger
  • 4,127
  • 8
  • 31
  • 68
  • First I don't think it's true there is no solution, second all in this answer is true but the answer doesn't provide any guidance for the original poster. – Pavel Šimerda Jun 01 '15 at 13:39
  • Everyone is posting answers with code how to undo things in git. But as I understand his question, he knows them, he wants to know if there is a global-possibility to undo anything with one command or by tracking git itself. Posting git commands doesn't address his question as I see it. – JDurstberger Jun 01 '15 at 13:43
  • I understand the question so that OP wants to undo just any command in Git and I believe my answer covers the specific examples in the question and more. I do think it also shows that there is no need to track changes in `.git` directory as Git's built-in tracking of those is sufficient. In my opinion, a person with knowledge of how Git works and how actions can be undone would ask such a question. – Pavel Šimerda Jun 01 '15 at 15:03
  • 1
    Anyway, I tried to take inspiration from your point of view and added a paragraph to my answer to address that possibility. – Pavel Šimerda Jun 01 '15 at 15:10