git 将现有源代码导入 GitHub

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4658606/
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 09:49:43  来源:igfitidea点击:

Import existing source code to GitHub

gitgithub

提问by Mohammad Ali Akbari

How can I import source code from my computer to my GitHub account?

如何将源代码从我的计算机导入到我的 GitHub 帐户?

回答by Peter

If you've got local source code you want to add to a new remote new git repository without 'cloning' the remote first, do the following (I often do this - you create your remote empty repository in bitbucket/github, then push up your source)

如果您有本地源代码想要添加到新的远程新 git 存储库而不先“克隆”远程,请执行以下操作(我经常这样做-您在 bitbucket/github 中创建远程空存储库,然后向上推你的消息来源)

  1. Create the remote repository, and get the URL such as [email protected]:/youruser/somename.gitor https://github.com/youruser/somename.git

    If your local GIT repo is already set up, skips steps 2 and 3


  2. Locally, at the root directory of your source, git init

    2a. If you initialize the repo with a .gitignore and a README.md you should do a git pull {url from step 1}to ensure you don't commit files to source that you want to ignore ;)

  3. Locally, add and commit what you want in your initial repo (for everything, git add .then git commit -m 'initial commit comment')


  4. to attach your remote repo with the name 'origin' (like cloning would do)
    git remote add origin [URL From Step 1]

  5. Execute git pull origin masterto pull the remote branch so that they are in sync.
  6. to push up your master branch (change master to something else for a different branch):
    git push origin master
  1. 创建远程存储库,并获取 URL,如[email protected]:/youruser/somename.githttps://github.com/youruser/somename.git

    如果您的本地 GIT 存储库已设置,则跳过第 2 步和第 3 步


  2. 在本地,在源的根目录中, git init

    2a. 如果您使用 .gitignore 和 README.md 初始化存储库,您应该执行 agit pull {url from step 1}以确保您不会将文件提交到要忽略的源;)

  3. 在本地,在初始存储库中添加并提交您想要的内容(对于所有内容,git add .然后 git commit -m 'initial commit comment'


  4. 使用名称“origin”附加您的远程存储库(就像克隆一样)
    git remote add origin [URL From Step 1]

  5. 执行git pull origin master拉取远程分支,使它们同步。
  6. 推高您的主分支(将主分支更改为其他分支的其他分支):
    git push origin master

回答by Gordon

This is explained in the excellent free eBook ProGit. It assumes you already have a local Git repository and a remote one. To connect them use:

这在优秀的免费电子书 ProGit 中有解释。它假设您已经有一个本地 Git 存储库和一个远程存储库。要连接它们,请使用:

$ git remote
origin
$ git remote add pb git://github.com/paulboone/ticgit.git
$ git remote -v
origin    git://github.com/schacon/ticgit.git
pb    git://github.com/paulboone/ticgit.git

To push the data from the local repository to GitHub use:

要将数据从本地存储库推送到 GitHub,请使用:

$ git push pb master

If you have not setup a local and/or a remote repository yet, check out the help on GitHuband the previous chapters in the book.

如果您还没有设置本地和/或远程存储库,请查看GitHub 上的帮助和本书前面的章节

回答by cfont

One of the comments mentioned using the GitHub GUI, but it didn't give any specific help on using and notice that most if not all the answers were useful at the command prompt only.

使用 GitHub GUI 提到的评论之一,但它没有提供任何具体的使用帮助,并注意到大多数(如果不是全部)答案仅在命令提示符下有用。

If you want to use the GitHub GUI, you can follow these steps:

如果要使用 GitHub GUI,可以按照以下步骤操作:

  1. Click the "+" button and choose "Add Local Repository" Enter image description here
  2. Navigate to the directory with your existing code and click the "Add" button.
  3. You should now be prompted to "Create a new local Git repository here" so click the "Yes" button. Enter image description here
  4. Add your "Commit Summary" and "Extended description" as desired. By default, all of your files should selected with checkmarks already. Click the "Commit & Sync" button. Enter image description here
  5. Now you will be prompted to add the name and description of your project as well as which account to push it to (if you have multiple). Click the "Push Repository" button Enter image description here
  1. 单击“+”按钮并选择“添加本地存储库” 在此处输入图片说明
  2. 导航到包含现有代码的目录,然后单击“添加”按钮。
  3. 现在应该会提示您“在此处创建新的本地 Git 存储库”,因此单击“是”按钮。 在此处输入图片说明
  4. 根据需要添加“提交摘要”和“扩展描述”。默认情况下,您的所有文件都应已选中并带有复选标记。单击“提交和同步”按钮。 在此处输入图片说明
  5. 现在系统会提示您添加项目的名称和描述以及要将其推送到哪个帐户(如果您有多个)。单击“推送存储库”按钮 在此处输入图片说明

After a moment with a spinning GitHub icon, your source code will belong to a local repository and pushed/synchronised with a remote repository on your GitHub account. All of this is presuming you've previously set up the GitHub GUI, your GitHub account, and SSH keys.

在旋转 GitHub 图标片刻之后,您的源代码将属于本地存储库并与您 GitHub 帐户上的远程存储库推送/同步。所有这些都假设您之前已经设置了 GitHub GUI、您的 GitHub 帐户和 SSH 密钥。

回答by ktec

As JB quite rightly points out, it's made incredibly easy on GitHub by simply following the instructions.

正如 JB 非常正确地指出的那样,只需按照说明操作,就可以在 GitHub 上轻松实现。

Here's an example of the instructions displayed after setting up a new repository on GitHub using http://github.com/newwhen you're logged in.

以下是在您登录时使用http://github.com/new在 GitHub 上设置新存储库后显示的说明示例。

Global setup:

 Set up Git:
  git config --global user.name "Name"
  git config --global user.email [email protected]


Next steps:

  mkdir audioscripts
  cd audioscripts
  git init
  touch README
  git add README
  git commit -m 'first commit'
  git remote add origin [email protected]:ktec/audioscripts.git
  git push -u origin master


Existing Git repository?

  cd existing_git_repo
  git remote add origin [email protected]:ktec/audioscripts.git
  git push -u origin master


Importing a Subversion repository?

  Check out the guide for step-by-step instructions.

It couldn't be easier!!

再简单不过了!!

回答by justinxreese

Yes. Create a new repository, doing a git initin the directory where the source currently exists.

是的。创建一个新的存储库,git init在源当前存在的目录中执行 a 。

More here: http://help.github.com/creating-a-repo/

更多信息:http: //help.github.com/creating-a-repo/

回答by ShawnFeatherly

I had a bit of trouble with merging when trying to do Pete's steps. These are the steps I ended up with.

在尝试执行 Pete 的步骤时,我在合并时遇到了一些麻烦。这些是我最终完成的步骤。

  1. Use your OS to delete the .gitfolder inside of the project folder that you want to commit. This will give you a clean slate to work with. This is also a good time to make a .gitignorefile inside the project folder. This can be a copy of the .gitignorecreated when you created the repository on github.com. Doing this copy will avoid deleting it when you update the github.com repository.

  2. Open Git Bash and navigate to the folder you just deleted the .gitfolder from.

  3. Run git init. This sets up a local repository in the folder you're in.

  4. Run git remote add [alias] https://github.com/[gitUserName]/[RepoName].git. [alias] can be anything you want. The [alias] is meant to tie to the local repository, so the machine name works well for an [alias]. The URL can be found on github.com, along the top ensure that the HTTP button out of HTTP|SSH|Git Read-Only is clicked. The git://URL didn't work for me.

  5. Run git pull [alias] master. This will update your local repository and avoid some merging conflicts.

  6. Run git add .

  7. Run git commit -m 'first code commit'

  8. Run git push [alias] master

  1. 使用您的操作系统删除.git您要提交的项目文件夹内的文件夹。这将为您提供一个全新的工作环境。这也是.gitignore在项目文件夹中创建文件的好时机。这可以是.gitignore您在 github.com 上创建存储库时创建的副本。执行此副本将避免在更新 github.com 存储库时将其删除。

  2. 打开 Git Bash 并导航到您刚刚.git从中删除文件夹的文件夹。

  3. 运行git init。这会在您所在的文件夹中设置一个本地存储库。

  4. 运行git remote add [alias] https://github.com/[gitUserName]/[RepoName].git。[别名] 可以是任何你想要的。[别名] 旨在绑定到本地存储库,因此机器名称适用于 [别名]。URL 可以在 github.com 上找到,在顶部确保单击 HTTP|SSH|Git Read-Only 中的 HTTP 按钮。该git://网址对我不起作用。

  5. 运行git pull [alias] master。这将更新您的本地存储库并避免一些合并冲突。

  6. git add .

  7. git commit -m 'first code commit'

  8. git push [alias] master

回答by JB.

  1. Open your GitHub dashboard (it's at https://github.com/if you're logged in)
  2. Click on New Repository
  3. Fill in the blanks; click on Create Repository
  4. Follow instructions on the page that appears then
  1. 打开您的 GitHub 仪表板(如果您已登录,则位于https://github.com/
  2. 单击新建存储库
  3. 填空;单击创建存储库
  4. 按照出现的页面上的说明进行操作

回答by Fengbo

From Bitbucket:

来自比特桶:

Push up an existing repository. You already have a Git repository on your computer. Let's push it up to Bitbucket:

上推现有的存储库。您的计算机上已经有一个 Git 存储库。让我们把它推到 Bitbucket:

cd /path/to/my/repo
git remote add origin ssh://[email protected]/javacat/geo.git
git push -u origin --all   # To push up the repo for the first time

回答by LondonRob

I came here looking for a simple way to add existing source files to a GitHub repository. I saw @Pete's excellently complete answer and thought "What?! There must be a simpler way."

我来这里是为了寻找一种将现有源文件添加到 GitHub 存储库的简单方法。我看到@Pete 非常完整的回答,想“什么?!一定有更简单的方法。”

Here's that simpler way in five steps (no console action required!)

这是五步的简单方法(不需要控制台操作!)

If you're really in a hurry, you can just read step 3. The others are only there for completeness.

如果你真的很着急,你可以阅读第 3 步。其他的只是为了完整起见。

  1. Create a repository on the GitHub website. (I won't insult your intelligence by taking you through this step-by-step.)
  2. Clone the new repository locally. (You can do this either through the website or through desktop client software.)
  3. Find the newly cloned repository on your hard drive and add files just like you would to a normal directory.
  4. Sync the changes back up to GitHub.
  5. That's it!
  1. 在 GitHub 网站上创建一个存储库。(我不会通过逐步引导您来侮辱您的智商。)
  2. 在本地克隆新的存储库。(您可以通过网站或桌面客户端软件执行此操作。)
  3. 在您的硬盘驱动器上找到新克隆的存储库,并将文件添加到普通目录中。
  4. 将更改同步回 GitHub。
  5. 就是这样!

Done!

完毕!

回答by Serge Seletskyy

Add a GitHub repository as remote origin (replace [] with your URL):

添加一个 GitHub 存储库作为远程源(将 [] 替换为您的 URL):

git remote add origin [[email protected]:...]

Switch to your master branch and copy it to develop branch:

切换到你的主分支并将其复制到开发分支:

git checkout master
git checkout -b develop

Push your develop branch to the GitHub develop branch (-fmeans force):

将您的开发分支推送到 GitHub 开发分支(-f表示强制):

git push -f origin develop:develop