One of the strengths of Git is that, because it operates with pointers, it is relatively easy to undo a wide array of tasks including deleting a commit or commits or creating and deleting remote branches. In many cases, all you really have to do is correctly reset the current branch's HEAD pointer to the desired location and voila, step undone. This encompasses a pretty wide array of cases.
Other than deleting an entire repository or a bad push, what's the most non-trivial action that either cannot be undone or is extremely difficult to undo in a standard Git repository?
There is the git clean command which is a convenience utility for undoing changes to the working directory. Additionally, git reset can be invoked with the --mixed or --hard options and will apply a reset to the working directory.
The git revert command is a forward-moving undo operation that offers a safe method of undoing changes. Instead of deleting or orphaning commits in the commit history, a revert will create a new commit that inverses the changes specified. Git revert is a safer alternative to git reset in regards to losing work.
If you have a commit that has been pushed into the remote branch, you need to revert it. Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line.
Using the git reset --hard option resets the current branch tip, and also deletes any changes in the working directory and staging area. Therefore, it resets index entries to the state of the specified commit.
Undo with: git checkout feature and git rebase master. What’s happening: You could have done this with git reset (no --hard, intentionally preserving changes on disk) then git checkout -b <new branch name> and then re-commit the changes, but that way, you’d lose the commit history. There’s a better way.
If you want to undo all changes, after git reset --hard, you should git checkout <branch>. I really don't get the idea of [duplicate] then ask a new question, when the answers are not satisfactory. It's a recipe for disaster in terms of more duplicates....
The main downside is that you won't be able to revert or modify old commits if you ever need to in the future, which may not be desirable in some cases. Again, for reference, we have this commit history: Let's create a toy feature branch and add some commits:
What’s happening: git checkout alters files in the working directory to a state previously known to Git. You could provide a branch name or specific SHA you want to go back to or, by default, Git will assume you want to checkout HEAD, the last commit on the currently-checked-out branch.
By far the most common "difficult to undo" error I come across amongst those new to git are abuses of git stash
, because git stash pop
isn't always reversible with git stash
. Consider:
git init /tmp/trash && cd /tmp/trash # make a new repo
echo A > A # create a file
git add A
git commit -m "Initial commit" # add and commit it
echo B > A # ... now change it
git stash # ... and stash it
echo C > A # and change it and commit it again
git add A
git commit -m "Another commit"
git stash pop # now pop the stash
The pop
will attempt to automerge A
and will throw a conflict, but you can't just back out of the pop by hitting git stash
again. This is a fairly trivial example, but it's easy to get into a nasty spot if you're stashing large sets of changes often, and switch divergent branches frequently, popping along the way. I believe git stash drop
is also permanent, i.e. there's no safety net if you drop the wrong stash.
In general, use stash
for what it was designed to do: Stash a dirty working index to hop over to another branch, fix a bug, commit, push, and hop back to your original state. If you try to use it to manage a lot of changes across many branches, it will inevitably bite you without a great deal of care.
git clean
deletes untracked files. They can't be restored with git.
Merging with an dirty working tree can result in something that is hard to restore.
So in short, things that aren't tracked by git can't be restored by git. Everything else is restorable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With