将现有项目推送到Github

问题描述 投票:196回答:16

我有一个包含项目源的文件夹。我如何将这个项目推入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?

git github
16个回答
307
投票
git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master

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

To [email protected]:roseperrone/project.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]: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.

3
投票
  1. 从命令行,导航到本地存储库目录。
  2. 在GitHub中创建新的存储库,它将为您提供以.git结尾的链接。
  3. 在cmd中运行:git remote add origin [your_GitHub_Repository_link](记住链接应以.git结尾)
  4. 然后运行:git push -u origin master

希望这很有用。


2
投票

首先,使用您的项目名称在Github上创建一个新的存储库。然后按照以下步骤操作。

1)git init
2)git add *
3)git commit -m "first commit"
4)git remote add origin https://github.com/yuvraj777/GDriveDemo.git
5)git push -u origin master

2
投票

讨厌添加另一个答案,但我的特定情况在这里并没有完全涵盖。我有一个本地仓库,里面有我想要保留的变更历史,还有一个非空的仓库在Github上为我创建(即使用默认的README.md)。是的,您可以随时重新创建Github仓库作为空仓库,但在我的情况下,其他人有权创建此特定仓库,如果有一个简单的解决方法,我不想让他烦恼。

在这种情况下,在设置远程原点后尝试git push时将遇到此错误:

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:<my repo>.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 integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

如错误所示,我需要在设置远程原点后执行git pull,但我需要指定--allow-unrelated-histories选项。没有这个选项,git pull抱怨warning: no common commits

所以这是对我有用的命令的确切顺序:

git remote add origin <github repo url>
cp README.md README.md-save
git pull origin master --allow-unrelated-histories
mv README.md-save README.md
git commit -a
git push

1
投票

如果你想离开命令行,另一个选择是使用SourceTree

以下是有关如何设置的一些其他资源:


1
投票

创建一个新的存储库

git clone <url>
cd "repositoryName"
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master

现有文件夹

cd existing_folder
git init
git remote add origin <url>
git add .
git commit -m "Initial commit"
git push -u origin master

现有的Git存储库

cd existing_repo
git remote rename origin old-origin
git remote add origin <url>
git push -u origin --all
git push -u origin --tags

1
投票

只需按照此URl:CLICK HERE中的步骤操作即可


0
投票

我发现刺激“自然”序列的更新比强制推动事物更容易。

假设repo已经在github上创建了,你可能还在README.md中添加了一些内容。

  1. 在您的计算机上,打开终端和git clone [repo URL]
  2. 您将看到一个新的文件夹,其中包含您的回购邮件名称。随意重命名 - 没关系。
  3. 将您的代码,文件等移动到此文件夹中。如果必须,编辑README.md。
  4. 现在打开终端/命令提示符,进入该文件夹并执行操作,就好像您正在对repo进行下一次更新:
git add .
git commit -m "v2"
git push origin master

注意:在commit命令中,git可能会拒绝,要求先配置用户的电子邮件和密码。按照屏幕上给出的步骤操作,然后再次运行commit命令。

  1. 这三个命令就是您每次要推送另一个更新时所执行的操作。

103
投票

用较少的技术术语

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

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

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

Option 1

登录您的github帐户并创建回购。 enter image description here

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

enter image description here

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

Option 2

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

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

git init

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

git add .

然后你提交并在""之间添加一个注释,如“first commit”等。

 git commit -m "Initial Commit"

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

git remote add github <project url>

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

git remote add github https://github.com/JGallardo/urbanhistorical.git

测试看它是否有效

git remote -v

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

enter image description here

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

git push github master

要么

git push origin master

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

git push -f origin master

33
投票

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

➤ git init ./
➤ git add Readme.md
➤ git commit -m "Initial Commit"
➤ git remote add github <project url>
➤ git push github master

将按预期工作。

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

➤ git branch -u github/master master

这将允许您从master执行git push而无需指定远程或分支。


7
投票

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

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

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


5
投票

综上所述;

git init
git status
git add "*"
git commit -m "Comment you want"
git remote add origin  https://link
git push  -u origin master

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

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


4
投票

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

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

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

第3步:打开一个新的终端窗口(是的,你必须使用终端......我尝试了所有其他方式......没有用)

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

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

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

第7步:输入git commit -m“初始提交”你将获得以下内容:#On branch master无提交,工作目录清理

要么

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

步骤8:键入git remote add origin {project url}项目URL可以在Github.com中找到。它是HTTPS克隆URL ...你应该能够只复制并粘贴到终端窗口。如果系统告诉您原点已存在,请创建一个不同的名称或使用您的项目名称(不同的东西)

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

我希望有所帮助。


4
投票
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)

3
投票
git init

在新的本地存储库中添加文件。这将它们用于第一次提交。

git add .

Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.

提交您在本地存储库中暂存的文件。

git commit -m "First commit"
# Commits the tracked changes and prepares them to be pushed to a remote

库。要删除此提交并修改文件,请使用'git reset --soft HEAD~1'并再次提交并添加文件。复制远程存储库URL字段在GitHub存储库的“快速设置”页面的顶部,单击以复制远程存储库URL。

在命令提示符下,添加将推送本地存储库的远程存储库的URL。

git remote add origin remote repository URL
# Sets the new remote
git remote -v
# Verifies the new remote URL

将本地存储库中的更改推送到GitHub。

git push origin master
# Pushes the changes in your local repository up to the remote repository you 

指定为原点


3
投票

我知道,这是一个老问题,但我试图解释每一步,所以它可能会帮助别人。这就是我将现有源添加到git的方法:

  1. 在git上创建repo,这样你就有了ssh || https,你要远程添加源代码。
  2. 在您的终端中,转到项目的路径。
  3. 运行git init(在这里你启动项目作为一个git)。
  4. 运行git add *(在这里添加项目中的所有文件和文件夹)。
  5. 运行git commit -m "Initial Commit."(这里你提交在步骤#4中添加的文件和文件夹;请注意,如果不提交它们就不能推送你的更改)。
  6. 运行git remote add origin https://[email protected]/your_username/project-name.git(在这里你添加一个远程项目,你的源将被推送;用步骤#1中的ssh || https替换我的链接)。
  7. 运行git push -u origin master(在这里将源推送到git存储库)。

注意:这些是将源推送到主分支的简单步骤。

© www.soinside.com 2019 - 2024. All rights reserved.