Recover a branch in Git

Asked

Viewed 146 times

1

I checked out an old commit from an application and the following message appeared:

You are in 'Detached HEAD' state. You can look Around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:

git switch -c

Or undo this Operation with:

git switch -

Turn off this Advice by Setting config variable Advice.detachedHead to false

HEAD is now at 4bf36b5 Created

In this commit there are two branches, Master and another that I created some new features. When I enter this branch with the changes are all there, but they are not yet in Master.

When I give a git status appears to me this message:

HEAD Detached at 4bf36b5 Nothing to commit, Working Tree clean

There is nothing to commit in any branch and when I try to merge Feature to master this appears:

$ git merge featureTransactions Already up to date.

Can someone give me a hint of what’s going on? I’m needing to move up these changes and push it on github to replace what’s there because it broke my application some changes I made.

  • Try, before trying to merge, to check out the master: git checkout master and then git merge branchDaFeature. What happens?

  • $ git merge featureTransactions Already up to date.

  • This means that all modifications of the featureTransactions are already contained in the master. Which leads you to believe that?

  • Only the changes are in the other branch but not in the master.

1 answer

1

When the message appears HEAD detached at 4bf36b5 nothing to commit, working tree clean you are free to create a new branch, at the point where you are - just give the command git branch <nome_da_nova_branch> , and can upload this new branch to the repository with git push -u origin <nome...> - Ready, then you already guaranteed yourself against losses.

So if the idea is to put together in master what’s there, is, being in that branch git merge master - will update your Feature branch with what was developed in master. Optionally you could do "rebase" instead of merge - but if you moved files that were moved in master, you will get conflicts that can be boring to solve. I don’t think I’d have much of an advantage in this case.

Well, done merging with the master, test your branch, if it’s working ok, and with the Features you wanted to include, just switch to the master, and do it again git merge <nome...> (this step can be done by the github/gitlab/etc... web interface via pull request, if you prefer)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.