git 中分支创建的混乱

发布于 2024-12-18 18:10:50 字数 548 浏览 3 评论 0原文

对分支 master 进行一些更改后,我决定从一个新分支开始工作。我执行了 git checkout -b new_branch_name 并创建并签出了一个新分支。 git status 显示了我对 master 所做的更改。

我很好奇我的更改现在是在两个分支(masternew_branch_name)上还是只是在 new_branch_name 上。所以我检查了 master 并注意到我的更改也在那里。因此,我使用 git checkout -- fileThatChanged 恢复了这些更改。这些更改确实从 master 中消失了。

不幸的是,检查 new_branch_name 并运行 git status 显示我的更改也从该分支恢复。

我想了解发生了什么以及将来如何避免这种情况。

一种解决方案是在开始工作之前创建/签出一个新分支。

After making a few changes to branch master I decided to work from a new branch. I did git checkout -b new_branch_name and a new branch was created and checked out. git status showed the changes I had made to master.

I was curious if my changes were now on both branches (master and new_branch_name) or just new_branch_name. So I checked out master and noticed my changes were there as well. So I reverted those changes with git checkout -- fileThatChanged. The changes were indeed gone from master.

Unfortunately checking out new_branch_name and running git status showed my changes were reverted from that branch as well.

I'd like to understand what happened and how can I avoid this in the future.

One solution is to just create/checkout a new branch before starting work.

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

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

发布评论

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

评论(4

段念尘 2024-12-25 18:10:50

您从未将更改提交到任何一个分支。也就是说,如果您这样做:

$ git status

并看到类似这样的内容:

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   myfile.txt
#

对“myfile.txt”的更改在任何分支上都不“存在”——它们仅存在于您的本地工作副本中。在您将它们提交到存储库之前,它们不会最终出现在分支上。

如果此时我要输入:

$ git checkout myfile.txt

这将消除我的更改(并将文件返回到当前分支上最后一次提交中的样子)。

如果我想在新分支上提交这些更改,我可能会这样做:

$ git checkout -b new_branch_name
$ git add myfile.txt
$ git commit -m "made some changes"

You had never committed your changes to either branch. That is, if you did this:

$ git status

And saw something like this:

# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   myfile.txt
#

The changes to "myfile.txt" do not "exist" on any branch -- they are only in your local working copy. They don't end up on a branch until you have comitted them to a repository.

If at this point I were to type:

$ git checkout myfile.txt

This would obliterate my changes (and return the file to whatever it looked like in the last commit on my current branch).

If I wanted to commit these changes on a new branch, I might do something like this:

$ git checkout -b new_branch_name
$ git add myfile.txt
$ git commit -m "made some changes"
剩一世无双 2024-12-25 18:10:50

该索引在所有分支之间共享。因为它是本地工作树。
它代表将要提交的内容,而不假设它将在哪个分支中提交。
但这意味着您恢复更改(即从索引或本地工作区中删除它),无论您位于哪个分支,它都会被恢复。

另请参阅:

git 文件转换

The index is shared amongst all branches. As it the local working tree.
It represents what will be committed, without assuming in what branch it will be committed.
But that means it you revert a change (ie remove it from the index or from the local workspace), it will be reverted no matter what branch you are in.

See also:

git file transition

茶色山野 2024-12-25 18:10:50

工作目录/索引中的修改更改(您在 git status 中看到的内容)不会链接到分支,但很常见。因此,当您切换分支时,更改仍将在新分支中。如果您撤消更改,那么当您切换分支时就会看到这一点。您应该在更改分支之前提交或隐藏。

Modified changes in your working directory / index ( what you see in git status ) are not linked to a branch but are common. So when you switch branches, the changes will still be in the new branch. If you undo the changes, that will be seen when you switch branches. You should either commit or stash before changing branch.

夏花。依旧 2024-12-25 18:10:50

正如拉尔斯克斯所说,您没有提交这些更改。

如果您希望在记住工作副本的同时切换分支,则必须使用 git stash。

完成后,您可以使用 git stash pop 恢复进程内工作目录。

As larsks says, you did not have these changes committed.

If you are looking to switch branches while remembering your working copy, you must use git stash.

When you are done, you can use git stash pop to restore your in-process working directory.

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