git:更新当前分支

发布于 2024-10-25 19:36:16 字数 1560 浏览 15 评论 0原文

有 2 个 git 存储库,A 和 B

,两者都只有一个 master 分支,并且都在本地签出并正在处理。

我正在从 A 推送到 B 的 master 分支,并且收到以下消息:

warning: updating the current branch
warning: Updating the currently checked out branch may cause confusion,
warning: as the index and work tree do not reflect changes that are in HEAD.
warning: As a result, you may see the changes you just pushed into it
warning: reverted when you run 'git diff' over there, and you may want
warning: to run 'git reset --hard' before starting to work to recover.
warning: 
warning: You can set 'receive.denyCurrentBranch' configuration variable to
warning: 'refuse' in the remote repository to forbid pushing into its
warning: current branch.
warning: To allow pushing into the current branch, you can set it to 'ignore';
warning: but this is not recommended unless you arranged to update its work
warning: tree to match what you pushed in some other way.
warning: 
warning: To squelch this message, you can set it to 'warn'.
warning: 
warning: Note that the default will change in a future version of git
warning: to refuse updating the current branch unless you have the
warning: configuration variable set to either 'ignore' or 'warn'.

如果我在 B 的已签出的 master 分支上工作,我该如何更新它,以便看到 A 的更改?

如果 B 上的 master 本地签出中还存在未提交的更改怎么办?

注意:我不太明白上面 git 的消息。 “导致混乱”是否意味着它不好并且可能导致数据丢失?或者这只是意味着这是一种不容易处理的情况,但我通常可以相信我的所有更改都会以某种方式保留,并且在必要时我将能够解决冲突。 “看到更改已恢复”代表什么?这是否意味着一些更改会丢失?

对于我这个外国人来说,这种语言不是很清楚。

编辑:我刚刚在 A 上添加了一个文件并将其推送到 B。在 BI 上收到文件已删除的状态。

处理这种情况的简单工作流程是什么?

There are 2 git repositories, A and B

On both there is only a master branch, and both are locally checked out and being worked on.

I am pushing into B's master branch from A and I receive this message:

warning: updating the current branch
warning: Updating the currently checked out branch may cause confusion,
warning: as the index and work tree do not reflect changes that are in HEAD.
warning: As a result, you may see the changes you just pushed into it
warning: reverted when you run 'git diff' over there, and you may want
warning: to run 'git reset --hard' before starting to work to recover.
warning: 
warning: You can set 'receive.denyCurrentBranch' configuration variable to
warning: 'refuse' in the remote repository to forbid pushing into its
warning: current branch.
warning: To allow pushing into the current branch, you can set it to 'ignore';
warning: but this is not recommended unless you arranged to update its work
warning: tree to match what you pushed in some other way.
warning: 
warning: To squelch this message, you can set it to 'warn'.
warning: 
warning: Note that the default will change in a future version of git
warning: to refuse updating the current branch unless you have the
warning: configuration variable set to either 'ignore' or 'warn'.

If I work on B's checked out master branch, how can I update it, so I see A's changes?

What if there are also changes in the local checkout of master on B already that are not commited?

Note: I do not really understand git's message above. Does "cause confusion" mean, it is bad and can lead to data loss? Or does it just mean it is a situation that is not easy to handle, but I can as usually trust that all my changes are kept somehow and I will be able to solve conflicts if necessary. What does "see the changes reverted" stand for? Does it mena some changes are lost?

For me as a foreigner this language is not very clear.

Edit: I just added a file on A and pushed it to B. On B I receive the status that the file is deleted.

What would be a simple workflow to handle the situation?

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

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

发布评论

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

评论(4

时光匆匆的小流年 2024-11-01 19:36:16

可能造成的混乱是这样的:假设您在 A 的 master 上的提交中添加了大量文件,并将其推送到 B 的 master 。然后,如果您更改为存储库 B 的工作树并运行 git status ,它会说您刚刚添加的所有文件都已被删除。当然,它们还没有被删除 - 您刚刚更新了当前分支和工作树,而索引还没有被触及。我认为这对很多人来说都是令人困惑的! (这也是消息“查看已恢复的更改”的含义 - 从 git status 看来,您好像已经恢复了刚刚推送的提交,只是因为工作树和索引现在位于分支后面。

)在这种情况下,如果您确定在推送到主分支之前 B 中的 git status 是干净的,则可以重置工作树和索引以匹配新的使用 git reset --hard 来确定分支的位置。

然而,如果您在 B 中进行了未提交或未暂存的更改,这会突然变得更加令人困惑,因为很难从推送导致的“更改”中区分出那些真正的更改 - 解开这些更改可能非常困难。

所以,如果你了解发生了什么,并且你很乐意处理这种情况,那就没问题了。就我个人而言,我几乎总是尝试通过以下方法之一来避免这种情况:

  • 从 A 拉取到 B,而不是
  • 从 A 推送到裸存储库,然后从 B 拉取
  • 从 A 直接推送到 B 中的另一个引用,如 在此 git FAQ 条目中进行了解释,以及来自该引用的合并

我猜想其中之一前两个是你想要的。

The kind of confusion that can be caused is like this: suppose you add lots of files in a commit on A's master and push that to B's master. Then if you change into the working tree of repository B and run git status it will say that all of those files you've just added have been deleted. Of course, they haven't been deleted - you've just updated the current branch and the working tree while the index haven't been touched. I think that counts as confusing for many people! (This is also what the message means by "see the changes reverted" - from git status it looks as if you've reverted the commits you've just pushed, just because the working tree and index is behind the branch now.)

In this case, if you're sure that git status in B was clean before you pushed into your master branch, you can reset the working tree and the index to match the new position of the branch with git reset --hard.

If you had uncommitted or unstaged changes in B, however, this suddenly gets doubly confusing, since those real changes will be difficult to tell from the "changes" that resulted from the push - disentangling those may be very difficult.

So, if you understand what's going on, and you're happy to deal with that situation, it's OK. Personally, I almost always try to avoid it by one of:

  • Pulling from A to B instead
  • Pushing to a bare repository from A, and then pulling from B
  • Pushing directly to another ref in B from A, as explained in this git FAQ entry, and the merging from that ref

I'd guess that one of the first two is what you want.

心不设防 2024-11-01 19:36:16

您收到错误消息是因为您推送到非裸存储库;有关详细信息,请参阅 Git 常见问题解答。简短的解决方案是:不要推送到带有工作目录的 git 存储库;但使用裸存储库作为中介。

当您推入分支时,git 会更新分支以反映分支的新状态。它不更新的是索引和工作目录(因为您可能在那里进行了更改,并且无论如何您都无法从远程解决冲突)。

在您的示例中,这意味着以下内容:

  • 最初,A 和 B 具有相同的 master 分支
  • 您在 A 的 master 分支中添加文件 F,提交此更改并将其推送到 B 的 master 分支
  • 现在,B 的 master 分支(在但由于 git 没有触及 B 的工作目录,因此 F 在那里不存在
  • 因此,在 B 中,git 报告文件 F 已删除:它存在于 HEAD 提交中,但不存在于工作目录中。

至于您关于数据丢失的问题:此推送不会丢失任何数据。但是,如果您的工作目录中有任何更改,则可能很难识别它们,因为 git diff 现在显示与分支的状态的差异。因此,有些更改是真正的更改,有些更改仅显示,因为结账从未更新到当前版本

You get the error message because you push into a non-bare repository; see the Git FAQ for some details. The short solution is: Don't push into an git repository with a working directory; but use an bare repository as intermediary.

When you push into a branch, git updates the branch to reflect the new state of the branch. What it does not update are the index and the working directory (because you might have changes there and you would not be able to resolve conflicts anyway, from remote).

In your example, this means the following:

  • Initially, A and B have the same master branch
  • You add a file F in A's master branch, commit this change and push it to B's master branch
  • Now, B's master branch (which is checked out at the moment) contains F. But as git didn't touch B's working directory, F does not exist there
  • Hence, in B, git reports the file F as deleted: It is present in the HEAD commit, but not in the working directory.

As to your question about data-loss: You do not lose any data with this push. But, if you had any changes in your working directory, it might be hard to identify them, as git diff now shows the diff against the new state of branch. So, some of the changes are real changes, some of the changes are only displayed because the checkout was never updated to the current version

谁的年少不轻狂 2024-11-01 19:36:16

我认为问题是您的远程存储库“A”也在主分支中工作。如果你在同一个分支中推送新的更改,git 会感到困惑。
您有 2 个选项:

  • 将 A 存储库重新创建为“裸”:git init --bare

  • 签出另一个分支在 A 中(git checkout another_branch),然后尝试再次从 B 推送

I think the problem is your remote repository 'A' is also working in the master branch. If you push new changes in the same branch, git will get confused.
You have 2 options:

  • Re-create the A repository as 'bare': git init --bare

  • Checkout another branch in A (git checkout another_branch) and then try to push again from B

只想待在家 2024-11-01 19:36:16

machineB 已签出 somebranch,并且您要将对 somebranch 的更改从 machineA 推送到 machineB< /code>,无需更新machineB工作副本或索引。在 machineB 上签出的树仍将处于您推送的所有更改之前的某个点。

现在,如果您转到 machineB 并进行更改并提交,而不先执行 git reset --hard,这些更改将形成一个新的提交,而不会包括您在 machineA 上所做的所有更改,并且 somebranch 将会出现分歧。如果您没有提交,而是进行了 git diff somebranch,则 diff 应该显示您在 machineA 上所做的所有更改在 machineB 上撤消 因为您的结帐不在分支的头部。

machineB has somebranch checked out, and you're pushing changes to somebranch from machineA to machineB, without updating machineB working-copy or index. The tree that's checked out on machineB will still be at a point before all of the changes you pushed.

Now if you go over to machineB and make changes and commit without doing a git reset --hard first, those changes will form a new commit that doesn't include all of the changes you did on machineA and somebranch will diverge. If instead of committing you did a git diff somebranch the diff should show all of the changes you did on machineA being undone on machineB because your checkout isn't at the head of the branch.

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