Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge changes on detached branch after git checkout <commit hash>

Tags:

git

github

I was originally working on git branch named "feature". Then ,I commited my changes .

Then I noticed there are some new created files shouldn't be in my commit. So I found the lastest commit hash 1b33aa (I use git log command).

After that, I did:

git checkout 1b63aa

Then I made more changes (I though I was still on feature branch...my mistake), and commited again.

Then I run git branch ,it prints out :

* (detached from 1b33aa)
  feature

How can I merge back those changes I just made to feature branch now?

like image 693
Leem.fin Avatar asked Dec 19 '13 17:12

Leem.fin


People also ask

Does merge change commit hash?

A merge that makes no changes at all to the top level will therefore have the same top-level tree ID as one or more of its parent commits. But it's still a new commit, a new object with a new ID, containing two or more parent IDs, a tree ID, an author and committer, and a log message.

How do I fix a detached committed head?

If you want to keep changes made with a detached HEAD, just create a new branch and switch to it. You can create it right after arriving at a detached HEAD or after creating one or more commits. The result is the same. The only restriction is that you should do it before returning to your normal branch.

Should I commit my changes before merging?

The “commit your changes or stash them before you can merge” error is raised when you try to pull code from a remote repository that conflicts with a local change you have made to a repository. To solve this error, either commit your change to the repository, discard your change, or stash your change for later.


1 Answers

You are on a detached branch now. It seems you want to replace your feature branch with this one. A safe way to do that is to rename feature to something else and then turn the current branch into a proper branch named feature:

git branch -m feature feature-bak
git checkout -b feature

In your first step, I think you wanted to do a git reset instead of a git checkout:

git reset 1b63aa

On the other hand, if you don't want to replace the feature branch but merge the changes in the current branch to feature, you can do like this:

git checkout -b temp
git checkout feature
git merge temp
like image 195
janos Avatar answered Nov 14 '22 20:11

janos