如何向Github提交代码

Step1: git init

含义:初始化在这个文件夹中建立一个空库

felix@felix-computer:~$ cd workspace/
felix@felix-computer:~/workspace$ cd desgin_parttern_learning/
felix@felix-computer:~/workspace/desgin_parttern_learning$ git init
初始化空的 Git 版本库于 /home/felix/workspace/desgin_parttern_learning/.git/

Step2:git add <filename>

含义:添加文件内容至缓冲区(注:若不明白缓冲区的概念请点击)

felix@felix-computer:~/workspace/desgin_parttern_learning$ git add sr*

Step3:git commit -m "代码提交说明"

felix@felix-computer:~/workspace/desgin_parttern_learning$ git commit -m “first commit”
[master (根提交) ded4c3c] first commit
14 files changed, 198 insertions(+)
create mode 100644 src/org/felix/parttern/command/step1/Light.java

create mode 100644 src/org/felix/parttern/command/step3/SimpleRemoteControl.java

Step4:git remote add origin <server>

含义:将你的仓库连接到某个远程服务器比如github,如此你就能够将你的改动推送到所添加的服务器上去了

felix@felix-computer:~/workspace/desgin_parttern_learning$ git remote add origin https://github.com/FelixLv/desgin_parttern_learning.git

Step5:git pull origin master

含义:更新远端服务器的主分支 (注:类似svn update 提交之前需更新,不然你会报错的)

felix@felix-computer:~/workspace/desgin_parttern_learning$ git pull origin master
来自 https://github.com/FelixLv/desgin_parttern_learning

  • branch master -> FETCH_HEAD
    Merge made by the 'recursive' strategy.
    README.md | 2 ++
    1 file changed, 2 insertions(+)
    create mode 100644 README.md

Step6:git push -u origin master

含义:将你要提交的代码推送到远端服务器上

felix@felix-computer:~/workspace/desgin_parttern_learning$ git push -u origin master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com':
Counting objects: 27, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (26/26), 2.65 KiB | 0 bytes/s, done.
Total 26 (delta 9), reused 0 (delta 0)
To https://github.com/FelixLv/desgin_parttern_learning.git
0aa428f..d448160 master -> master
Branch master set up to track remote branch master from origin.

总结

1.执行 git push -u origin master 时报如下错误:

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

解决办法: push前先pull 执行命令 git pull origin master

你可能感兴趣的:(git)