Reputation: 1184
Before git checkout I'd like to ensure that my last commit(s) is not orphan and not get lost.
The commit(s) may get lost in case of detached HEAD for example. The fact of loosing the commits is nicely reported after finishing "git checkout":
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
8fdb039 Message log for lost commit 2
d8edb79 Message log for lost commit 1
...
Switched to branch 'master'
But, I'd like to check BEFORE running 'git checkout'. --dry-run option for git checkout would be a kind of solution (git 1.7.9.5 doesn't have such option).
Need an option to abort git checkout in case some commit(s) going to be lost. Or an option to quick check if the last commit(s) is orphan.
Upvotes: 3
Views: 656
Reputation: 60323
No need to be so cautious, when you see the "leaving commits behind" message just tag the history you left behind.
git tag some-useful-name @{-1}
There's lots of ways to refer to commits, and everything you've done in the last month stays in your repository. Git doesn't, ever, lose commits.
Upvotes: 0
Reputation: 2436
When you are in a detached HEAD, git branch
will give * (no branch)
, and git status
will give # Not currently on any branch.
Note that git has been conceived to make branching easy, so you should avoid working in a detach HEAD state and only use this state for "inspection and discardable experiments". When you start modifying anything, simply creates a new branch with git branch non_existing branch
to create a branch before checking out.
If you left without creating a branch, your commit is not necessarily lost. You can retrieve the ID of the commit with git log -2 HEAD
and then return to it with git checkout ID
to create a branch
Upvotes: 4