使用git命令提交代码到github

第一步

首先我们先要安装git客户端,可以去官网下载。安装完成之后进行第二步。

第二步

如果你是第一次提交代码到git仓库,那么在项目根目录,右键然后选择 git bash here,打开之后会进入到git命令行界面。

  1. 然后输入git init 初始化仓库。

  2. 接着输入 git add .,如果此时你想增加特定的文件只需把 . 更换成你想要添加的文件或文件名即可。

  3. 接着输入 git commit -m "描述你此次提交的内容"

  4. 接着输入命令,将本地的仓库关联到github上。(远程仓库需要自己在github上事先创建好)

git remote add origin ssh地址或https地址

  1. 然后执行 git pull origin master。如果此时出现 fatal: refusing to merge unrelated histories 错误,那么我们可以执行 git pull origin master --allow-unrelated-histories 这条命令。

  2. 最后执行 git push -u origin master 上传到远程仓库

详细命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git init
Reinitialized existing Git repository in D:/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld/.git/

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git add Classes/

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git add Resources/

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git commit -m "first commit"
On branch master
Untracked files:
.cocos-project.json
AppDelegate.cpp
AppDelegate.h
CMakeLists.txt
HelloWorldScene.cpp
HelloWorldScene.h
cocos2d/
proj.android-studio/
proj.android/
proj.ios_mac/
proj.linux/
proj.win32/

nothing added to commit but untracked files present

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git remote add origin git@github.com:huxiaolei1997/Plane.git
fatal: remote origin already exists.

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git pull origin master --allow-unrelated-histories
From github.com:huxiaolei1997/Plane
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md

Mystery@Mystery MINGW64 /d/cocos2d-x/cocos2d-x-3.16/tools/cocos2d-console/bin/myprojects/HelloWorld (master)
$ git push -u origin master
Counting objects: 46, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (44/44), done.
Writing objects: 100% (46/46), 7.28 MiB | 424.00 KiB/s, done.
Total 46 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:huxiaolei1997/Plane.git
5f1bd95..30d5cad master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

总结

以上只是讲解了如何使用git命令提交代码到远程仓库,并没有讲解git的安装以及详细配置,如果需要了解这些,请参考下面给出的资料。

  1. 起步 - 初次运行 Git 前的配置

  2. Generating a new SSH key and adding it to the ssh-agent

显示评论