简体   繁体   中英

How to ensure the last commit(s) will not get lost after git checkout?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM