Git: “Not currently on any branch.” Is there an easy way to get back on a branch, while keeping the changes?

So I’ve done some work in the repository and when I’m about to commit I realize that I’m not currently on any branch.

This happens a lot when working with submodules and I am able to solve it, but the process is tedious and I’ve been thinking that there must be an easier way to do this.

  • Development and testing branch in @nvie's Git branching model?
  • git-svn: how to create a tag/branch for a sub project of SVN
  • Git branching model, what is the benefit of having a development branch?
  • Renaming remote branch on git and update log
  • How do I move a Git branch out into its own repository?
  • Folder converted to branch?
  • Is there an easy way to get back on a branch, while keeping the changes?

  • How to snapshot a software release (with GitHub)?
  • Should I add web libraries as git submodules for a project?
  • git: where is my configuration information coming from?
  • Git tag release version?
  • Git workflow using Puppet & Hiera
  • How to patch all changed files before a commit in Git
  • 9 Solutions collect form web for “Git: “Not currently on any branch.” Is there an easy way to get back on a branch, while keeping the changes?”

    If you have not committed:

    git stash
    git checkout some-branch
    git stash pop

    If you have committed and have not changed anything since:

    git log --oneline -n1 # this will give you the SHA
    git checkout some-branch
    git merge ${commit-sha}

    If you have committed and then done extra work:

    git stash
    git log --oneline -n1 # this will give you the SHA
    git checkout some-branch
    git merge ${commit-sha}
    git stash pop

    this helped me

    git checkout -b newbranch
    git checkout master
    git merge newbranch
    git branch -d newbranch
    git checkout master

    That’s result something like this:

    Warning: you are leaving 2 commits behind, not connected to
    any of your branches:
    1e7822f readme
    0116b5b returned to clean django
    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 1e7822f25e376d6a1182bb86a0adf3a774920e1e

    So, let’s do it:

    git merge 1e7822f25e376d6a1182bb86a0adf3a774920e1e

    Leaving another way here

    git branch newbranch
    git checkout master 
    git merge newbranch 

    Alternatively, you could setup your submodules so that rather than being in their default detached head state you check out a branch.

    Edited to add:

    One way is to checkout a particular branch of the submodule when you add it with the -b flag:

    git submodule add -b master <remote-repo> <path-to-add-it-to>

    Another way is to just go into the submodule directory and just check it out

    git checkout master

    I recently ran into this problem again. It’s been a while since I last worked with submodules and having learned more about git I realized that simply checking out the branch you want to commit on is sufficient. Git will keep the working tree even if you don’t stash it.

    git checkout existing_branch_name

    If you want to work on a new branch this should work for you:

    git checkout -b new_branch_name

    The checkout will fail if you have conflicts in the working tree, but that should be quite unusual and if it happens you can just stash it, pop it and resolve the conflict.

    Compared to the accepted answer, this answer will save you the execution of two commands, that don’t really take that long to execute anyway. Therefore I will not accept this answer, unless it miraculously gets more upvotes (or at least close) than the currently accepted answer.

    The following method may work:

    git rebase HEAD master
    git checkout master

    This will rebase your current HEAD changes on top of the master. Then you can switch the branch.

    Alternative way is to checkout the branch first:

    git checkout master

    Then Git should display SHA1 of your detached commits, then you can cherry pick them, e.g.

    git cherry-pick YOURSHA1

    Or you can also merge the latest one:

    git merge YOURSHA1

    To see all of your commits from different branches (to make sure you’ve them), run: git reflog.

    I know I told babay in 2012 that I thought it was unlikely that someone wouldn’t realize that they weren’t on a branch and commit. This just happened to me, so I guess I have to admit that I was wrong, but considering that it took until 2016 for this to happen to me, you could argue that it is in fact unlikely.

    Anyway, creating a new branch is overkill in my opinion. All you have to do is:

    git checkout some-branch
    git merge commit-sha

    If you didn’t copy the commit-sha before checking out the other branch, you can easily find it by running:

    git reflog

    For thoses who used repo and forgot about it, the magical command is :

    repo start -all origin

    “origin” is for original branch given by the manifest. you can change it for a specific branch.

    (I know this is not the exact topic but some like me may get here ^^)

    Git Baby is a git and github fan, let's start git clone.