简体   繁体   English

Git如何通过远程仓库/本地仓库推送和拉动?

[英]How does Git push and pull work with a remote repo/local repo?

I come from SVN and I am not understanding how the whole Git thing works. 我来自SVN,我不明白整个Git是如何工作的。 I've created a remote repo, cloned the repo in my local machine - if I make changes to files in my local machine and try to push those files to the exact remote repo I cloned from I get an error like this. 我已经创建了一个远程仓库,在我的本地机器上克隆了仓库 - 如果我对本地机器中的文件进行了更改并尝试将这些文件推送到我克隆的确切远程仓库,我会得到这样的错误。

remote: error: refusing to update checked out branch: refs/heads/master remote:错误:拒绝更新签出的分支:refs / heads / master

I browsed through some SO questions regarding the same issue and the most popular solution was creating a bare repository and then committing to it. 我浏览了一些关于同一问题的SO问题,最流行的解决方案是创建一个裸存储库然后承诺。 So what happens when I clone from that new repo - would this happen again? 那么当我从新的回购中克隆时会发生什么 - 这会再次发生吗? Should I keep creating new bare repos for each commit from my local? 我应该继续为我当地的每次提交创建新的裸仓吗? I'm confused and it will be great if someone can point me in the right direction. 我很困惑,如果有人能指出我正确的方向,那将会很棒。 I think I am doing this completely wrong. 我想我这样做完全错了。

Exact steps I am following: 1) Created a directory in remote server - init as git repo 2) Copied all contents I needed into the server 3) Added the content into git and committed them there 4) Cloned the git repo from remote to my local machine 5) Made a change to a file in my local, added the change to my local and committed it. 我正在遵循的确切步骤:1)在远程服务器中创建一个目录 - init作为git repo 2)将我需要的所有内容复制到服务器中3)将内容添加到git并将其提交到那里4)将git repo从远程克隆到我的本地机器5)对我本地的文件进行了更改,将更改添加到我的本地并提交了它。 6) When I try to push changes made in my local to remote using: git push origin I get the error message. 6)当我尝试将我本地的更改推送到远程时使用:git push origin我收到错误消息。 I'm providing the whole error message below. 我在下面提供了整个错误消息。

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)

The bare repos are the only ones that you should generally push into, because a non-bare repo means there are copies of the files checked out that would need to be updated. 裸露的回购是您通常应该进入的唯一的回购,因为非裸回购意味着有签出的文件的副本需要更新。 If you have two repos that you control, and you want them both to have checked-out copies of the data, then use git pull from both locations to pull from the opposite one. 如果您有两个您控制的存储库,并且您希望它们都具有已检出的数据副本,则使用两个位置的git pull从相反的位置拉出。 On one side you can do a clone, but on the other you'll have to use git remote add in order to create a remote to pull from. 一方面你可以做一个克隆,但另一方面你必须使用git remote add来创建一个遥控器。

A bare repo will let you push into it, because you know you're not messing up a checked-out copy's in-the-middle-of-doing-something notion. 一个简单的回购将让你进入它,因为你知道你没有弄乱一个签出副本的中间做某事的概念。

If you haven't read the man pages for 'gittutorial', I would start there as they're wonderful places to start learning. 如果您还没有阅读过“gittutorial”的手册页,我会从那里开始,因为它们是开始学习的好地方。

As for a good working model, I often have one (server) machine with a bare repo in it, and often a clone of that repo on multiple other machines including the server itself. 至于一个好的工作模型,我经常有一台(服务器)机器,里面有一个裸仓库,而且往往是在包括服务器本身在内的多台其他机器上克隆该仓库。 So if I'm working on the repo on the server, I don't work from the bare repo (obviously), I clone it and work somewhere else and push into it. 因此,如果我正在处理服务器上的repo,我不会使用裸仓库(显然),我克隆它并在其他地方工作并推入它。 That lets each clone push into the "master" bare repo and pull from it. 这让每个克隆进入“主”裸仓库并从中拉出。 It makes synchronization a bit easier, especially when some copies are not always online. 它使同步更容易,特别是当某些副本不总是在线时。

Note: there is no "wrong way" or "best way". 注意:没有“错误的方式”或“最好的方式”。 Each person has their own preferred way of dealing with repositories. 每个人都有自己处理存储库的首选方式。 And that's one of the better parts of a distributed repository system (git or otherwise): it lets each user do what works best for themselves. 这是分布式存储库系统(git或其他)的更好部分之一:它让每个用户都能做最适合自己的事情。

Shared / remote repositories use a bare format, meaning they have no working copy. 共享/远程存储库使用裸格式,这意味着它们没有工作副本。 Subversion is the same - the central repository does not have a working copy. Subversion是相同的 - 中央存储库没有工作副本。 Working copies are created via checkouts in subversion, and via clones in git. 工作副本是通过subversion中的签出和git中的克隆创建的。

The problem will not continue to occur if you change the remote repository to bare format. 如果将远程存储库更改为裸格式,则不会继续出现此问题。

The workflow is not significantly different than subversion, just an extra step (or two, depending on the commands you use) 工作流程与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)

When you want to pull changes from others from the remote repo, you do a "git pull" which functions similar to "svn update." 当您想从远程仓库中提取其他人的更改时,您会执行“git pull”,其功能类似于“svn update”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM