gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:48:06 29 4
gpt4 key购买 nike

在使用“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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com