阅读这篇文章你将得到什么?

  • git 命令的帮助文档获取
  • git checkout 命令系统知识详解,所有参数哟
  • 怎么系统学习一个git 命令

根据git 的帮助一步步的撸下去,这样够全吧

获取参考手册

  1. 首先git checkout -h 获取快速参考
$ git checkout -h
usage: git checkout [<options>] <branch>or: git checkout [<options>] [<branch>] -- <file>...-q, --quiet           suppress progress reporting-b <branch>           create and checkout a new branch-B <branch>           create/reset and checkout a branch-l                    create reflog for new branch--detach              detach HEAD at named commit-t, --track           set upstream info for new branch--orphan <new-branch>new unparented branch-2, --ours            checkout our version for unmerged files-3, --theirs          checkout their version for unmerged files-f, --force           force checkout (throw away local modifications)-m, --merge           perform a 3-way merge with the new branch--overwrite-ignore    update ignored files (default)--conflict <style>    conflict style (merge or diff3)-p, --patch           select hunks interactively--ignore-skip-worktree-bitsdo not limit pathspecs to sparse entries only--ignore-other-worktreesdo not check if another worktree is holding the given ref--progress            force progress reporting

上面的帮助是快速参考还不够全面,通过下面命令进入全面的手册
2. 获取全面的手册

$ git checkout --help

执行上面命令会跳转到一个浏览器的页面中,很全面的手册

从上面的命令帮助中可以知道:主要有两个用法,切换分支和恢复WorkSpace文件

git checkout 命令系统知识详解

  • 切换分支
 git checkout [<options>] <branch>

切分支的命令,根据不同的options,有如下组合形式:

git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]##对应下面的1,2,3,4

开始撸不同的组合形式:

  1. 无声切,强制切,融合切

无声切:切分支会有一些信息提示,如果不需要可以使用不要提示的切分支,如下

$ git checkout develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.$ git checkout -q develop## 上面有输出信息,下面没有

强制切:如果本地有修改没有提交,切分支会报错,这个时候可以强制切分支,放弃本地修改 ,如下

$ git checkout develop
error: Your local changes to the following files would be overwritten by checkout:add.txt
Please commit your changes or stash them before you switch branches.
Aborting$ git checkout -f develop
Switched to branch 'develop'
Your branch is up-to-date with 'origin/develop'.## 上面切分支会报错,下面强制切分支可以切,但是会放弃本地修改

融合切:如果本地有修改,修改还需要保留,这个时候切分支就用融合切。合并的策略是本地修改,当前分支和要切的分支做三方合并,有冲突的话会有提示,需要解决冲突后再提交。

下面的本地有修改,从分支test切换到develop,最后有合并冲突

$ git status
On branch test
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified:   add.txtno changes added to commit (use "git add" and/or "git commit -a")$ git checkout -m develop
warning: LF will be replaced by CRLF in add.txt.
The file will have its original line endings in your working directory.
Switched to branch 'develop'
M       add.txt
Your branch is up-to-date with 'origin/develop'.$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Unmerged paths:(use "git reset HEAD <file>..." to unstage)(use "git add <file>..." to mark resolution)both modified:   add.txtno changes added to commit (use "git add" and/or "git commit -a")
  1. 按照上面切完,我还要分离头(HEDA)

上面的切分支操作,都会把HEAD指向要切的分支,如果不需要HEAD指向分支,只需指向分支对应的最后的commitid,使用如下命令

## 查看当前HEAD 的内容
$ cat .git/HEAD
ref: refs/heads/test## 查看当前分支最后一个提交的信息
$ git log -1 --pretty=oneline --decorate
25947cf707e1afb1851824412f5c70f5302b5d97 (HEAD -> test) server rebase## detach方式切换分支,切换到其他分支也一样,这里为了便于演示查看commit_id
$ git checkout --detach test
HEAD is now at 25947cf... server rebase## 再次查看当前HEAD 的内容,不再指向分支了,指向分支的最后一个提交commit_id
$ cat .git/HEAD
25947cf707e1afb1851824412f5c70f5302b5d97
  1. 分支切的位置太少,我还要切commitid

上面的切换都依赖分支,分支的本质是指向commit_id的引用,那么也就可以切commit_id

## 查看本地提交的信息$ git log --oneline --graph --decorate --all
* 93b6b6d (develop) commit all
* 56672d8 (origin/develop, origin/HEAD) client change
* 0c00cdf server add
* 8c09458 add content
* 20e18cb server change
* 153ee15 rebase client
| * d903c43  solve conflict
| * 5da6795 commit all
| * 01ae796 add
|/
* 25947cf (HEAD -> test) server rebase 2
* b76f324 server rebase
* a280114 add new
*   feb48b9 merge solve conflict
|\
| * d1e08ec add server content
* | f93ee54 add client content
|/
* ec7df6c add two
* c91a69d (origin/master, master) init## 切换到 d903c43
$ git checkout d903c43
Note: checking out 'd903c43'.You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:git checkout -b <new-branch-name>HEAD is now at d903c43... solve conflict## 查看最后一个提交
$ git log -1 --pretty=oneline --decorate
d903c433b2d6356e137710681109d7eadba351dd (HEAD, test1) solve conflict
  1. 原来有的位置切的不爽,还要新建分支切

上面所有的切分支是基于分支存在的情况,如果分支不存在咋办?新建分支再切,有没有一步到胃的方法,有。 如下:

$ git log -1 --pretty=oneline --decorate
25947cf707e1afb1851824412f5c70f5302b5d97 (HEAD -> test) server rebase 2$ git checkout -b newTest
Switched to a new branch 'newTest'$ git log -1 --pretty=oneline --decorate
25947cf707e1afb1851824412f5c70f5302b5d97 (HEAD -> newTest, test) server rebase 2## 查看当前分支指向,创建并切换到行分支newTest,再次查看并验证

上面是切换到一个新的分支,并且以当前分支为基础start_point,如果想在其他commit为起始点怎么办? 如下

$ git log -3 --pretty=oneline --decorate
25947cf707e1afb1851824412f5c70f5302b5d97 (HEAD -> newTest, tag: v1.0.1, tag: v1.0.0, test2, test, hotfix, future) server rebase 2
b76f324c3d7c0f192df00ab145718e66bf9d8b82 server rebase
a280114eb0dff901aa154c4c0772720374318ab7 add new$ git checkout -b from_a28011 a280114eb0dff901aa154c4c0772720374318ab7
Switched to a new branch 'from_a28011'$ git log -1 --pretty=oneline --decorate
a280114eb0dff901aa154c4c0772720374318ab7 (HEAD -> from_a28011) add new## 查看最近的三个提交,以a28011提交为基础创建分支并切换,查看结果

上面的切分支是在要切的分支不存在,如果存在就报错;有没有解决方法,有。这种方式在分支存在时,会重置分支到新的start_point;不存在和上面一样。

$ git log -2 --pretty=oneline --decorate
25947cf707e1afb1851824412f5c70f5302b5d97 (HEAD -> test, tag: v1.0.1, tag: v1.0.0, test2, newTest, hotfix, future) server rebase 2
b76f324c3d7c0f192df00ab145718e66bf9d8b82 server rebase$ git checkout -B test b76f324c3d7c0f192df00ab145718e66bf9d8b82
Reset branch 'test'$ git log -2 --pretty=oneline --decorate
b76f324c3d7c0f192df00ab145718e66bf9d8b82 (HEAD -> test) server rebase
a280114eb0dff901aa154c4c0772720374318ab7 (from_a28011) add new

创建一个分支并切换,不想在任何分支基础上创建,不要父母的孤儿分支

$ git checkout --orphan orphanBranch
Switched to a new branch 'orphanBranch'$ git log -2 --pretty=oneline --decorate
fatal: your current branch 'orphanBranch' does not have any commits yet## 看看孤儿分支啥都没有
  • 恢复WorkSpace文件
 git checkout [<options>] [<branch>] -- <file>...

恢复工作空间文件的命令,根据不同的options,有如下组合形式:

git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>…
git checkout [-p|--patch] [<tree-ish>] [--] [<paths>…]

开始撸不同的组合形式

指定从哪里恢复工作区文件,可以指定分支,commit 等,不指定就是index

[–] … 指定要恢复的文件,最好带上 “–” ,避免文件名和分支一样,造成切分支

  1. 放弃修改工作空间内容

git add 之前

$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified:   add.txtno changes added to commit (use "git add" and/or "git commit -a")$ git checkout -- add.txt$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working tree clean

git add 之后

$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes to be committed:(use "git reset HEAD <file>..." to unstage)modified:   add.txt$ git checkout HEAD -- add.txt$ git status
On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working tree clean
  1. 有冲突时选择恢复的策略
$ git status
On branch develop
Your branch and 'origin/develop' have diverged,
and have 1 and 1 different commits each, respectively.(use "git pull" to merge the remote branch into yours)
You have unmerged paths.(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)Unmerged paths:(use "git add <file>..." to mark resolution)both modified:   add.txtno changes added to commit (use "git add" and/or "git commit -a")$ git ls-files --unmerged
100644 76d4bb83f8dab3933a481bd2d65fbcc1283ef9b7 1       add.txt
100644 6b680f97bac0e45cc94bfa38eb9ddc6a457f6bc4 2       add.txt
100644 bda5aed1e6c6e9678222302fec58f448b3b279f9 3       add.txt## 这个时候不允许不带参数策略的恢复
$ git checkout -- add.txt
error: path 'add.txt' is unmerged## 恢复成本地的 对应stage #2 (ours)
$ git checkout --ours -- add.txt$ cat add.txt
add
add client## 恢复成远端的 对应stage #3 (theirs)
$ git checkout --theirs  -- add.txt$ cat add.txt
add
add server
  1. 有冲突时强制恢复

上面出现冲突不能恢复,需要用到强制恢复

## 不选择策略,不让恢复index
$ git checkout -- add.txt
error: path 'add.txt' is unmerged## 不选择策略,不让恢复index,强制也不行
$ git checkout -f -- add.txt
warning: path 'add.txt' is unmerged## 可以强制恢复HEAD
$ git checkout -f HEAD -- add.txt$ cat add.txt
add
add client
  1. 选择处理冲突策错了,重新恢复冲突
$ cat add.txt
add
<<<<<<< HEAD
add client
=======
add server
>>>>>>> 187e52c9b8b75fc6a8e969b57df18aeb3202e030$ git checkout --ours -- add.txt$ cat add.txt
add
add client$ git checkout -m -- add.txt$ cat add.txt
add
<<<<<<< ours
add client
=======
add server
>>>>>>> theirs
  1. 改变合并策略

有冲突的合并策略可以通过merge.conflictStyle 配置,默认值是"merge" 还可以设置为 “diff3”

$ cat add.txt
add
<<<<<<< ours
add client
=======
add server
>>>>>>> theirs$ git checkout --conflict=diff3  -- add.txt$ cat add.txt
add
<<<<<<< ours
add client
||||||| base
=======
add server
>>>>>>> theirs
  1. 打个补丁,选择某个分支下的一些文件差异打补丁,并且有交互式提示
$ git checkout -p test -- add.txt
diff --git b/add.txt a/add.txt
index 6b680f9..6903f8d 100644
--- b/add.txt
+++ a/add.txt
@@ -1,2 +1,4 @@add
-add client
+add two
+client add
+add server content
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]?
y - apply this hunk to index and worktree
n - do not apply this hunk to index and worktree
q - quit; do not apply this hunk or any of the remaining ones
a - apply this hunk and all later hunks in the file
d - do not apply this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]? y$ cat add.txt
add
add two
client add
add server content

怎么系统学习一个git 命令

  1. 获取帮助文档
  2. 查看快速参考中的usage知道命令的功能分类,查看详细帮助文档的SYNOPSIS大纲知道命令的组合形式
  3. 根据详细文档的解释,"猜测"命令的意思和验证
  4. 最后能输出一份文档(这一步最重要,能写出文档绝对收获满满)

总结:要学习一个命令的所有参数的用法,需要在不同的场景配合使用,不然会不起作用!

git checkout 命令所有参数用法详解相关推荐

  1. Oracle数据泵备份与恢复 命令 expdp/impdp 用法详解

    Oracle数据泵备份与恢复 命令 expdp/impdp 用法详解 关于expdp和impdp     使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户 ...

  2. JVM调优系列--Java命令选项(参数)--大全/详解/常用

    原文网址:JVM调优系列--Java命令选项(参数)--大全/详解/常用_IT利刃出鞘的博客-CSDN博客 简介 说明        本文介绍Java的java命令用法,包括:常用用法.选项大全. J ...

  3. Python必备基本技能——命令行参数args详解

    Python必备基本技能--命令行参数args详解 1. 效果图 2. 源码 2.1 简单命令行参数 2.1 轮廓检测源代码 参考 这篇博客将介绍一项开发人员.工程师和计算机科学家必备的技能--命令行 ...

  4. python 函数参数self_Python类中self参数用法详解

    Python编写类的时候,每个函数参数第一个参数都是self,一开始我不管它到底是干嘛的,只知道必须要写上.后来对Python渐渐熟悉了一点,再回头看self的概念,似乎有点弄明白了. 首先明确的是s ...

  5. Shell命令 getopts/getopt用法详解 命令行参数

    Shell命令行参数解析getopts/getopt用法详解 在Linux bash中,可以用以下三中方式解析命令行参数: 直接处理:使用$1.$2.$3- 进行解析 getopts:短选项的情况,例 ...

  6. linux shell命令行选项与参数用法详解

    问题描述:在linux shell中如何处理tail -n 10 access.log这样的命令行选项? 在bash中,可以用以下三种方式来处理命令行参数,每种方式都有自己的应用场景. 1,直接处理, ...

  7. chmod命令原理及用法详解

    Chmod命令主要用于修改.设置文件权限 chmod 修改文件权限主要有两种方式: 字母法与数字法 虽然数字法相对字母法简单,但是数字法是基于字母法,所以这里先介绍字母法. 1.字母法:chmod   ...

  8. chmod命令原理及用法详解(转)

    Chmod命令主要用于修改.设置文件权限 chmod 修改文件权限主要有两种方式: 字母法与数字法 虽然数字法相对字母法简单,但是数字法是基于字母法,所以这里先介绍字母法. 1.字母法:chmod   ...

  9. Outlook 2003命令行参数开关详解

    启动 Microsoft Outlook 的命令是 Outlook.exe.命令行参数开关是正斜杠后跟开关名和开关具有的任何参数. 开关的用法 查找 Microsoft Outlook 可执行文件 O ...

最新文章

  1. [设计模式随意链接]——命令模式
  2. XDR3020 WiFi6 11ax使用体验 11ax性能数据
  3. 【Qt】数据库实战(三)
  4. CSS clear 清除浮动,兼容各浏览器
  5. ASP.NET Core使用HostingStartup增强启动操作
  6. SaaS服务的私有化部署,这样做最高效|云效工程师指北
  7. php抛出和捕获异常,关于php:捕获和重新抛出异常的最佳实践是什么?
  8. 转载--卷积神经网络卷积层池化层输出计算公式
  9. vue插槽面试题_vue面试题(一)
  10. 网页形式的php抓取文件,PHP 抓取网页源文件
  11. C# 使用Emit深克隆
  12. 华为芯片鸿蒙的由来,华为“鸿蒙”真的来了!看完这些商标来历,网友们又激动了...
  13. Js isNaN()和Number.isNaN()、isFinite() 和 Number.isFinite()
  14. 湘江智能携手中联重科,工程机械产业智能化开启“星城速度”
  15. 狼与狗的本质区别:从打工到老板的突破
  16. 计算机视觉——全景图像拼接
  17. File的了解与使用
  18. 操作系统最坏适应最优适应最先适应
  19. 怎么能学会做买卖步骤是什么(想做买卖赚钱应该先从什么做起)
  20. 安卓开发工程师职业发展规划

热门文章

  1. ffmpeg源码优化之推流发送篇
  2. HDU2066:一个人的旅行(Dijkstra算法模板+多源多目的最短时间问题)
  3. 2022中国北京国际老龄产业博览会
  4. HDU2554 N对数的排列
  5. rnaseq 使用salmon之后的下游分析报错
  6. Java之有限状态机
  7. 【最通俗易懂】C#有限状态机
  8. 【剑指Offer】42. 连续子数组的最大和
  9. AAAI | 达摩院经典自适应多域联合优化训练框架及其应用
  10. 用Python写一个爬虫,爬取双色球开奖记录