git的使用(三)checkout

checkout的一般作用都会改变工作区域,可能比reset还常用

检出命令git checkout是git最常用的命令之一,同时也是一个很危险的命令,因为这条命令会重写工作区。检出命令的用法如下:

用法一:git checkout [-q] [<commit>] [--] <paths>...

用法二:git checkout [<branch>]

用法三:git checkout [-m] [[-b]--orphan] <new_branch>] [<start_point>]

上面列出的第一种用法和第二种用法的区别在于,第一种用法在命令中包含路径<paths>。为了避免路径和引用(或者提交ID)同名而发生冲突,可以在<paths>前用两个连续的短线(短号)作为分隔。

第一种用法的<commit>是可选项,如果省略则相当于从暂存区(index)进行检出。这和上一章的重置命令大不相同:重置的默认值是HEAD,而检出的默认值是暂存区。因此重置一般用于重置暂存区(除非使用--hard参数,否则不会重置工作区),而检出命令主要是覆盖工作区(如果<commit>不省略,也会替换暂存区中相应的文件)。

第一种用法(包含了路径<paths>的用法)不会改变HEAD头指针,主要是用于指定版本的文件覆盖工作区中对应的文件。如果省略<commit>,则会用暂存区的文件覆盖工作区的文件,否则用指定提交中的文件覆盖暂存区中和工作区中对应的文件。

第二种用法(不使用路径<paths>的用法)则会改变HEAD头指针。之所以后面的参数会写作<branch>,是因为只有HEAD切换到一个分支才可以对提交进行跟踪,否则仍然会进入“分离头指针”的状态。在“分离头指针”状态下的提交不能被引用关联到,从而可能丢失。所以用法二最主要的作用就是切换到分支。如果省略<branch>则相当于对工作区进行状态检查。

第三种用法主要是创建和切换到新的分支(<new_branch>),新的分支从<start_point>指定的提交开始创建。新分支和我们熟悉的master分支没有什么实质的不同,都是在refs/heads命名空间下的引用。




至于checkout和reset hard的区别,主要体现在history上,对工作区都一样,下面是详细的解释。

This answer is mostly quoted from my answer to a previous question: git reset in plain english.

The two are very different. They result in the same state for your index and work tree, but the resulting history and current branch aren't the same.

Suppose your history looks like this, with the master branch currently checked out:

- A - B - C (HEAD, master)

and you run git reset --hard B. You'll get this:

- A - B (HEAD, master)      # - C is still here, but there's no
                            # branch pointing to it anymore

You'd actually get that effect if you use --mixed or --soft too - the only difference is what happens to your work tree and index. In the --hard case, the work tree and index match B.

Now, suppose you'd run git checkout B instead. You'd get this:

- A - B (HEAD) - C (master)

You've ended up in a detached HEAD state. HEAD, work tree, index all match B, same as with the hard reset, but the master branch was left behind at C. If you make a new commit D at this point, you'll get this, which is probably not what you want:

- A - B - C (master)
       \
        D (HEAD)

So, you use checkout to, well, check out that commit. You can fiddle with it, do what you like, but you've left your branch behind. If you want the branch moved too, you use reset.


### git checkout 使用方法、语法及示例 `git checkout` 是 Git 中一个非常重要的命令,主要用于切换分支或恢复工作目录中的文件。以下是关于 `git checkout` 的详细说明: #### 1. 切换分支 当需要从当前分支切换到另一个分支时,可以使用以下语法: ```bash git checkout <branch_name> ``` 例如,如果要切换到名为 `dev` 的分支,可以运行以下命令[^2]: ```bash git checkout dev ``` #### 2. 分离头指针(切换到特定提交) 分离头指针是指将 HEAD 指向一个具体的提交记录,而不是指向某个分支。这在查看历史记录或进行实验性开发时非常有用。其基本语法为: ```bash git checkout <commit_id> ``` 例如,如果要切换到提交 ID 为 `abc123` 的提交,可以运行以下命令[^1]: ```bash git checkout abc123 ``` #### 3. 创建并切换到新分支 可以通过以下命令创建并切换到一个新的分支: ```bash git checkout -b <new_branch_name> ``` 例如,创建并切换到名为 `feature-xyz` 的新分支: ```bash git checkout -b feature-xyz ``` #### 4. 恢复工作目录中的文件 如果需要恢复某个文件到最近一次提交的状态,可以使用以下语法: ```bash git checkout -- <file_path> ``` 例如,恢复 `README.md` 文件到最近一次提交的状态: ```bash git checkout -- README.md ``` #### 5. 关联远程分支并切换 如果需要创建本地分支并关联远程分支,可以使用以下语法: ```bash git checkout -b <local_branch_name> <remote>/<remote_branch_name> ``` 例如,创建本地分支 `dev` 并关联远程分支 `origin/dev`: ```bash git checkout -b dev origin/dev ``` 推荐使用更安全的 `git switch` 命令: ```bash git switch -c dev -t origin/dev ``` #### 6. 更新子模块指针 如果项目中包含子模块,并且需要更新子模块的指针,可以参考以下步骤[^3]: ```bash git commit -m "Changed checked out revision in submodule" git push ``` #### 注意事项 -使用 `git checkout` 切换分支时,确保工作目录没有未提交的更改,否则可能会丢失这些更改。 - 如果需要更安全的操作,可以考虑使用 `git switch` 命令来代替 `git checkout`。 ### 示例代码 以下是一些常见的 `git checkout` 使用场景: #### 切换到现有分支 ```bash git checkout main ``` #### 创建并切换到新分支 ```bash git checkout -b new-feature ``` #### 恢复文件到最近一次提交状态 ```bash git checkout -- path/to/file.txt ``` #### 切换到特定提交 ```bash git checkout abc123 ``` #### 创建本地分支并关联远程分支 ```bash git checkout -b dev origin/dev ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值