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

3

93

EGit strikes again. I made the mistake of trying to switch to a different branch in EGit and it somehow messed up and checked out no branch. I then made a commit to this non-branch, and then when I realized I wasn't tracking the right branch, I ran the following:

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

    bada553d My commit message

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

    git branch new_branch_name ....

Branch issue2 set up to track remote branch issue2 from origin.
Switched to a new branch issue2. 

Now that I've botched things, how do I associate that commit with my current branch? I'm not interested in creating a brand new branch, I just want to pull that commit into my branch, issue2.

Wearable answered 16/1, 2013 at 19:1 Comment(3)
Possible duplicate of What happens to git commits created in a detached HEAD state? – Odo
How can you be on "no branch" anyway...? – Vaporescence
@Vaporescence πŸ˜‚ when people like me use git in weird ways. Fortunately StackOverflow saves my bacon again! – Tucky
P
141

you can git cherry-pick bada553d if it's just the one commit.

You can also reference anywhere you've been by using the reflog:

git reflog

then use one of those commits:

git checkout -b temp HEAD@{3}

to checkout and make a branch temp from where your current commit was 3 "times" ago. It's a bread crumb of where you used to be.

Placidia answered 16/1, 2013 at 19:5 Comment(3)
How to delete commit like this which is not required? – Scrooge
If you don't give it a name, like a branch or tag, it'll eventually be deleted by git's garbage collector. – Impenitent
For multiple commits you can cherry pick a range using git cherry-pick ebe6942^..905e279 – Andee
K
20

If you just want to associate that commit with your current branch, you can simply do this

  1. Run this command to create a new branch with that commit

    git branch temp {commit's SHA}

  2. Then simply merge this commit with your current branch

    git merge temp

  3. Now just delete the temporary new branch we created

    git branch -d temp

Kennethkennett answered 8/2, 2022 at 20:40 Comment(1)
Perfect, thanks so much. IMO this is more straightforward than the accepted answer. – Bluecollar
C
0

Also you can just apply tag to your commit after using git reflog when you'll know commit's hash:

 Git tag <tag's_name>  <commit's_hash>/HEAD@{<commit's_num>} 
Congress answered 2/9, 2021 at 19:40 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.