-3

I commit often my change with

git add *

and

git commit -m "message"

There is two week I discover branch so I create a branch and code in that branch. Now i exec git checkout master and I lose lot of file(there is file that were before branch create that were deleted) It's ruin 1 year of work. After checking branch master my branch named test were erased(I don't how is it possible) I've checked with recuva the file dispaer totally. (sorry for my english) Solved by @underscore_d:

1) git reflog

I obtain a list of last HEAD

c92fba6 HEAD@{0}: checkout: moving from bc679831379ae324856d02492aecdeb0abffff8d
 to master
bc67983 HEAD@{1}: commit: bug fixe #32
fcaccea HEAD@{2}: commit: bug fixe #31
009579a HEAD@{3}: commit: bug fixe #30

so I type

2) git checkout bc67983

And Iget all my file back.

Et7f3XIV
  • 562
  • 1
  • 7
  • 20
  • google `git reflog`. search Stack Overflow: https://stackoverflow.com/questions/2961240/get-back-the-changes-after-accidental-checkout https://stackoverflow.com/questions/30643119/git-checkout-recover-lost-files https://stackoverflow.com/questions/3601911/how-do-i-undo-a-checkout-in-git https://stackoverflow.com/questions/7147680/accidentally-reverted-to-master-lost-uncommitted-changes – underscore_d Oct 25 '17 at 15:16
  • I have also git gui and git gui say that all is fine. Dot mean local fonder and asterisk mean all file in folder. – Et7f3XIV Oct 25 '17 at 15:38
  • It is _tremendously_ unlikely that `checkout` deleted the previous `HEAD` branch, or that objects you previously added to `git` are now immediately lost and not still referenced by some branch or dangling commit. Post the output of `git branch` and (the first 25 or so) lines of `git reflog`. – underscore_d Oct 25 '17 at 15:42
  • A big thanks @underscore_d post in answer that I can mark you as accepted. I'am editing my original post to explain how I solve It – Et7f3XIV Oct 25 '17 at 15:48
  • What you should now do is to `git reset` some branch to point at the latest commit you're recovering, otherwise I think you'll still be on a `detached HEAD` and will probably get into the same problem again later... – underscore_d Oct 25 '17 at 16:01

3 Answers3

0
git checkout master 

changes your working copy to the master branch - your other branch is still there, you working copy just has to be changed back to it.

git checkout <branchname> should do it - after that you need probably to merge your branches content to your master. git help checkout git help merge should help you further.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

You checked out master, so git reverted your work tree to the state of the last commit to master. If you want to return to a state reflecting everything you committed to the branch, check out the branch. (You didn't say what the branch is named; I'll suppose it's my_branch.)

git checkout my_branch

If you want to incorporate the changes you made on the branch into master, you can merge.

git checkout master
git merge my_branch

In the future, you might want to understand a feature, rather than just "discover" it, before trying to use it.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
0

So, it sounds like, rather than committing to a branch, you were committing to a detached HEAD. Thus, checking out master meant that you 'lost' the reference to the commit(s) that you had been working on.

But all is not lost. git keeps a log of the refs that it has seen, and these won't be lost forever until garbage-collection runs and deletes things that have been orphaned outwith the configured retention period. You can view this by running git reflog.

And thankfully, by doing so, as you saw, even commits that don't belong to any branch are still retained in git's database, and you can check them out.

What you should now do is to git reset some branch to point at the latest commit you're recovering, otherwise you'll still be on a detached HEAD and will probably get into the same problem again later... So, if you want master to point at your recovered commit, run git checkout master && git reset --hard [the hash]. Or create a new branch from the detached HEAD with git checkout -b phew_that_was_a_close_one.

underscore_d
  • 6,309
  • 3
  • 38
  • 64