简体   繁体   English

当文件夹和文件已经存在时克隆git存储库

[英]cloning git repository when folder and files already exist

I was working on 2 computers, alternating which one I use, and every time moved the whole thing between them. 我当时在2台计算机上工作,交替使用一台,每次都在它们之间移动整个东西。 Then I setup git on computer A and synced it with github repository. 然后,我在计算机A上安装了git并将其与github存储库同步。 So far so good. 到现在为止还挺好。

Now I want to clone this to computer B, but git clone doesn't work because the folder and files already exist. 现在我想将其克隆到计算机B,但是git clone不起作用,因为文件夹和文件已经存在。

So this is what I've done on computer B: 这就是我在计算机B上所做的事情:

git init
git remote add origin <my-github-rep.git>
git fetch

Now I need to bring the latest files from github here. 现在,我需要从这里获取github上的最新文件。

git reset origin/master

I think this is what resets current HEAD to point to master branch on origin. 我认为这是将当前HEAD重置为指向原点上的master分支的原因。

Okay, now here is the problem: 好的,现在是问题所在:

I would assume that at this point I need the following: 我认为此时我需要以下内容:

git checkout -t origin/master

Because this would bring all the latest files from origin and would have the whole thing checked out on computer B. This is what I want, but it doesn't work. 因为这会从源中带走所有最新文件,并且会在计算机B上将整个内容签出。这是我想要的,但是它不起作用。 Here is the error msg: " fatal: A branch named 'master' already exists. " 这是错误消息:“ 致命:已经存在一个名为'master'的分支。

How do I overcome this and just bring the files? 我该如何克服这个问题,而只是带上文件? What have I done wrong. 我做错了什么。

(to clarify: I don't want just to delete the folder and the files and simply clone the repository because there are many more files in there which are not supposed to be on git, so it's hard to do pick and choose; at this point what I want is to overwrite all the relevant files in my local folders from the latest files in the remote repository - this way I'll have the latest code, and the files that are not tracked by git won't be affected) (澄清一下:我不想只删除文件夹和文件,而只是克隆存储库,因为其中还有更多文件不应该放在git上,因此很难进行选择;在这里我要指向的是从远程存储库中的最新文件覆盖本地文件夹中的所有相关文件-这样,我将拥有最新代码,并且不受git跟踪的文件不会受到影响)

Your git reset command created the branch named master . 您的git reset命令创建了一个名为master的分支。 It already points to the correct commit. 它已经指向正确的提交。 The problem is that you now have a populated index, but not a correctly-populated work-tree. 问题是您现在有一个已填充的索引,但没有正确填充的工作树。

The simplest solution at this point is to move all the files you want to keep out of this directory entirely, eg, mkdir ../save and then mv * ../save to get them out of the way. 此时最简单的解决方案是将要保留的所有文件全部移出该目录,例如, mkdir ../save ,然后使用mv * ../save将其mv * ../save Then you have an empty current working tree, and git status will show you that all files are deleted but not staged for commit. 然后,您有一个空的当前工作树,并且git status将显示所有文件都已删除但尚未上载提交。 You can then do either git reset --hard or git checkout -- . 然后,您可以执行git reset --hardgit checkout -- . to bring all files back, then mv ../save/* . 将所有文件恢复,然后mv ../save/* . to put your saved files back in place. 将保存的文件放回原位。

At that point, the one remaining issue will be that your local master has no upstream. 到那时,剩下的一个问题就是您的本地master没有上游。 To fix that, just run: 要解决此问题,只需运行:

git branch --set-upstream-to=origin/master

and you will be all set. 这样您就会准备就绪。

git branch                          <-- view list of branches>
git checkout master                 <-- switch branch to master>

If this dosent work 如果这个重要的工作

git branch -d branch_name           <-- delete the specified branch locally>

To have a fresh local branch code cloned 克隆新的本地分支代码

git config --global                 <-- followed by the user id or name>
git clone repo_address.git          <-- repo_address.git is the repository url>

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

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