-1

My last commit is removed. My commands:

 git branch
* (detached from a96f277)
  help
  master
  omid
  reza
  rezalocal

git commit
is commited

git push
cant push

git checkout master
git merge master a96f277
: a96f277 is not found!

git checkout a96f277
git log
: my last commit not found;

This show my last commit and all my edits and file added is removed!

Omid Ahmadyani
  • 1,430
  • 13
  • 15
  • 1
    Possible duplicate of [What happens to git commits created in a detached HEAD state?](https://stackoverflow.com/questions/9984223/what-happens-to-git-commits-created-in-a-detached-head-state) – Adi Fatol May 07 '19 at 08:39

3 Answers3

1

You can't push because when you did your commit you were not on any branch. The commit you made takes a96f277 for its parent, but is not referenced anywhere and will be candidate for garbage collection.

And indeed git hints at this when you do commit on a detached HEAD state it outputs something of this form :

Warning: you are leaving 1 commit behind, not connected to any of your branches:

abcdef123 HEAD test

If you want to keep it by creating a new branch, this may be a good time to do so with:

git branch abcdef123

You should have checked out the branch on which to commit beforehand, but it's not too late since you can now just create a branch at this commit and merge it into master :

git checkout -b temp <commitHash>
git checkout master
git merge temp

(where <commitHash> is to be found in your recent ouput when you committed after a96f277) (if not in output, get it in your reflog)

Then you'll be able to push master as usual.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • i run your commands and temp branch created but when run git log in temp branch didnt show my last commit and its be removed. – Omid Ahmadyani May 07 '19 at 08:31
1

You committed [lets say hash is xyz123] on top of a96f277 and then switched to master. If you now go back to a96f277 by git checkout a96f277, your last commit xyz123 won't be there as you are pointing to its previous commit.

Now the commit xyz123 is dangling as there's no branch pointing to it. To get the commit id for this, you can do git reflog. It will give you history of all operations you did [As long as git's garbage collection cycle is not run].

You can then find the actual commit id for xyz123 and then checkout from it, create a new branch and push it to remote.

Hope this helps!

Krantisinh
  • 1,579
  • 13
  • 16
1

a96f277 is the start point of the detached HEAD and your commit is ahead of it.

To find out the hash of your commit, you can either search the output of git commit you have run or use git reflog.

After you find it, supposing it's abc123,

git checkout master
git cherry-pick abc123
# or
git merge abc123
ElpieKay
  • 27,194
  • 6
  • 32
  • 53