git checkout 命令所有参数用法详解

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

  • 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-bits
                          do not limit pathspecs to sparse entries only
    --ignore-other-worktrees
                          do 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.txt

no 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.txt

no 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.txt

no 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.txt

no 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. 最后能输出一份文档(这一步最重要,能写出文档绝对收获满满)

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值