您的当前位置:首页正文

将本地代码上传到github上

来源:图艺博知识网

1、github上创建自己的Repository:

Repository.png

2、填写项目名称、描述、选择是否公开、需不需要创建README,注意这个页面的地址,之后要用到。


Repository1.png

3、建立git仓库
命令cd到你的本地项目根目录下,执行git命令,此命令会在当前目录下创建一个.git文件夹。

git init

4、将项目的所有文件添加到仓库中, 注意这个.是指当前目录下所有文件

git add .

5、将add的文件commit到仓库

git commit -m "注释"

6、将本地的仓库关联到github上

git remote add origin 第2步中的地址。

7、上传代码到github远程仓库

git push -u origin master

遇到一个问题,在第2步将readme选上之后,第7步就报错:

! [rejected]        master -> master (fetch first)
error: failed to push some refs to 
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.

是因为远程repository和我本地的repository冲突导致的,而我在创建版本库后,在github的版本库页面点击了创建README.md文件的按钮创建了说明文档,但是却没有pull到本地。这样就产生了版本冲突的问题,所以第7步前需要先执行下面这句:

git pull origin master --allow-unrelated-histories

这样就好了。

貌似还有其他方法(我没试)

  • 使用强制push的方法:
$ git push -u origin master -f 

这样会使远程修改丢失,一般是不可取的,尤其是多人协作开发的时候。

  • 若不想merge远程和本地修改,可以先创建新的分支:
$ git branch [name]

然后push

$ git push -u origin [name]

然后就没有然后了。。。

Top