Sometimes I just make a mistake and do something I did not want with Git:
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.