我有一个包含项目源的文件夹。我如何将这个项目推入Github的存储库?
我尝试使用这个步骤:
git init
,因此在项目根目录中出现了.git
文件夹。 git add sourcesFolderName
git commit -m "initial commit"
git remote add MyProject <url>
git push
,但没有任何东西被推送到远程仓库......(没有授权失败)那么我如何将现有资源推送到新创建的github repo?
答案 0 :(得分:303)
git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master
-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.
答案 1 :(得分:100)
我的回答并没有什么不同,但我正在添加更多信息,因为那些新的信息可以从填补信息空白中获益。
在github上创建repo后,他们有说明。你可以关注那些。但是这里有一些额外的提示,因为我知道开始使用git是多么令人沮丧。
让我们说你已经在本地开始了你的项目。你有多少无所谓。但是,让我们假装你有一个php项目。让我们说你有index.php,contact.php和一个包含图像,css和字体的assets文件夹。你可以这样做(简单),但有很多选择:
在下面的屏幕中,如果单击按钮(屏幕右侧)到&#34;克隆在桌面&#34;中,您可以将其复制到您需要的位置。
您可以(或以其他方式执行)然后将现有项目中的内容复制到新的仓库中。使用github应用程序,您可以使用他们的GUI(这意味着您只需单击应用程序中的按钮)从那里提交。当然,您输入提交的注释。
首先执行此操作以初始化git(版本控制)。
git init
然后执行此操作以添加要监控的所有文件。&#34;如果您有要忽略的文件,则需要添加.gitignore
,但为了简单起见,只需使用此示例进行学习。
git add .
然后你提交并在""
喜欢&#34;第一次提交&#34;之间添加一个注释。等
git commit -m "Initial Commit"
现在,您可以在此处添加现有仓库
git remote add github <project url>
但不要按字面意思键入<project url>
,而是输入您自己的项目网址。你怎么做到的?转到您的repo在github上的链接,然后复制链接。在我的情况下,我的其中一个回购邮件是 https://github.com/JGallardo/urbanhistorical ,因此我生成此命令的网址只会在此之后添加 .git 。所以这就是
git remote add github https://github.com/JGallardo/urbanhistorical.git
通过执行
测试它是否有效git remote -v
你应该看看你的回购链接到了什么。
然后你可以将更改推送到github
git push github master
或
git push origin master
如果仍然出现错误,可以使用-f
强制它。但是,如果您在团队环境中工作,请注意不要强迫或者您可能会产生更多问题。
git push -f origin master
答案 2 :(得分:32)
在推送时,您需要指定哪个分支和哪个远程:
➤ 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
允许您在不指定远程或分支的情况下从主服务器执行git push
。
答案 3 :(得分:7)
如果您使用的是Mac(这可能在PC上运行相同),这是一种非常简单的方法。奇怪的是,对于这个简单的过程,我看起来很高,而且从未找到它。
我知道不建议将项目文件夹用作repo文件夹。我一直这样做,它总是有效,它简单,我从来没有遇到任何麻烦。
答案 4 :(得分:5)
自2005年推出以来,Git一直是首选的版本控制系统。大约有87%的开发人员将Git用作其版本控制系统。
但是,如果您已经有一个项目,并且想在远程服务器中推送到Git,请按照以下步骤操作:
转到项目目录的终端
您需要使用git init
创建一个 .gitignore 文件,它实际上是一个文本文件,告诉Git项目中要忽略的文件或文件夹。
使用git add .
使用适当的提交消息将更改提交到本地存储库:git commit -m "my first commit"
在此步骤中,您只需要在任何分布式版本控制系统(例如GitHub或Bitbucket)中创建一个存储库
使用此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
将您的代码推送到远程GitHub存储库
git push origin master
注意:git push
命令需要两个参数:远程存储库的名称( origin )和要推送到的分支(此处为 master 是每个存储库的默认分支)。
请参阅blog以获取详细信息。
答案 5 :(得分: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} 项目网址可以在Github.com找到。它是HTTPS克隆URL ...你应该能够只复制并粘贴到终端窗口。如果系统告诉您原点已存在,请创建其他名称或使用项目名称(不同的名称)
步骤9:转到Mac上的GitHub应用程序,然后单击“同步分支”按钮(即使没有挂起的更改)。我认为实际提交需要一段时间,但是如果你回到本地存储库文件夹,你会看到你的新项目。我不得不重新创建父文件夹,但这只是移动文件的问题。转到GitHub.com并刷新浏览器,您的新文件也应该在那里。
我希望有所帮助。
答案 6 :(得分: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)
答案 7 :(得分:4)
总结;
git init
git status
git add "*"
git commit -m "Comment you want"
git remote add origin https://link
git push -u origin master
我想与您分享一个来源,以便您更轻松地了解Git。
答案 8 :(得分:3)
我知道,这是一个古老的问题,但我试图解释每一步,所以它可能会帮助别人。这就是我将现有源添加到git的方法:
git init
(此处您将项目作为git启动)。git add *
(此处添加项目中的所有文件和文件夹)。git commit -m "Initial Commit."
(此处您提交在步骤#4中添加的文件和文件夹;请注意,如果不提交更改,则无法推送更改。)git remote add origin https://your_username@bitbucket.org/your_username/project-name.git
(在这里添加一个远程项目,其中您的源将被推送;将我的链接替换为步骤#1中的ssh || https)。git push -u origin master
(此处将源代码推送到git存储库中)。注意:这些是将源推送到主分支的简单步骤。
答案 9 :(得分:3)
.git
结尾的链接。 git remote add origin [your_GitHub_Repository_link]
(请记住链接应以.git
)git push -u origin master
希望这很有用。
答案 10 :(得分:3)
git init
在新的本地存储库中添加文件。这会将它们分配给第一次提交。
git add .
提交您在本地存储库中暂存的文件。
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
指定为原点
答案 11 :(得分:2)
讨厌添加另一个答案,但我的特定情况并未完全涵盖在这里。我有一个本地仓库,里面有我想要保留的变更历史,还有一个非空的仓库在Github上为我创建(即使用默认的README.md)。是的,您可以随时重新创建Github仓库作为空仓库,但在我的情况下,其他人有权创建此特定仓库,如果有一个简单的解决方法,我不想让他烦恼。
在这种情况下,在设置远程原点后尝试git push
时会遇到此错误:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:<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
答案 12 :(得分: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
答案 13 :(得分:2)
从2019年7月29日开始,Github向用户提供创建存储库时完成此任务的说明,并提供以下几种选择:
在命令行上创建新的存储库
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/novomotus/vitaprice.git
git push -u origin master
从命令行推送现有存储库
git remote add origin https://github.com/novomotus/vitaprice.git
git push -u origin master
从另一个存储库导入代码
按import
按钮初始化过程。
对于在那里的视觉学习者:
答案 14 :(得分:1)
如果您想要离开命令行,另一个选择是使用SourceTree。
以下是一些有关如何设置的其他资源:
答案 15 :(得分: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
答案 16 :(得分:1)
只需按照此URl中的步骤操作:CLICK HERE
答案 17 :(得分:0)
我发现以“自然”顺序刺激更新是比强行推动事情更容易的途径。
假设回购已在github上创建,并且您可能还把一些东西放到了README.md中。
在您的计算机上,打开终端,然后打开git clone [repo URL]
您将看到将创建一个带有您的存储库名称的新文件夹。随时重命名-没关系。
将您的代码,文件等移至该文件夹。如果需要,请编辑README.md。
现在打开终端/命令提示符,进入该文件夹,然后执行操作,就像对存储库进行下一次更新一样:
git add .
git commit -m "v2"
git push origin master
注意:在提交命令git可能会拒绝,要求首先配置用户电子邮件和密码。遵循屏幕上给出的步骤,然后再次运行commit命令。
答案 18 :(得分:0)
这对我有用(只是在需要时保留以供参考)
# Go into your existing directory and run below commands
cd docker-spring-boot
echo "# docker-spring-boot" >> README.md
git init
git add -A
git commit -m "first commit"
git branch -M master
git remote add origin https://github.com/devopsmaster/docker-spring-boot.git
git push -u origin master