我有一个文件夹,里面有我的项目资料。我怎么能把这个项目推到Github的存储库?

我尝试了以下步骤:

我在GitHub上创建了空仓库。 我运行git-bash并输入git init,所以在根项目中出现了。git文件夹。 我使用git add sourcesFolderName添加了一些文件到版本控制中 我使用git commit -m "initial commit"来提交上一步添加的文件 我指定远程存储库使用git远程添加MyProject <url> 最后git推送,但什么都没有推送到远程repo…(未授权失败)

那么我怎么能把现有的资源推到新创建的github回购?


当前回答

创建一个新的存储库

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

其他回答

我不想再补充另一个答案,但我的特定场景在这里没有完全涵盖。我有一个本地回购,其中有我想保存的更改历史,还有一个在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拉取,但我需要指定——allow-unrelated-histories选项。如果没有这个选项,git pull会发出警告:没有共同提交。

下面是对我有效的命令序列:

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

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

➤ 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推送。

用不那么专业的术语来说

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

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

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

选项1

登录到你的github帐户,并创建回购。

在下面的屏幕中,如果你点击按钮(屏幕右侧)“克隆在桌面”,你可以将它复制到你需要的地方。

然后,您可以(或以另一种方式)将现有项目中的内容复制到新的repo中。使用github应用程序,你可以使用他们的GUI从那里提交(这意味着你只需要单击应用程序中的按钮)。当然,您要为提交输入您的笔记。

选项2

如上所述,在github上创建你的回购。 在您的计算机上,使用终端进入您的目录。您可以使用Linux命令行CD到该目录中。在这里,您可以运行以下命令来“连接”您现有的项目到github上的repo。(这是假设你在github上创建了你的repo,目前是空的)

首先要初始化git(版本控制)。

git init

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

git add .

然后提交,并在“”之间加上一个注释,比如“第一次提交”等等。

 git commit -m "Initial Commit"

现在,这里是您添加现有回购的地方

git remote add github <project url>

但是不要直接输入<项目url>,而是输入你自己的项目url。你是怎么做到的?去你的回购在github上的链接,然后复制链接。在我的例子中,我的一个repo是https://github.com/JGallardo/urbanhistorical,所以这个命令的结果url只会在后面添加.git。所以在这里

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

通过做来测试它是否有效

git remote -v

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

然后你可以把你的改变推送到github

git push github master

or

git push origin master

如果你仍然得到一个错误,你可以用-f强制它。但是如果你在一个团队环境中工作,小心不要强迫,否则你会制造更多的问题。

git push -f origin master

如果你用的是Mac(这可能在PC上也一样),这里有一个非常简单的方法来做到这一点。奇怪的是,我到处寻找这个简单的方法,但始终没有找到。

Do not do anything on Github (other than having an account, and not having used up all your available repos). Download GitHub for Mac and install. Go through the account setup, etc. Do NOT create any repositories for your existing project. "Add New Local Repository" in repositories. Select your existing folder. It'll ask if you want to do that, say yes. Once done, you'll see a list of all your files, etc. Commit them. Go to Repositories and Publish (this will create the new repo on GitHub for you, if you set up your account properly). Go to Repositories and Push (you'll either see the "nothing to push" thing, or it'll push your files/changes to the newly-auto-made repo). Wonder why you could not find this simple process anywhere else.

我知道不建议使用项目文件夹作为回购文件夹。我一直这样做,它总是有效的,它使它变得简单,而且我从来没有遇到过任何麻烦。

这个对我来说很有用(需要的时候留作参考)

# 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