git 在 Xcode 4 中,如何将远程 GitHub 存储库添加到现有的本地项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5803768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:47:33  来源:igfitidea点击:

In Xcode 4 how do I add a remote GitHub repository to an existing local project?

gitgithubxcode4push

提问by jbjon

The Git integration in Xcode 4 is very welcome however it seems to be a bit flaky when it comes to dealing with remote repositories. For clarity I'm using OS X version 10.6.7 and Xcode 4.0.2 (4A2002a).

Xcode 4 中的 Git 集成非常受欢迎,但是在处理远程存储库时似乎有点不稳定。为清楚起见,我使用的是 OS X 版本 10.6.7 和 Xcode 4.0.2 (4A2002a)。

If I create a new Xcode 4 Project and accept the option to create a local Git repository, all is well. I can commit locally and that's great.

如果我创建一个新的 Xcode 4 项目并接受创建本地 Git 存储库的选项,一切都很好。我可以在本地提交,这很棒。

When I want to push the new project to GitHub I want to add a remote repository and push my local changes up to GitHub. I can add a GitHub repository to Xcode 4 as a new Repository in the Organizer window but there's no method of setting it as a remote repository for my existing project.

当我想将新项目推送到 GitHub 时,我想添加一个远程存储库并将我的本地更改推送到 GitHub。我可以将 GitHub 存储库作为管理器窗口中的新存储库添加到 Xcode 4,但无法将其设置为现有项目的远程存储库。

So I switch to using git commands in Terminal.

所以我切换到在终端中使用 git 命令。

git remote add origin https://<username>@github.com/<organization>/<repository>.git

If I then go back to Xcode 4 and try to use

如果我再回到 Xcode 4 并尝试使用

File -> Source Control -> Push...

文件 -> 源代码管理 -> 推送...

then the Choose the repository to which to push changes.dialog appears listing the originremote repository. There's a yellow indicator saying that Authentication is required, which is fine as I've not been able to enter my credentials yet for HTTPS. However when I click 'Push' and it tries to push the changes it says "The operation could not be performed because access to the repository 'origin' is denied."

然后选择要将更改推送到的存储库。将出现列出远程存储库的对话框。有一个黄色指示器表示需要进行身份验证,这很好,因为我还没有能够为 HTTPS 输入我的凭据。但是,当我单击“推送”并尝试推送更改时,它会显示“无法执行操作,因为对存储库“来源”的访问被拒绝。

A dialog prompting me for my username and password then appears with my pre-filled out so it must be reading this correctly from the local repository's config.

然后会出现一个对话框,提示我输入用户名和密码,并预先填写好,因此它必须从本地存储库的配置中正确读取。

When I enter my password Xcode 4 then crashes with the following error

当我输入密码时,Xcode 4 崩溃并显示以下错误

ASSERTION FAILURE in /SourceCache/IDEKit/IDEKit-303/Framework/Classes/SourceControl/IDESourceControlOperationInfo.m:59
Details:  Message sent to invalidated object: <IDESourceControlPushOperationInfo, 0x200bd2a80>. Backtrace for invalidation:
 (null)
Object:   <IDESourceControlPushOperationInfo: 0x200bd2a80>
Method:   -invalidate
Thread:   <NSThread: 0x200020700>{name = (null), num = 1}
Hints:   None

If I restart Xcode an originrepository has been added to the Repositories browser in Organizer and I can see the master branch on GitHub so I know the credentials are right without me changing anything.

如果我重新启动 Xcode,一个原始存储库已添加到 Organizer 的存储库浏览器中,我可以在 GitHub 上看到 master 分支,因此我知道凭据是正确的,而无需我更改任何内容。

Has anyone found a reliable way of using Xcode 4 with GitHub starting with a local project first?

有没有人首先从本地项目开始,找到一种在 GitHub 上使用 Xcode 4 的可靠方法?

If I clone an existing GitHub repository with an Xcode project in it, open it with Xcode 4 then even though the first Push seems to fail, the 2nd push did then work. So maybe starting from absolute scratch with Xcode 4 creating the repository for me isn't the best pattern (until they improve the Git tooling in a later version of Xcode 4).

如果我克隆了一个包含 Xcode 项目的现有 GitHub 存储库,请使用 Xcode 4 打开它,然后即使第一次推送似乎失败,第二次推送也会起作用。所以也许从头开始使用 Xcode 4 为我创建存储库并不是最好的模式(直到他们在更高版本的 Xcode 4 中改进 Git 工具)。

EDIT WITH FURTHER DETAILS OF ONE WORKAROUND

使用一种解决方法的更多详细信息进行编辑

This example only really applies to the first project you're adding to a GitHub repository. As such I think you're better off with the following steps, bearing in mind there still seem to be some Xcode 'gotchas' in this solution.

此示例仅适用于您添加到 GitHub 存储库的第一个项目。因此,我认为您最好执行以下步骤,请记住,此解决方案中似乎仍然存在一些 Xcode“问题”。

  1. List item
  2. 'Connect to a repository' from Welcome to Xcode dialog.
  3. Use GitHub SSH URL for Location
  4. Give it a name (RepositoryName)and Clone to a directory
  5. 1st attempt appears to fail even though contents are downloaded
  6. Try Again and it works.
  7. There is now a new Repository defined in the Organizer window with its origin set to the GitHub remote SSH URL.
  8. Create a New Project within the directory you just cloned into but DO NOT tick the 'Create local git repository for this project' option.
  9. Do initial commit to local Git repository.
  10. Try Source Control -> Push and Xcode 4 fails with 'The operation could not be performed because access to the repository RepositoryNamewas denied. Check username and password'
  11. Try Source Control -> Push and Xcode 4 pops up the remote Repository dialog again but this time has detected a remote Repository called origin. Next to it there's a yellow ball and 'Authentication required'.
  12. Clicking 'Push' again caused the remote Push to work.
  1. 项目清单
  2. “欢迎使用 Xcode”对话框中的“连接到存储库”。
  3. 使用 GitHub SSH URL 作为位置
  4. 给它一个名字(RepositoryName)并克隆到一个目录
  5. 即使下载了内容,第一次尝试似乎也失败了
  6. 再试一次,它的工作原理。
  7. 现在在管理器窗口中定义了一个新的存储库,其来源设置为 GitHub 远程 SSH URL。
  8. 在您刚刚克隆到的目录中创建一个新项目,但不要勾选“为此项目创建本地 git 存储库”选项。
  9. 对本地 Git 存储库进行初始提交。
  10. 尝试源代码管理 -> 推送,Xcode 4 失败并显示“操作无法执行,因为对存储库RepositoryName 的访问被拒绝。检查用户名和密码'
  11. 尝试 Source Control -> Push 和 Xcode 4 再次弹出远程存储库对话框,但这次检测到一个名为origin的远程存储库。在它旁边有一个黄色的球和“需要身份验证”。
  12. 再次单击“推送”会导致远程推送工作。

One thing I've spotted and not managed to work out yet is that the response messages back from GitHub with the checkin hash value and any messages seem to get reported in Xcode Log navigator as Push failures, even though the Push is successful as far as GitHub is concerned.

我发现但尚未解决的一件事是,从 GitHub 返回的带有签入哈希值的响应消息和任何消息似乎都在 Xcode 日志导航器中报告为推送失败,即使推送是成功的GitHub 很担心。

Anyone else have a better solution?

其他人有更好的解决方案吗?

回答by Tim

I think I found the Xcode way to push a existing project to GitHub or any other remote repository!

我想我找到了将现有项目推送到 GitHub 或任何其他远程存储库的 Xcode 方式!

Preconditions

先决条件

  • I assume that you have a project created by Xcode 4 witha local git repository.
  • You created an empty remote git repository (I call it RemoteRepositoryin in the following example)
  • 我假设您有一个由 Xcode 4 创建的带有本地 git 存储库的项目。
  • 您创建了一个空的远程 git 存储库(我在以下示例中将其称为RemoteRepository

Push it to remote

推送到远程

  1. Open the Organizerin Xcode an go to the Repositoriestab

  2. At first click on Remotesand then on "Add Remote"

    Xcode Organizer

  3. Enter a Remote Nameand the Locationof your remote repository and click on create

    Add a remote dialog

  4. Then select your project and open File -> Source Control -> Push ...

    open push dialog

  5. Select your remote repository and wait until it is connectable! Look for the Createin the brackets.

    push dialog

  6. Now your old project is published to GitHub

    result on GitHub

  1. 在 Xcode 中打开Organizer并转到Repositories选项卡

  2. 首先点击遥控器,然后点击“添加遥控器”

    Xcode 管理器

  3. 输入远程名称和远程存储库的位置,然后单击创建

    添加远程对话框

  4. 然后选择您的项目并打开 File -> Source Control -> Push ...

    打开推送对话框

  5. 选择您的远程存储库并等待它可连接!在括号中查找Create

    推送对话框

  6. 现在您的旧项目已发布到 GitHub

    GitHub 上的结果

I hope it will works for you also! In my case it works with every remote git repository.

我希望它也适用于你!就我而言,它适用于每个远程 git 存储库

Please contact me on twitterif something unclear or wrote in bad English!

如果有不清楚的地方或英文写得不好,请在twitter上联系我!

回答by Dan Ray

I've done this successfully using an ssh connection (which looks like [email protected]:<username>/<project>.git). It authenticates the user based on an SSH public key, which you can generate on the command line--lots of docs about that at github in the "help" section.

我已经使用 ssh 连接(看起来像[email protected]:<username>/<project>.git)成功地完成了这项工作。它根据 SSH 公钥对用户进行身份验证,您可以在命令行上生成该公钥——在 github 的“帮助”部分中有很多关于此的文档。

回答by mahboudz

Xcode 6.2 (and possibly 6.x) changed things around. There no longer is a Repositories tab in the Organizer. Here's how to do it with the newer Xcodes.

Xcode 6.2(可能还有 6.x)改变了一切。管理器中不再有存储库选项卡。以下是如何使用较新的 Xcode 执行此操作。

To add a project with a local git repository to GitHub, this is what you do:

要将具有本地 git 存储库的项目添加到 GitHub,您需要执行以下操作:

  1. Go to the Source Control menu, elect your project and select Configure:
  1. 转到 Source Control 菜单,选择您的项目并选择 Configure:

enter image description here

在此处输入图片说明

  1. You'll see:
  1. 你会看到的:

enter image description here

在此处输入图片说明

  1. Select the Remotes tab, and press the "+" in the lower left corner. Select Add Remote.
  1. 选择遥控器选项卡,然后按左下角的“+”。选择添加远程。

enter image description here

在此处输入图片说明

  1. Type in a name for the remote repository, and type in the URL for your remote repository. Make sure you've created this remote repository already, and if on GitHub, make are you specify that this is an existing project, and not a new one.
  1. 输入远程存储库的名称,然后输入远程存储库的 URL。确保您已经创建了这个远程存储库,如果在 GitHub 上,您是否指定这是一个现有项目,而不是一个新项目。

enter image description here

在此处输入图片说明

  1. You're almost done. Go to the Source Control menu and select Push. You will then see the name of the repo you used in the last step, something like "simplest/master"; press the Push button.
  1. 你几乎完成。转至源代码管理菜单并选择推送。然后你会看到你在最后一步中使用的 repo 的名称,类似于“simplest/master”;按下按钮。

Done.

完毕。