git - "git checkout - . "和 "git checkout -- ."之间的区别

标签 git

在使用“git checkout”时,我对这两个选项感到有点困惑

我总是使用 git checkout -- . 来清除我的工作目录。

但今天当我错误地输入 git checkout - . 时。我没有发现来自 git 的错误警报。

我已经阅读了 git 文档,也不知道这个 - 选项是做什么的。这很难用谷歌搜索。

那么请问有人对此有任何想法吗?

最佳答案

使用git checkout时,可以使用-作为@{-1}的简写。
来自man git-checkout(强调第二段):

<branch>
    Branch to checkout; if it refers to a branch (i.e., a name that, when
    prepended with "refs/heads/", is a valid ref), then that branch is checked
    out. Otherwise, if it refers to a valid commit, your HEAD becomes
    "detached" and you are no longer on any branch (see below for details).

    As a special case, the "@{-N}" syntax for the N-th last branch/commit
    checks out branches (instead of detaching). You may also specify - which is
    synonymous with "@{-1}".

通过在分支 master 中创建一个文件,在另一个分支中修改它,并使用来自 master 的 checkout - 来在一个空的 repo 中进行尝试:

$ git init
Initialized empty Git repository in workspace/git-test/.git/

git:(master)  $ echo a > a
git:(master*) $ git add a 
git:(master*) $ git commit
[master (root-commit) 8433343] Add a to master
 1 file changed, 1 insertion(+)
 create mode 100644 a

git:(master)  $ git checkout -b other
Switched to a new branch 'other'

git:(other)   $ echo b > a
git:(other*)  $ git add a 
git:(other*)  $ git commit
[other be2298f] Replace a by b
 1 file changed, 1 insertion(+), 1 deletion(-)

git:(other)   $ git checkout -
Switched to branch 'master'

git:(master)  $ git checkout -- . # no changes (no * next to master in the line below)
git:(master)  $ git checkout - . # checking out files from alternate branch 'other'
git:(master*) $ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   a

git:(master*) $ cat a
b

关于git - "git checkout - . "和 "git checkout -- ."之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44001013/

相关文章:

git - git 什么时候真正验证提交链的完整性?

git - 无法登录gitolite服务器进行克隆,但可以通过ssh连接

git - 如何在 Gitlab CI 5.4+ 上执行特定分支/标签的作业?

asp.net - 如何 merge 并提交到 TeamCity 中的分支

git - 带有 git 子模块的 Docker 项目结构

文件上传的 GITHub API 问题

Git - 处理混合编码文件名

git - 文件超出了 git 的大小,即使它已缩小到限制以下

linux - 如何针对 Kerberos 跳过 Git 的密码输入?

git - 如何在 Git 中重定位