Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Switch from Head to Master cause to disappear of part of the project

Valeriy Minenko August 3, 2016

Hello.

I have a such a problem. I worked on project, commit some stuff (I use SourceTree + git). Then I noticed that I commit it to HEAD brunch, not to Master one. Don't have an idea what is HEAD. I decided to switch to master and merge changes to it from HEAD. And after that all my commits just disappear. Is there any way to get them back?

I was pretty sure that everything that is commited can't disappear whenever I do sad

Thanks

2 answers

1 accepted

23 votes
Answer accepted
thinsoldier
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 3, 2016

In command line do: git reflog

That will list all commits that were used as HEAD recently.

If you see a commit that looks like last one in the series of commits that have disappeared, copy it's hash.

Then do: git tag emergency <the hash you copied>

You should now see a tag in sourcetree named "emergency" all the commits you couldn't find will be reachable from that tag. You can make a new branch from there and merge it with master.

---

The only way something might disappear permanently with git is if it has no label.

tags are a kind of label

branches are a kind of label

If there's no branch name or tag name pointing to the last commit and the only label it has is HEAD then they will disappear when you move HEAD to somewhere else by checking out something else.

Luckily `git reflog` lets you see places that were recently checked out as the location of HEAD. And you can use that info to tag the commits you lost so they have a label. 

Commits with no label of any kind will eventually be garbage collected and disappear forever.

---

Sourcetree gives you a clear warning about this problem you are experiencing when you check out something that is not a branch:

> Are you sure you want to checkout 'd2922f3828241dfe38ece99fc5ce5ce231c6b069'?

> Doing so will make your working copy a 'detached HEAD', which means you won't be on a branch anymore. If you want to commit after this you'll probably want to either checkout a branch again, or create a new branch. Is this ok?

You did not heed or read the warning.

Valeriy Minenko August 4, 2016

Hey

It worked! Thank you so much! You really saved me!

Andy Stutzmann April 16, 2017

You're a lifesaver! Thanks! 

Jake Brantley July 23, 2018

Lifesaver dude

blackie1019 August 31, 2018

@thinsoldier Thanks for your explain and it fixed my problem. But can explain more why commits from HEAD branch to empty label(no branches and no tags belong to)?

Thank you so much!

Christof Schwenzfeier April 4, 2019

Lifesaver for sure ;)

Amshumaan Rakshith May 21, 2019

May you live for a hundred years. You just saved my whole night's work.

Greg Tran December 16, 2020

THANK YOU!!!! <3

harikeerthipati February 12, 2021

Thank you so much man. I can't explain you my happiness after I got back the code in which I spent 3 days. Life savier.

johnhuan1 February 22, 2021

Great! You saved my life!

0 votes
Tim Crall
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 3, 2016

HEAD is a an automatic tag that refers to whatever commit you have checked out right now.  It is not a branch.

It is possible to undo a commit completely, but difficult to do it by accident.  A screenshot and/or a command history would help.

Were you using the command line or Source Tree? 

 

Probably you were working on a branch other than master, and the commits are still there on master.  Make sure you have 'show all branches' checked if you're in SourceTree.  

 

If you're at the command line, do a "git log --oneline --all --decorate --graph" command to see what's going on.

Darren Woodhouse April 1, 2019

Why dows sourcetree allow commits to nothing?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events