2

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.

Orbit
  • 2,985
  • 9
  • 49
  • 106
  • 1
    It couldn't have reset your other branch. They're completely unrelated, unless you ran the same command twice (once on each branch). What does `git log --graph --oneline --decorate` tell you? – Makoto Dec 25 '15 at 04:47
  • I ran the command in the terminal once while on the master branch, and it most definitely reset my other branch. git log shows this: http://prntscr.com/9i9zyv . The `f413cac` commit is the one I targeted for the reset. – Orbit Dec 25 '15 at 04:54
  • 1
    So right now, master and designredo point to the same location. Are you saying that, *before* this, they pointed to different locations? What does `git reflog` show you? – Makoto Dec 25 '15 at 04:55
  • git reflog: http://prntscr.com/9ia0po . Before the reset, I was on master with my uncommited test features. I created a new branch (so it was identical with master at that point, again no commits on this new branch). I then switched back from the new branch to master, and performed the reset to the last commit id I found. – Orbit Dec 25 '15 at 04:58
  • 1
    You basically reset master back to where it started from. The reflog is showing that master's changes are the same as designredo's changes. There's no evidence of a commit happening there. Are you **absolutely certain** that your test changes aren't in master now? The evidence here is fairly strong stating that either they are, or that the test changes don't exist anywhere. – Makoto Dec 25 '15 at 05:01
  • Yes, both `master` and the `designredo` branch (which is what i was referring to as testfeatures branch) look as they did when I did commit `f413cac` – Orbit Dec 25 '15 at 05:06

2 Answers2

3

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 (i.e. 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.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • So what your saying is, when I created the new branch, the new branch was created at the last commit point of master? Im using Android Studio so when you create a new branch, it looks like it copies everything exactly. You can write a comment in master and make a new branch with nothing commited and still see the comment if you switch between branches. – Orbit Dec 25 '15 at 05:06
  • Yes. When you create a new branch, 2 things happen: the committed code diverges on the new branch (so, if you add commits here, you won't see them on the other branch) but the uncommitted things are the same (i.e. git does not have the concept of modifications one a specific branch; they are just modifications to the code, that stay there until you do something with them). – Paul92 Dec 25 '15 at 05:11
0

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

Community
  • 1
  • 1
mkrufky
  • 3,268
  • 2
  • 17
  • 37