sourcetree - git :I want a committed in head

66 views Asked by At

I am a newbie with git. I have commited some changes. I made a mistake and I didn't commit it in any branch and it is in head. I would like to commit this in a branch.

If I run git reflog show I can see that it is in head(7) with a code 15a3e43

If I run this, then I'm in the last element of head git chechout HEAD^

if I run git chechout -b branchA branchA is created and the last is moved to this branch

I have tried to get to head(7) git checkout HEAD@{1} git chechout 15a3e43 and no way. I would like to move the commit of head(7) to a branch.

Thanks, Joan

3

There are 3 answers

0
uraimo On BEST ANSWER

If you want to add a specific commit to another branch, you can use cherry-pick:

  1. Move to that other branch
  2. git cherry-pick <commit_hash>

A new commit with the content of <commit_hash> will be created.

0
ckruczek On

Imagine you have the following commit graph

A - B - C - D - E - F - G - H master

And you want commit D-H to go to a different branch. What you do is the following:

git branch newBranch
git reset --hard C

A - B - C master
         \
          \
           D - E - F - G - H newbranch

Keep in mind, that this is harmful if you have already pushed everything you miss-commited.

0
David Deutsch On

Assuming that 15a3e43 is the hash of the last commit you made, you can merge it (and previous commits) into a branch with:

git checkout branchname
git merge 15a3e43