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

我尝试了以下步骤:

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

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


当前回答

我知道,这是一个老问题,但我试图解释每一步,所以它可以帮助别人。这是我如何添加一个现有的源代码到git:

Create the repo on the git, so you'll have the ssh || https where you're gonna remote add you source code. In your terminal go to the path of your project. Run git init (here you initiate the project as a git one). Run git add * (here you add all the files and folders from you project). Run git commit -m "Initial Commit." (here you commit your files and folders added in step #4; keep in mention that you can't push your changes without committing them). Run git remote add origin https://your_username@bitbucket.org/your_username/project-name.git (here you add a remote project where your source it's gonna be pushed; replace my link with your ssh || https from the step #1). Run git push -u origin master (here you push your source into the git repository).

注意:这些都是将源代码推入主分支的简单步骤。

其他回答

截至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

从命令行推送现有存储库

git remote add origin https://github.com/user/repo.git
git push -u origin master

从其他存储库导入代码

按导入按钮初始化进程。

对于视觉学习者来说:

首先,用你的项目名在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

Git自2005年问世以来一直是版本控制系统的首选。大约87%的开发者使用Git作为他们的版本控制系统。

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

Go to the terminal of your project directory You need to initialize your project git using git init Create a .gitignore file and it is actually a text file that tells Git which files or folders to ignore in a project. Stage your files using git add . Commit your changes to your local repository with an appropriate commit message: git commit -m "my first commit" In this step, you just need to create a repository in any one of the distributed version control systems like GitHub or Bitbucket Use this Git command to link your local repository with that of the remote: 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是每个存储库的默认分支)。

详细信息请参考这个博客。

我不想再补充另一个答案,但我的特定场景在这里没有完全涵盖。我有一个本地回购,其中有我想保存的更改历史,还有一个在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 .

在本地存储库中添加文件,并对它们进行提交。要取消文件,使用'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 

指定为原点