更简单地解释 GIT checkout 命令的路径

发布于 2024-09-24 16:13:57 字数 385 浏览 6 评论 0原文

checkout 命令的 git 文档 中找到以下文本:

...如果没有给出路径, git checkout 也会将 HEAD 更新为 将指定分支设置为 当前分支....

任何人都可以对这意味着什么给出更简单的解释吗?如果这看起来很简单,我很抱歉,并且通读该页,我似乎无法弄清楚它的确切含义。抱歉,如果这看起来很基本......

特别是我对结账如何更新 HEAD 感到困惑。我通常设想签出会影响工作目录——这是 git 独有的功能吗?因为您正在更新存储库的本地副本以便稍后使用它?

In the git documentation for the checkout command the following text is found:

...If no paths are given,
git checkout will also update HEAD to
set the specified branch as the
current branch....

Can anyone give a simpler explanation of what this means? I'm sorry if it seems simple, and reading through that page, I can't seem to come up with what it means exactly. Sorry if this seems basic..

In particular I am confused on how checkout is updating HEAD. I usually envision checkout affecting the working directory -- is this an ability unique to git in that you are updating your local copy of the repository for the purposes of working with it later?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

依 靠 2024-10-01 16:13:57

版本 A:(仅指定分支)

git checkout <branch>

获取该<分支>的所有文件并将 HEAD(指向“我现在在哪里”的指针)放置在指定的分支处。

版本 B:指定路径)

git checkout <file>

获取的最新版本并留下 HEAD 独自一人。

Version A: (specifying only the branch)

git checkout <branch>

Gets all files for that <branch> and places HEAD (a pointer to "where am I now") at the branch specified.

Version B: (specifying a path only)

git checkout <file>

Gets the latest version of <file> and leaves HEAD alone.

冷情 2024-10-01 16:13:57

HEAD 是指向签出工作副本的提交的指针。因此,如果您签出分支(或提交或标记),则 HEAD 将设置为该提交。

这些信息存储在文本文件 .git/HEAD 中,您可以简单地查看其内容:

$ cat .git/HEAD
# refs: refs/heads/master

HEAD is a pointer to the commit your working copy was checked out from. so if you checkout a branch (or commit or tag) then HEAD is set to that commit.

this information is stored in the textfile .git/HEAD, you can simply look at its content:

$ cat .git/HEAD
# refs: refs/heads/master
蝶…霜飞 2024-10-01 16:13:57

这意味着 git checkoutbranchname 将从

  1. 该分支的尖端检出文件到您的工作目录中,并将
  2. 您的 HEAD 设置为该分支的尖端,以便您现在正在“上”它。

例子:

 jb@apto % git branch         
 * develop
   master
   next
 jb@apto % git checkout master
 Switched to branch 'master'
 jb@apto % git branch
   develop
 * master
   next

It means that git checkout branchname will

  1. Check out the files from the tip of that branch into your working directory, and
  2. Set your HEAD to the tip of that branch, so that you are now "on" it.

Example:

 jb@apto % git branch         
 * develop
   master
   next
 jb@apto % git checkout master
 Switched to branch 'master'
 jb@apto % git branch
   develop
 * master
   next
芸娘子的小脾气 2024-10-01 16:13:57

如果您处于 DETACHED HEAD 模式(请参阅这个问题),您可以轻松地将 HEAD 重置到主分支:

checkout detached

这里的 'git checkout' 会将 HEAD 重置到 master 分支的尖端。
(更多内容请参阅“可视化 Git 参考”)
它还将更新索引和工作目录。
所以这不仅仅是重置指针“HEAD”。

If you are in a DETACHED HEAD mode (see this question), you could easily reset HEAD to the master branch:

checkout detached

Here a 'git checkout' would reset HEAD to the tip of master branch.
(More in "A Visual Git Reference")
It will also update both the index and the working directory.
So it is not just about resetting the pointer 'HEAD'.

卖梦商人 2024-10-01 16:13:57

对于 Git 2.23+(2019 年 8 月),请使用 git switch 而不是令人困惑的git checkout,加上git Restore(仅适用于文件)

git switch master

这会将 HEAD 切换回 master

您仍然可以使用 git switch --detach 直接引用提交(分离的 HEAD)。
不再需要签出(同时处理分支和文件)。

With Git 2.23+ (August 2019), use git switch instead of the confusing git checkout, coupled with git restore (for files only)

git switch master

That would switch HEAD back to master.

You can still reference a commit directly (detached HEAD) with git switch --detach <acommit>.
No more checkout (which deals both with branches and files).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文