首页 > web前端 > js教程 > 正文

放弃 Git Checkout:改用 Git Switch 和 Git Restore

Barbara Streisand
发布: 2024-10-14 13:28:05
原创
797 人浏览过

如果你在日常工作中使用Git,git checkout是一个常用的命令。它经常用于切换分支,如果你查看文档,你可以看到短语“切换分支或恢复工作树文件”。但 UNIX 应该是做一件事并且把它做好。这很令人困惑,因此 Git 2.23 带来了一对命令来取代它。


在介绍其工作原理之前,我们需要先简单了解一下其相关的 Git 概念。

  • 工作副本(工作树文件):指存储库中出现在硬盘上的文件。

  • 索引(暂存区或缓存):它指的是你有 git add-ed,或者,如果你运行 git commit 将提交什么。

  • HEAD:指的是“当前”或“活动”分支,当我们需要检出一个分支时(指的是你尝试将该分支与工作副本中的内容进行匹配),只有一个可以一次检查一次。

git checkout 可以签出一个分支或创建一个新分支并签入其中:

# Switched to branch 'test'
$ git checkout test

# Switched to a new branch 'test'
$ git checkout -b test

# Switch back to the previous branch
$ git checkout -

# Switched to a commit
$ git checkout master~1
登录后复制

而git switch是用来接管分支相关的,所以它还可以做:

# Switched to branch 'test'
$ git switch test

# Switched to a new branch 'test'
$ git switch -c test

# Switch back to the previous branch
$ git switch -

# Switched to a commit
$ git switch -d master~1
登录后复制

正如我们一开始所说,git checkout 还可以恢复工作树文件。这部分功能由 git Restore 接管。

以前,我们可以使用 git checkout -- main.c 从索引中恢复工作树文件,语法是 git checkout [treeish] -- ,其中可以指定treeish为源。简单的例子:

# Restoring the working tree from the index
$ git checkout -- ./main.c

# Restoring index content from HEAD
$ git reset -- ./main.c

# Restoring the working tree and index from HEAD
$ git checkout HEAD -- ./main.c
登录后复制

注意,从 HEAD 恢复索引内容时,我们只能使用 git reset,而 git checkout 没有相应的选项。

用图表显示:

Ditch Git Checkout: Use Git Switch and Git Restore Instead

git Restore 可以更轻松地确定将恢复哪些文件以及将恢复到何处。以下选项指定恢复位置:

-W
--worktree
-S
--staged
登录后复制

默认情况下, -W 和 --worktree 将从索引恢复工作树,就像没有指定选项一样(如 git Restore -- ./main.c )。而 -S 和 --staged 将从 HEAD 恢复索引内容。当两者都通过时,索引和工作树将从 HEAD 恢复。

例如:

# Restoring the working tree from the index
$ git restore -- ./main.c
# Equivalent to
$ git restore --worktree -- ./main.c

# Restoring index content from HEAD
$ git restore --staged -- ./main.c

# Restoring the working tree and index from HEAD
$ git restore --staged --worktree ./main.c
登录后复制

用图表显示:

Ditch Git Checkout: Use Git Switch and Git Restore Instead

上述是默认的,如果我们想从不同的提交恢复,我们可以使用--source选项。例如:

# Restore `./main.c` in the working tree with the last commit
$ git restore -s HEAD^ -- ./main.c
# Equivalent to
$ git restore --source=HEAD^ -- ./main.c
登录后复制

另一个有用的 git 恢复案例可能是恢复错误处理的文件。例如:

# Incorrectly deleted files
$ rm -f ./main.c

# Quickly restore main.c from index
$ git restore ./main.c
登录后复制

批量恢复:

# Restore all C source files to match the version in the index
$ git restore '*.c'

# Restore all files in the current directory
$ git restore .

# Restore all working tree files with top pathspec magic
$ git restore :/
登录后复制

结论

git checkout 的功能区分得很清楚:git switch 用于切换分支,而 git Restore 用于恢复工作树文件。它们提供了更明确的语义,符合 UNIX 哲学。

这两个命令都是在 2019 年提出的,到目前为止,它们还处于实验阶段。可能会有变化,但通常不会太大,所以你现在就可以使用它们,它们更容易理解并且更容易混淆。

如果您发现这有帮助,请考虑 订阅我的时事通讯 以获取更多有关 Web 开发的有用文章和工具。感谢您的阅读!

以上是放弃 Git Checkout:改用 Git Switch 和 Git Restore的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!