如何在“git checkout”上更新工作目录?

发布于 2024-12-12 04:02:13 字数 813 浏览 0 评论 0原文

考虑以下“故事”:

$ mkdir my_project
$ cd my_project
$ git init
Initialized empty Git repository in /home/misha/misha/my_project/.git/

$ echo "first line" > hello.txt
$ git add hello.txt
$ git commit -m "first commit"
[master (root-commit) 9c913a1] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

$ git branch new_feature
$ git checkout new_feature
Switched to branch 'new_feature'
$ echo "second line" >> hello.txt
$ cat hello.txt
first line
second line

$ git checkout master
M    hello.txt
Switched to branch 'master'
$ cat hello.txt
first line
second line

为什么 hello.txt 在分支 master 上有两行? (我认为 git checkout 会将工作目录恢复到之前的状态,即 hello.txt 将只有一行。)

工作幕后实际发生了什么git checkout 上的目录?它是如何更新的?

Consider the following "story":

$ mkdir my_project
$ cd my_project
$ git init
Initialized empty Git repository in /home/misha/misha/my_project/.git/

$ echo "first line" > hello.txt
$ git add hello.txt
$ git commit -m "first commit"
[master (root-commit) 9c913a1] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

$ git branch new_feature
$ git checkout new_feature
Switched to branch 'new_feature'
$ echo "second line" >> hello.txt
$ cat hello.txt
first line
second line

$ git checkout master
M    hello.txt
Switched to branch 'master'
$ cat hello.txt
first line
second line

Why hello.txt has two lines on branch master? (I thought that git checkout will revert the working directory to the previous state, i.e. hello.txt will have only one line.)

What actually happens behind the scenes to the working directory on git checkout? How it is updated?

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

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

发布评论

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

评论(4

﹂绝世的画 2024-12-19 04:02:13

您的 git checkout 到 master 可以防止您丢失未提交的更改。这就是为什么您的 hello.txt 文件中仍然有第二行。如果您确实想丢失未提交的更改,则必须使用 -f 参数。

最后你的结帐看起来像这样:

git checkout -f master

Your git checkout to master prevents you from losing uncommitted changes. Thats why you still have your second line in your hello.txt file. If you really want to lose your uncommitted changes you have to use the -f parameter.

Finally your checkout would look like this:

git checkout -f master
谜泪 2024-12-19 04:02:13

Git checkout(松散地)将使用指定提交时存储库的内容更新工作副本。您的 new_feature 分支没有您添加到文件中的第二行(因为您尚未提交)。现在,该额外的行只是工作副本中未跟踪的更改,它将添加到您提交它的分支中。

Git checkout (loosely) will update the working copy with the contents of the repository at the commit which specify. Your new_feature branch doesn't have the second line which you've added to your file (since you haven't committed it yet). Right now, that extra line is just an untracked change in your working copy and it will get added to the branch on which you commit it.

浅语花开 2024-12-19 04:02:13

如果添加第二行已提交给 new_feature,Git 将按预期签出。
未提交的更改通常会阻止签出,但这里是合并。

Git will checkout as expected if adding second line had been committed to new_feature.
Uncommitted changes normally prevent from checkout but here it is a merge.

我的鱼塘能养鲲 2024-12-19 04:02:13

Git checkout 不会替换文件系统中的文件。
Git checkout 会更改您所在的分支。
更改分支不会更改文件系统上的内容。
我喜欢认为树看起来像这样:

  1. 远程仓库
    1.a.大师
    1.b.功能
  2. 本地存储库
    2.a.大师
    2.b.功能
  3. 未提交的代码/文件系统

签出 -> a 和 b 之间切换

拉动 ->将当前分支中新的未同步内容从 1 复制到 2

Push ->将当前分支中的新内容从 2 复制到 1

Commit ->将新内容从 3 复制到 2

Git checkout doesn't replace the files in your filesystem.
Git checkout changes the branch you're on.
Changing branch doesn't change what's on your filesystem.
I like to think the tree looks like this:

  1. Remote repo
    1.a. Master
    1.b. Feature
  2. Local repo
    2.a. Master
    2.b. Feature
  3. Uncommitted code / filesystem

Checkout -> switch between a nd b

Pull -> copy new unsynchronized stuff in the current branch from 1 to 2

Push -> copy from new stuff in the current branch from 2 to 1

Commit -> copy new stuff from 3 to 2

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