git:更新当前分支 [英] git: updating the current branch

查看:228
本文介绍了git:更新当前分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有2个git存储库,A和B

在两者上都只有一个master分支,并且都在本地检出并正在使用.

我正在从A插入B的master分支,并且收到此消息:

 警告:更新当前分支警告:更新当前已签出的分支可能会造成混乱,警告:因为索引和工作树无法反映HEAD中的更改.警告:因此,您可能会看到刚刚推送到其中的更改警告:在那儿运行"git diff"时已还原,您可能需要警告:在开始恢复之前运行'git reset --hard'.警告:警告:您可以将"receive.denyCurrentBranch"配置变量设置为警告:远程存储库中的拒绝"禁止将其推入警告:当前分支.警告:要允许推送到当前分支,可以将其设置为忽略";警告:但是不建议这样做,除非您安排更新其工作警告:树与您以其他方式推送的内容相匹配.警告:警告:要抑制此消息,可以将其设置为警告".警告:警告:请注意,默认值将在git的将来版本中更改警告:除非您具有以下条件,否则拒绝更新当前分支警告:配置变量设置为忽略"或警告". 

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

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

注意:我不太了解上面的git消息.引起混乱"是不是很不好并且可能导致数据丢失?还是仅表示这是一种不易处理的情况,但我通常会相信我的所有更改都以某种方式得以保留,并且我将能够在必要时解决冲突.看到已还原的更改"代表什么?变法力消失了吗?

对于我作为外国人来说,这种语言不是很清楚.

我刚刚在A上添加了一个文件,并将其推送到B.在B上,我收到该文件已删除的状态.

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

解决方案

可能引起的混乱是这样的:假设您在A的 master 上的一次提交中添加了许多文件,交给B的 master .然后,如果您进入存储库 B 的工作树并运行 git status ,它将说您刚刚添加的所有这些文件都已删除.当然,它们并没有被删除-您只是在不触及索引的情况下更新了当前分支和工作树.我认为这让许多人感到困惑!(这也是消息查看已还原的更改"的含义-从git status看来,您已经还原了刚刚推送的提交,只是因为工作树和索引现在位于分支的后面.)/p>

在这种情况下,如果在插入主分支之前先确保B中的 git status 是干净的,则可以重置工作树和索引用 git reset --hard 匹配分支的新位置.

但是,如果您对B进行了未提交或未定阶段的更改,则这会突然变得令人困惑,因为这些真正的更改将很难从推动产生的更改"中分辨出来-弄清这些更改可能非常困难.

因此,如果您了解正在发生的事情,并且很乐意应对这种情况,那就可以了.就我个人而言,我几乎总是尝试通过以下一种方法来避免出现这种情况:

  • 从A拖到B
  • 从A推送到裸仓库,然后从B推送
  • 直接从A向B中的另一个引用推送,如对此进行解释git FAQ条目,并从该引用中合并

我猜想前两个是您想要的.

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?

解决方案

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.

这篇关于git:更新当前分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆