Undoing a git reset to uncommited state before reset?

So I was just testing some things on my master branch but knew I wanted to reset/revert to the last commit on master to before I started these test feautures. So I immediately create a new branch (lets call it testfeatures ) that I thought would not be affected by using git reset --hard while on the master branch. The new testfeatures branch was created in the same state as the master (so it still had all those test features), and I then checked out back to master. While on master, I used git reset --hard <#lastcommitidhere> . Now, this reset my master branch to how I wanted it, but come to find out it also reset the previously create 'testfeatures' branch (after creating this branch, I didnt commit anything before switching back to master and resetting).

So my question is, how would I undo the reset back on any branch to get those test features back? Is it possible? Also, why did resetting master have an affect on my other branch, as I thought they were isolated?

Also, the "test features" were not commited on the master branch either before the reset.


The uncommited changes in git are not on a branch and git does not keep track of them. So you basically had some committed changes, that git did keep track of, and you did some additional changes. When you created a new branch, the uncommitted changes were not duplicated (ie on both branches), since git does not keep track of them. When you removed the changes by performing a reset, they were lost.

The right way to achieve what you wanted would have been to use a git stash to temporarly save the changes, then do the checks you needed and a git apply to bring the changes back. Unfortunately, in your situation, there is nothing that can be done.

As a future advice, try your best not to use git reset --hard. There are better options. Another approach would have been to commit the changes, checkout to the old HEAD, do your thing and, if required, do a revert.


It actually is possible to recover from this situation. See related question with very interesting answers: Undo git reset --hard with uncommitted files in the staging area

链接地址: http://www.djcxy.com/p/4034.html

上一篇: 扔掉git中的本地提交

下一篇: 在重置之前撤消git reset到uncommited状态?