git分布式版本控制系统权威指南学习笔记(五):git checkout

文章目录

  • 分离头指针
    • 通过cat可以查看当前的分支
    • 通过branch查看当前分支
    • checkout commitId(真正的,挽救分离头指针)
  • git checkout 用法

分离头指针

涉及到的命令
cat .git/head
git branch
git checkout commitId
git reflog

通过cat可以查看当前的分支

➜  demo git:(master) cat .git/HEAD
ref: refs/heads/master

通过branch查看当前分支

➜  demo git:(master) git branch -v
  b      81b09ec add
* master 1e20fe7 a

checkout commitId(真正的,挽救分离头指针)

经过checkout commitId之后,当前的头指针是游离状态的。

//ed97a5e3d4a是某次提交的hash值
➜  demo git:(8e0f16b) git checkout ed97a5e3d4a
//会有一个警告,当前在一个游离的头指针上
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  8e0f16b dfg

If you want to keep it by creating a new branch, this may be a good time
to do so with:
 git branch <new-branch-name> 8e0f16b
➜  demo git:(ed97a5e) git branch
//在例子中这里是*(no branch)但是没有复现。
* (HEAD detached at ed97a5e)
  b
  master
 //查看第一条reflog
➜  demo git:(ed97a5e) git reflog -1
ed97a5e (HEAD) HEAD@{0}: checkout: moving from master to ed97aa
➜  demo git:(ed97a5e) git checkout master
//大概意思是,你已经有了一个commit 但是不隶属于任何一个分支,假如想要
//这次提交的话,可以采用git branch new_branch_name 4700da(此处省略hash值)的方式。
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  91a80cc dsaf

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

 git branch <new-branch-name> 91a80cc

Switched to branch 'master'
//上面是希望通过checkout改变commit历史的,这里直接gitmerge回来
➜  demo git:(master) git merge 91a80cc(这里是刚才checkout的commit的id)
Already up to date.
➜  demo git:(master) git cat-file -p HEAD
tree acefb39d45e5cc33c3f4889b0d150c3bb6983f56
//看到这里有两个parent
parent 1e20fe7563a4b4bf8a091fe6c55ca9f32786224c
parent 91a80cc6cc69cf2c2438b667964ff81b8e99587b
author dataiyangu <32248827@qq.com> 1552348529 +0800
committer dataiyangu <32248827@qq.com> 1552348529 +0800

sdf

git checkout 用法

撤销本地工作区的修改,前提是没有被git add.
gitcheckout .
gitcheckout fiel
gitcheckout 目录

你可能感兴趣的:(#,Tool,------,Git)