Git如何与远程仓库/本地仓库进行push和pull交互?

3
我来自SVN,不太理解Git的工作原理。我已经创建了一个远程仓库,在本地机器上克隆了该仓库 - 如果我在本地机器上更改文件并尝试将这些文件推送到我从中克隆的确切远程仓库,我会收到如下错误信息。
remote: error: refusing to update checked out branch: refs/heads/master 我查看了一些关于同样问题的SO问题,最受欢迎的解决方案是创建一个裸仓库,然后提交到它。那么当我从那个新仓库克隆时会发生什么 - 这会再次发生吗?我应该为每个本地提交创建新的裸仓库吗?我很困惑,如果有人能指点我正确的方向就太好了。 我认为我做错了。
我正在遵循的确切步骤: 1)在远程服务器上创建一个目录 - 初始化为git repo 2)将我需要的所有内容复制到服务器上 3)将内容添加到git中并在其中提交 4)从远程克隆git repo到我的本地机器上 5)在本地更改文件,将更改添加到本地并进行提交。 6)当我尝试使用以下命令将本地所做的更改推送到远程:git push origin 我会收到错误消息。我会提供整个错误消息。
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: Auto packing the repository for optimum performance.

remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
 ! [remote rejected] master -> master (branch is currently checked out)

1
你对自己想要做的事情的描述很清楚。提供一些具体步骤的细节会更有助于帮助你。 - larsen
2个回答

2
裸仓库是您通常应该推送到的唯一仓库,因为非裸仓库意味着有文件的副本被检出,需要更新。如果您拥有两个仓库,并且希望它们都具有数据的已检出副本,则可以从两个位置使用git pull来拉取相反的副本。在一侧,您可以进行克隆,但在另一侧,您将不得不使用git remote add来创建远程以进行拉取。
裸仓库将允许您推送到它,因为您知道您不会弄乱正在进行中的检出副本。
如果您还没有阅读过“gittutorial”的手册,我建议您从那里开始学习,因为那里是一个很好的起点。
至于一个良好的工作模型,我经常在一台机器(服务器)上放置一个裸仓库,并且通常在多个其他机器上克隆该仓库,包括服务器本身。因此,如果我正在服务器上处理仓库,我就不会从裸仓库(显然)开始工作,我会在其他地方克隆它并推送到它。这使得每个副本都可以推送到“主”裸仓库并从中拉取。这使得同步变得更加容易,特别是当某些副本不总是在线时。
注意:没有“错误的方法”或“最佳的方法”。每个人都有自己喜欢处理仓库的方式。这是分布式仓库系统(包括git)的一个更好的部分:它让每个用户为自己找到最好的方法。

2

共享/远程仓库使用裸格式,意味着它们没有工作副本。Subversion也是如此——中央仓库没有工作副本。在Subversion中,通过检出创建工作副本,在Git中则通过克隆。

如果将远程仓库更改为裸格式,则问题将不会继续发生。

工作流与Subversion并没有显著的区别,只是多了一步(或两步,具体取决于您使用的命令)。

Make changes
git add (to add the changes to the staging area)
git commit (to save the changes to the local repo)
git push (to push the changes to the remote repo)

当你想要从远程代码仓库拉取别人的变更时,你需要执行 "git pull" 命令,它与 "svn update" 命令功能类似。


谢谢,我现在对Git的工作原理有了更好的理解。 - ExtremelyPoorMan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接