git学习之git checkout

要切换到一个已存在的分支,需要使用git checkout命令。 假设现在我们要从master分支切换到dev分支,则可以使用git checkou dev来实现,它的内部操作主要有两部分:

  • 让HEAD指向dev分支,
  • 将工作目录恢复成dev分支所指向的快照内容。

假设切换前是如下所示的状态:

则切换后会变成下面的状态

问题:在master分支上对文件做了修改之后再执行git checkout dev,当前工作目录中的修改会被保留吗?会!

或者说当master分支和dev分支指向同一个提交对象时,在master分支修改了文件,此时可以正常git checkout dev,并且文件的修改会被保留在当前工作目录中,但是当master分支和dev分支指向不同的提交对象时,在master分支修改了文件,则git checkout dev会报错。为什么会这样?还需要进一步了解git checkout的原理。

看看git checkout的帮助文档(git checkout --help)

GIT-CHECKOUT(1)                                                                 Git Manual                                                                 GIT-CHECKOUT(1)



NAME
       git-checkout - Switch branches or restore working tree files

SYNOPSIS
       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>]
       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
       git checkout [<tree-ish>] [--] <pathspec>...
       git checkout (-p|--patch) [<tree-ish>] [--] [<paths>...]


DESCRIPTION
       Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the
       specified branch as the current branch.

       git checkout <branch>
           To prepare for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local
           modifications to the files in the working tree are kept, so that they can be committed to the <branch>.

由上面的说明文档可知,git checkout <branch> 会将HEAD指向branch分支,并用branch分支所指向的提交对象来更新索引区和工作目录,但也会保留分支切换前工作目录中的修改,目的是为了在新分支上进行提交。

如果真的是这么简单,那应该不会出现下面这种情况了

➜  demo3 git:(master) ✗ git checkout dev
error: Your local changes to the following files would be overwritten by checkout:
	a.txt
Please commit your changes or stash them before you switch branches.
Aborting

为什么会报错?我确实在切换分支之前修改了a.txt文件,但上面的说明文档不是说会保留工作目录中的修改吗?不解,说明文档有问题,还是自己理解错了?大概率是后者。继续看文档,又找到下面一段说明:

-m, --merge
    When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are
    switching, the command refuses to switch branches in order to preserve your modifications in context. However, with this option, a three-way merge between the
    current branch, your working tree contents, and the new branch is done, and you will be on the new branch.

这是git checkout的-m选项,这段文字告诉我们当切换分支之前,如果你所修改的那个文件在两个分支所指向的提交对象之间存在差异,那么git checkout branch将会为了保护当前环境下的修改而拒绝切换分支,这也就是上面报错的原因。换言之,如果你所修改的那个文件在两个分支所指向的提交对象之间没有差异,那么git checkout branch将不会报错,并会对切换后的分支保留工作目录中的修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值