您的位置:首页 > Web前端 > Node.js

将本地nodejs服务器的代码提交到github

2017-02-28 00:25 260 查看
将本地nodejs服务器的代码提交到github

本文中所提交的代码的执行效果,请参考:http://blog.csdn.net/u013553529/article/details/57646352

最终提交到github的URL为:https://github.com/galian123/nodejs_http_server

可以通过
git clone https://github.com/galian123/nodejs_http_server[/code]获取到。 
或者,直接打开https://github.com/galian123/nodejs_http_server,点击页面中中的“Clone or download”,然后点击“Download ZIP”。

1. 编写README.md

README.md是markdown格式的,需要写上此代码仓库的用途以及用法,方便他人理解。

也可以在提交到github之后,再添加README.md也是可以的。

注:本文中代码仓库是指github中的repository。

2. 初始化代码仓库:
git init

$ git init

初始化空的 Git 仓库于 /cygdrive/d/git/nodejs/.git/


查看当前状态:

$ git status
位于分支 master

初始提交

未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)

README.md
images/
index.js
requestHandlers.js
router.js
server.js

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)


3. 添加文件到本地代码仓库:
git add

$ git add .


查看当前状态:

$ git status
位于分支 master

初始提交

要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)

新文件:   README.md
新文件:   images/abracadabra.jpg
新文件:   images/view.jpg
新文件:   index.js
新文件:   requestHandlers.js
新文件:   router.js
新文件:   server.js


4. 提交到本地代码仓库:
git commit

$ git commit -m "http server created by nodejs"

[master(根提交) f536e1d] http server created by nodejs
7 files changed, 159 insertions(+)
create mode 100755 README.md
create mode 100755 images/abracadabra.jpg
create mode 100755 images/view.jpg
create mode 100755 index.js
create mode 100755 requestHandlers.js
create mode 100755 router.js
create mode 100755 server.js


5. 在github中创建代码仓库

登陆自己的github,例如我的github:https://github.com/galian123

点击右上角的加号,点击“New repository”,如图:



设置“Repository name”,我起的仓库名为
nodejs_http_server


添加“Description”,这是可选的,方便理解,例如“用nodejs搭建简易的HTTP服务器”。

不要勾选“Initialize this repository with a README”,因为之前我已经添加了README.md文件。如果之前没有事先写好README.md,则此时可以勾选“Initialize this repository with a README”,代码仓库创建完成之后,再编写README.md。

“Add .gitignore”,设置gitignore的类型。注意,如果你要导入github的是某个工程目录,例如Android project、C++ project,那么,需要选择.gitignore,目的是防止不必要的文件传到github中。

“Add a license”,选择一个开源协议。



然后点击“Create repository”。

创建完代码仓库的样子,如图:

代码仓库的URL为:https://github.com/galian123/nodejs_http_server



6. 将本地代码仓库与github的代码仓库(远程仓库)进行绑定:
git remote add

$ git remote add origin https://github.com/galian123/nodejs_http_server[/code] 
验证一下远程仓库是否绑定上,执行
git remote -v


$ git remote -v

origin  https://github.com/galian123/nodejs_http_server (fetch)
origin  https://github.com/galian123/nodejs_http_server (push)


7. 将本地代码仓库中的文件提交到github:
git push

提交代码之前,先执行
git pull origin master
,让远程代码仓库与本地代码仓库进行同步。

然后执行
git push origin master


$ git push origin master

Username for 'https://github.com':
Password for 'https://[my_github_username]@github.com':
对象计数中: 12, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (12/12), 完成.
写入对象中: 100% (12/12), 4.05 MiB | 142.00 KiB/s, 完成.
Total 12 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/galian123/nodejs_http_server 0e0d160..b4a6e17  master -> master


8. 刷新github中刚才创建的代码仓库

即,刷新https://github.com/galian123/nodejs_http_server



README.md的显示效果(部分):



可能出现的错误

! [rejected]        master -> master (fetch first)
error: 无法推送一些引用到 'https://github.com/galian123/nodejs_http_server'
提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外
提示:一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 'git pull ...')。
提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。


或者

! [rejected]        master -> master (non-fast-forward)
error: 无法推送一些引用到 'https://github.com/galian123/nodejs_http_server'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。


原因:本地仓库没有与远程仓库同步。

解决方法:执行
git pull origin master
进行同步。

$ git pull origin master

来自 https://github.com/galian123/nodejs_http_server * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
LICENSE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 201 insertions(+)
create mode 100644 LICENSE


参考

(1)https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/

(2)用nodejs搭建简易的HTTP服务器: http://blog.csdn.net/u013553529/article/details/57646352
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: