将现有项目推送到Github

MyT*_*tle 212 git github

我有一个包含项目源的文件夹.我如何将这个项目推入Github的存储库?

我尝试使用这个步骤:

  1. 我在GitHub上创建了空存储库.
  2. 我运行git-bash并输入git init,所以在项目根目录下出现了.git文件夹.
  3. 我使用了一些文件添加到版本控制 git add sourcesFolderName
  4. 我提交了在上一步中添加的文件 git commit -m "initial commit"
  5. 我使用指定了远程存储库 git remote add MyProject <url>
  6. 最后git push,但没有任何东西被推到远程回购......(没有授权失败)

那么我如何将现有的资源推送到新创建的github repo?

Ros*_*one 322

git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master
Run Code Online (Sandbox Code Playgroud)

强制推动的-f选项git push.如果你不使用它,你会看到如下错误:

To git@github.com:roseperrone/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:roseperrone/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

  • 这解决了我的问题,但是我无法执行命令4`git remote add origin <project url>```命令语法不正确`.我忽略它并且它有效 (4认同)
  • 请注意,如果您不想使用GitHub网站,可以在第三步之后添加:`curl -u'USER'https://api.github.com/user/repos -d '{"name":"REPO"}'`,将USER替换为您的用户名,将REPO替换为要创建的存储库的名称. (3认同)
  • 我相信`-f` 标志会强制远程历史消失吗?这似乎是一个危险的操作员。 (2认同)

JGa*_*rdo 104

用较少的技术术语

我的答案没有什么不同,但我正在添加更多信息,因为那些新的信息可以从填补信息空白中获益.

在github上创建repo后,他们会有说明.你可以关注那些.但是这里有一些额外的提示,因为我知道开始使用git是多么令人沮丧.

假设您已经在本地启动了项目.你有多少无所谓.但是让我们假装你有一个php项目.假设您有index.php,contact.php和包含图像,css和字体的assets文件夹.你可以这样做(简单),但有很多选择:

选项1

登录您的github帐户并创建回购.

在此输入图像描述

在下面的屏幕中,如果单击按钮(屏幕右侧)以"在桌面克隆",则可以将其复制到需要的位置.

在此输入图像描述

您可以(或以其他方式执行)然后将现有项目中的内容复制到新的仓库中.使用github应用程序,您可以使用他们的GUI(这意味着您只需单击应用程序中的按钮)从那里提交.当然,您输入提交的注释.

选项2

  • 如上所述,在github上创建您的仓库.
  • 在计算机上,使用终端转到目录.使用linux命令行,你将cd进入目录.从这里运行以下命令,将现有项目"连接"到github上的repo.(这假设你在github上创建了你的repo并且它当前是空的)

首先执行此操作以初始化git(版本控制).

git init
Run Code Online (Sandbox Code Playgroud)

然后执行此操作以添加所有文件以进行"监视".如果你有想要忽略的文件,你需要添加一个.gitignore但是为了简单起见,只需使用这个例子来学习.

git add .
Run Code Online (Sandbox Code Playgroud)

然后你提交并在""类似"第一次提交"等之间添加一个注释.

 git commit -m "Initial Commit"
Run Code Online (Sandbox Code Playgroud)

现在,您可以在此处添加现有仓库

git remote add github <project url>
Run Code Online (Sandbox Code Playgroud)

但是不要字面输入<project url>,而是你自己的项目URL.你怎么做到的?转到您的repo在github上的链接,然后复制链接.在我的情况下,我的一个回购是https://github.com/JGallardo/urbanhistorical所以我的结果url这个命令只会添加.git之后.所以这就是

git remote add github https://github.com/JGallardo/urbanhistorical.git
Run Code Online (Sandbox Code Playgroud)

测试看它是否有效

git remote -v
Run Code Online (Sandbox Code Playgroud)

你应该看看你的回购链接到了什么.

在此输入图像描述

然后,您可以将更改推送到github

git push github master
Run Code Online (Sandbox Code Playgroud)

要么

git push origin master
Run Code Online (Sandbox Code Playgroud)

如果仍然出现错误,可以强行使用-f.但是,如果您在团队环境中工作,请注意不要强迫或者您可能会产生更多问题.

git push -f origin master
Run Code Online (Sandbox Code Playgroud)

  • 你的是最好的解释.对于其他读过这篇文章的人:我首先在github上创建了我的存储库,所以它不会让我推动.我不得不> github拉<project url>然后github推.在那之后,一切都很好. (3认同)
  • 很棒的解释!但是我会添加以下行:git remote add origin <project url>然后git push -f origin master.没有-f,就会出错 (2认同)

bri*_*ice 34

您需要在推送时指定哪个分支哪个远程:

? git init ./
? git add Readme.md
? git commit -m "Initial Commit"
? git remote add github <project url>
? git push github master
Run Code Online (Sandbox Code Playgroud)

将按预期工作.

您可以通过以下操作默认设置:

? git branch -u github/master master
Run Code Online (Sandbox Code Playgroud)

这将允许您在git push不指定远程或分支的情况下从master 执行.


小智 7

如果你在Mac上(这可能在PC上运行相同),这是一个非常简单的方法.奇怪的是,对于这个简单的过程,我看起来很高,而且从未找到它.

  • 不要在Github上做任何事情(除了拥有一个帐户,并且没有用完所有可用的回购).
  • 下载GitHub for Mac并安装.完成帐户设置等.不要为现有项目创建任何存储库.
  • 存储库中的"添加新本地存储库".
  • 选择现有文件夹.它会问你是否愿意这样做,说是的.
  • 完成后,您将看到所有文件的列表等.提交它们.
  • 转到存储库和发布(如果您正确设置帐户,这将为您在GitHub上创建新的存档).
  • 转到存储库和推送(您将看到"无需推送"的事情,或者它会将您的文件/更改推送到新自动制作的存储库).
    • 不知道为什么你在其他地方找不到这个简单的过程.

我知道不建议将项目文件夹用作repo文件夹.我一直这样做,它总是有效,它使它变得简单,我从来没有遇到任何麻烦.


sha*_*pal 6

综上所述;

git init
git status
git add "*"
git commit -m "Comment you want"
git remote add origin  https://link
git push  -u origin master
Run Code Online (Sandbox Code Playgroud)

我想与您共享一个资源,以便您更轻松地了解Git。

https://try.github.io/levels/1/challenges/1


viv*_*kar 6

自 2005 年成立以来,Git 一直是首选的版本控制系统。大约 87% 的开发人员使用 Git 作为他们的版本控制系统。

但是,如果您有一个已经存在的项目,并且想要推送到远程服务器中的 Git,请按照以下步骤操作:

  1. 转到项目目录的终端

  2. 您需要使用 git init

  3. 创建一个.gitignore文件,它实际上是一个文本文件,它告诉 Git 在项目中忽略哪些文件或文件夹。

  4. 使用暂存文件 git add .

  5. 使用适当的提交消息将更改提交到本地存储库: git commit -m "my first commit"

  6. 在这一步中,您只需要在任何一个分布式版本控制系统(如 GitHub 或 Bitbucket)中创建一个存储库

  7. 使用此 Git 命令将您的本地存储库与远程存储库链接起来: git remote add <your-remote-name> <your-remote-url>

所以,如果你的 GitHub repo-url 是https://github.com/your-github-username/new-repository.git,那么 Git 命令变成:

git remote add origin https://github.com/<your-github-username>/new-repository.git
Run Code Online (Sandbox Code Playgroud)
  1. 将您的代码推送到远程 GitHub 存储库

    git push origin master

注意:git push命令需要两个参数:远程存储库的名称 ( origin ) 和要推送到的分支(这里的master是每个存储库的默认分支)。

有关详细信息,请参阅此博客


小智 5

我将遵循 Rose P 之前的评论。我花了很长时间才找到解决方案,所以我重新发布(希望用简单的英语)对我有用的内容......

第 1 步:在 Github.com 上创建您的新存储库(如果您已经有,请跳过)

第 2 步:关闭 XCode...不需要

第 3 步:打开一个新的终端窗口(是的,您必须使用终端……我尝试了所有其他方法……没有任何效果)

第 4 步:使用命令cd查找项目的文件夹位置(要添加到现有或新存储库的项目)

第 5 步:输入git init 你会得到这样的东西。 重新初始化 /{current directory} 中的现有 Git 存储库

第 6 步:输入git add 。 在这一步之后没有任何反应,但输入它并继续下一步。

第 7 步:输入 git commit -m "Initial commit" 你会得到以下信息:# 在分支 master 上没有什么可提交的,工作目录是干净的

或者

关于配置的一些解释,然后是已更改的文件列表。

第八步:输入git remote add origin {project url} 项目 url 可以在 Github.com 找到。它是 HTTPS 克隆 URL……您应该能够复制并粘贴到终端窗口。如果系统告诉您来源已经存在,请创建不同的名称或使用您的项目名称(不同的名称)

第 9 步:转到 Mac 上的 GitHub 应用程序并单击“同步分支”按钮(即使没有待处理的更改)。我认为它需要一段时间才能真正提交,但是如果您返回本地存储库文件夹,您应该会看到您的新项目。我不得不重新创建父文件夹,但这只是移动文件的问题。转到 GitHub.com 并刷新浏览器,您的新文件也应该在那里。

我希望这有帮助。


Ank*_*tix 5

Follow below gitbash commands to push the folder files on github repository :-
1.) $ git init
2.) $ git cd D:\FileFolderName
3.) $ git status
4.) If needed to switch the git branch, use this command : 
    $ git checkout -b DesiredBranch
5.) $ git add .
6.) $ git commit -m "added a new folder"
7.) $ git push -f https://github.com/username/MyTestApp.git TestBranch
    (i.e git push origin branch)
Run Code Online (Sandbox Code Playgroud)


the*_*est 5

截至 2019 年 7 月 29 日,Github 向用户提供了在创建存储库时完成此任务的说明,并提供了多个选项:

在命令行上创建一个新的存储库

git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/user/repo.git
git push -u origin master
Run Code Online (Sandbox Code Playgroud)

从命令行推送现有存储库

git remote add origin https://github.com/user/repo.git
git push -u origin master
Run Code Online (Sandbox Code Playgroud)

从另一个存储库导入代码

import按钮初始化进程。

对于视觉学习者来说:

在此输入图像描述