Git之git checkout再理解及用法汇总

目录

前言

切换分支

放弃修改


前言

checkoutGit最常用的命令之一,我们常常使用它来切换分支,但是切换分支的时候总会遇到一些莫名其妙的问题导致切换失败,查询资料会发现它还有一些其他用法,虽然能够解决当前的问题,但是总会感觉有些用不明白,用不明白的原因应该是没有深度地、全面地理解它,所以要对它好好整理一下。

checkoutCVSSVN中都是检出的意思,从版本库检出一个版本,在Git中就不是这么简单了。手册上是这样介绍的:

git-checkout - Switch branches or restore working tree files

翻译这句话意思就是,checkout用于切换分支或者恢复工作树的文件。

那我们看下它的详细用法:

$ 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
    --recurse-submodules[=<checkout>]
                          control recursive updating of submodules
    --progress            force progress reporting

usage后面的or表示这个命令有两种用法,第一种是切换分支,第二种是撤销修改。

checkout 本意是检出的意思,也就是将某次commit的状态检出到工作区;所以它的过程是先将HEAD指向某个分支的最近一次commit,然后从commit恢复index,最后从index恢复工作区。
 

切换分支

创建并切换到新的分支,参考语法:

git checkout -b|-B <new_branch> [<start point>] 

git checkout branchname

如果不指定切换到哪个分支,那就是切换到当前分支

git checkout -b branchname 创建并切换到新的分支.
这个命令是将git branch newbranchgit checkout newbranch合在一起的结果。

这个时候分支是本地分支,并没有提交到服务器上去,如果这个分支已经被创建,这个命令会失败,这个时候,如果想要重置这个分支,需要使用-B参数。

(1)创造 index 和 工作区 都有变动的场景

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   t5.txt

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:   t2.txt
        
查看分支
$ git branch
  b1
* master

从 master 切换到 b1 
$ git checkout b1
error: Your local changes to the following files would be overwritten by checkout:
        t5.txt
Please commit your changes or stash them before you switch branches.
Aborting

只提到了 index 中的文件 t5.txt,那是不是我把他提交了就可以了呢?
$ git commit -m 'add'

$ git checkout b1
Switched to branch 'b1'
M       t2.txt

$ git branch
* b1
  master
  
$ git status
On branch b1
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:   t2.txt

no changes added to commit (use "git add" and/or "git commit -a")

由此可见 index 中的变动会阻止切换分支,那是因为一旦切换了,index 的变动就会消失;而工作区的变动会体现在切换后的分支上。


(2)重新创造 index 和 工作区 都有变动的场景,然后使用 -f 来强制切换分支呢?
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   t2.txt

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:   a/b/c/t3.txt

$ git checkout -f b1
Switched to branch 'b1'

$ git status
On branch b1
nothing to commit, working tree clean

切回 master 看一看
$ git checkout master
Switched to branch 'master'

$ git status
On branch master
nothing to commit, working tree clean

可见 -f 参数会强制切换分支,并清除掉 index 和 工作区的全部变动!!

那么,如果既要切换分支,又希望全部修改在切换后的分支中体现,那么只需要将 index 中的变动撤销即可(git reset HEAD filename),这样之前add进来的文件就成了Unstaged,然后切换分支即可。

放弃修改

如果不指定切换到哪个分支,那就是切换到当前分支,虽然HEAD的指向没有变化,但是后面的两个恢复过程依然会执行,于是就可以理解为放弃index和工作区的变动。但是出于安全考虑 git 会保持 index 的变动不被覆盖。

1、只放弃工作区的改动,index 保持不变,其实就是从当前 index 恢复 工作区:

放弃工作区中全部的修改

git checkout .

放弃工作区中某个文件的修改:

git checkout -- filename

先使用 git status 列出文件,然后 git checkout -- app/Http/Controllers/Read/Read3Controller.php

2、强制放弃 index 和 工作区 的改动:

git checkout -f

这是不可逆的操作,会直接覆盖,但是还是很有用的,有时候想放弃这些改动,使用svn的时候可以直接把文件删掉再update,但是使用git就不能这么操作,使用 git checkout 可以满足这一点。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕城南风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值