原帖收藏于 IT 老兵驿站 https://www.oxysun.cn/git/git-checkout-2.html , 传递一个 IT 老兵在凋零前的光和氧.
前言
结合前一篇文章, 再认真总结一下 git-checkout 的用法, 因为可能一次总结到不了位, 那么就不怕啰嗦, 不怕重复, 多总结几次, 这样可能会造成每篇文章内容之间的分布不是那么清晰, 将来再做更好的整理吧, 因为现在这种方式, 对于当前的学习是有帮助的, 是从浅入深的.
正文
概要
git-checkout - Switch branches or restore working tree files
从上面可见, git checkout 是用来切换分支或者回复工作目录的, 看到这里, 记住这一点, 会有很大帮助.(备注: 我发现, 有的时候, 学习的节奏放慢一些, 反而其实是更有效率的, 反而是更快的. 多思考一些, 记的更准确, 因慢得快)
语法
- 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>...?]
用法 1
git checkout <branch>
用来切换到一个分支上. 切换 index 和工作目录, 还有 HEAD 指针到这个分支上. 本地发生的修改也会被保留. 如果本地不存在这个分支而远程存在同名分支的话, 则这个命令相当于:
- $ git checkout -b <branch> --track <remote>/<branch>
- git checkout -b|-B <new_branch> [<start point>]
-b 表示创建新分支; 如果分支存在的话, 不进行任何处理.
-B 在创建新分支的功能和 - b 是一样的; 但是, 如果分支存在的话, 它会重置 < start_point>.
- Specifying -b causes a new branch to be created as if git-branch[1] were called and then checked out. In this case you can use the --track or --no-track options, which will be passed to git branch. As a convenience, --track without -b implies branch creation; see the description of --track below.
- If -B is given, <new_branch> is created if it doesn't exist; otherwise, it is reset. This is the transactional equivalent of
- $ git branch -f <branch> [<start point>]
- $ git checkout <branch>
- that is to say, the branch is not reset/created unless "git checkout" is successful.
这里涉及到 track 和 < start point > 的概念.
track 表示的是远程仓库与之对应的分支, 这个信息被称为 upstream, 上游, 远程仓库的, 是上游. 本地的, 是下游, 有一个对应的关系. track 本意是轨迹, 跟踪的意思, 使用了 --track 或者 --no-track 来设置这个, 这个信息会传递给 git branch.
用法 2
- git checkout --detach [<branch>]
- git checkout [--detach] <commit>
切换代码到某一个提交号或者分支上, 并且分离了 HEAD 指针, 指向了这个提交. 这块有点复杂, 还需要理解深度理解一下, 这个可能要留到下一篇帖子来完成了, 争取每天整理一些 (2018-08-03).
整理完成, 可以参考这里 https://www.oxysun.cn/git/git-DETACHED-HEAD.html (2018-08-04).
用法 3
- git checkout [<tree-ish>] [--] <pathspec>...?
- git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...?]
从 index 或者 < tree-ish> 检出代码来替换 < pathspec> 处的代码. 如果 < tree-ish> 被指定了, 那么 index 和工作空间的代码都会被更新.
The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out. Using -f will ignore these unmerged entries. The contents from a specific side of the merge can be checked out of the index by using --ours or --theirs. With -m, changes made to the working tree file can be discarded to re-create the original conflicted merge result.
index 区域可能还有一些没有 merge 的条目, 因为之前有失败的 merge.
后一种用法使用互动的方式来完成这个功能.
到此, 这个命令的用法基本整理完, 下一步, 要实践一些实例.
来源: http://www.bubuko.com/infodetail-2713534.html