Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Not currently on any branch" after doing a commit

Tags:

git

branch

commit

I did a checkout to an earlier commit:

git checkout 12345

Then back to the last commit:

git checkout 56789

And then continued committing and I'm:

Not currently on any branch.

Perhaps, I should've done:

git checkout master

After the first checkout, instead of pointing to a commit id.

Still, any idea how to get my latest commits into the master branch (which is a few commits behind)?

Thanks

like image 251
Albus Dumbledore Avatar asked Nov 25 '11 13:11

Albus Dumbledore


People also ask

How do I add a commit to a current branch?

Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want. "Cherry pick" the commits you want into this branch. Run this command: git cherry-pick super-long-hash-here . That will pull just this commit into your current branch.

Can you switch branches after commit?

If changing a branch would overwrite your uncommitted changes, Git will not let you switch. When you have work in progress and want to switch branches you should put it in the stash using git stash .

What happens when I commit in a branch?

Commits are permanent1 and unchangeable. So, nothing ever happens to any commit, as long as you can find it. Branches—or more precisely, branch names—in Git are temporary and highly changeable. Any branch name can be renamed, or even removed entirely, at any time.

Are commits tied to branches?

Git branches are just pointers to commits. They are not actually 'branches' at all, since don't contain nor group several commits: every branch just refers to a single commit.


1 Answers

When you do git checkout 12345 you will be in no branch state. Do not do that. This is meant for commit inspection rather than working in it.

If you are on master and want to reset your master to the commit that you wanted, use git reset 12345 ( or supply --hard ) If you wanted to branch, use git checkout -b <name> <sha1> to create a branch at that point and start working there.

Similarly while coming back, like you mentioned, you should have done git checkout master

Now that you have committed over 56789, note down the commit over 56789, and then checkout master, and do:

git reset <commit_over_56789>
like image 166
manojlds Avatar answered Nov 03 '22 01:11

manojlds