Linux下github的使用

1.注册github账号

2.更新linux下的git

[itcast@itcast:~]$ sudo apt-get install git
[sudo] password for itcast:
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
建议安装的软件包:
git-daemon-run git-daemon-sysvinit git-doc git-el git-email git-gui gitk
gitweb git-arch git-bzr git-cvs git-mediawiki git-svn
下列软件包将被升级:
git
升级了 1 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 566 个软件包未被升级。
需要下载 2,586 kB 的软件包。
解压缩后会消耗掉 311 kB 的额外空间。
获取:1 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main git amd64 1:1.9.1-1ubuntu0.3 [2,586 kB]
下载 2,586 kB,耗时 31秒 (81.4 kB/s)
(正在读取数据库 … 系统当前共安装有 201457 个文件和目录。)
正准备解包 …/git_1%3a1.9.1-1ubuntu0.3_amd64.deb …
正在将 git (1:1.9.1-1ubuntu0.3) 解包到 (1:1.9.1-1ubuntu0.1) 上 …
正在设置 git (1:1.9.1-1ubuntu0.3) …

3.创建文件夹,用来初始化为git

[itcast@itcast:~/protwo/day2]$ mkdir git
itcast@itcast:~/protwo/day2$ ls
dstorage git

4.按git方式初始化文件夹

itcast@itcast:~/protwo/day2/git$ git init
初始化空的 Git 版本库于 /home/itcast/protwo/day2/git/.git/
itcast@itcast:~/protwo/day2/git$ vi README.md
itcast@itcast:~/protwo/day2/git$ git add README.md

5.将本地git关联到github

origin这个是远程获取源端口,本地管理则不需要
https://github.com/hackywit/firstGitPro.git这个是github链接url,github.com这个如果是本地,就输入ip地址,hackywit用户名,firstGitPro项目名,.git是后缀不能少

itcast@itcast:~/protwo/day2/git$ git remote add origin https://github.com/hackywit/firstGitPro.git

6.如果注册的时候没邮箱需要输入联系邮箱和联系昵称,少了不让上传

itcast@itcast:~/protwo/day2/git$ git config –global user.name “hackywit”
itcast@itcast:~/protwo/day2/git$ git config –global user.email hackywit@aliyun.com

7.将一个README文件添加到本地git

itcast@itcast:~/protwo/day2/git$ git add README.md

8.将本地指定文件标记为需要提交—–双引号中的为提交时候的日志,主要交代为什么要提交

itcast@itcast:~/protwo/day2/git$ git commit README.md -m”first project”
[master (根提交) c6df75e] …
1 file changed, 2 insertions(+)
create mode 100644 README.md

9.上传需要提价的文件到自己的github中

必须要先git pull一下,和本地同步一下,以免覆盖别人的代码

itcast@itcast:~/protwo/day2/git$ git push -u origin master
Username for ‘https://github.com‘: hackywit
Password for ‘https://hackywit@github.com‘:
Counting objects: 3, done.
Writing objects: 100% (3/3), 233 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/hackywit/firstGitPro.git
* [new branch] master -> master

分支 master 设置为跟踪来自 origin 的远程分支 master。

10.比对github和本地git获取本地git的当前状态

itcast@itcast:~/protwo/day2/git$ git status
位于分支 master
您的分支与上游分支 ‘origin/master’ 一致。

无文件要提交,干净的工作区

11.如何将整个文件夹提交到github

 11.1添加整个文件夹文件到git仓库中

itcast@itcast:~/protwo/day2/git gitadd./include/itcast@itcast: /protwo/day2/git git add –all
查看一下git仓库状态
itcast@itcast:~/protwo/day2/git$ git status
位于分支 master
您的分支与上游分支 ‘origin/master’ 一致。

要提交的变更:
(使用 “git reset HEAD …” 撤出暂存区)

新文件:       include/make_log.h

未跟踪的文件:
(使用 “git add …” 以包含要提交的内容)

Makefile
conf/
fdfs_client_test.c
fdfs_client_test.o
fdfs_test
logs/
make_log.c
make_log.o
start-up.sh
test
test_main.c
test_main.o

 11.2.将仓库中的include中文件标记为可提交状态

-a标示标记所有跟踪文件

itcast@itcast:~/protwo/day2/git$ git commit -a -m”include中的文件”
[master 8281b08] include中的文件
1 file changed, 22 insertions(+)
create mode 100644 include/make_log.h

 11.3.同步到github ——>-u表示同步所有待提交文件

itcast@itcast:~/protwo/day2/git$ git push -u origin master
Username for ‘https://github.com‘: hackywit
Password for ‘https://hackywit@github.com‘:
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 620 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/hackywit/firstGitPro.git
c6df75e..8281b08 master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

 11.4同步出现冲突的解决办法

github中修改之后,本地同步不出错的情况
itcast@itcast:~/protwo/day2/git$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
来自 https://github.com/hackywit/firstGitPro
81a9630..28707b9 master -> origin/master
更新 81a9630..28707b9
Fast-forward
test_main.c | 1 +
1 file changed, 1 insertion(+)
github中修改之后,本地同步出错的情况,此时不能上传下载
itcast@itcast:~/protwo/day2/git$ git status
位于分支 master
您的分支与上游分支 ‘origin/master’ 一致。
尚未暂存以备提交的变更:
(使用 “git add …” 更新要提交的内容)
(使用 “git checkout – …” 丢弃工作区的改动)

修改: test_main.c

修改尚未加入提交(使用 “git add” 和/或 “git commit -a”)
itcast@itcast:~/protwo/day2/git$ git push -u origin master
Username for ‘https://github.com‘: hackywit
Password for ‘https://hackywit@github.com‘:
To https://github.com/hackywit/firstGitPro.git
! [rejected] master -> master (fetch first)
error: 无法推送一些引用到 ‘https://github.com/hackywit/firstGitPro.git
提示:更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外
提示:一个版本库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更
提示:(如 ‘git pull …’)。
提示:详见 ‘git push –help’ 中的 ‘Note about fast-forwards’ 小节。
itcast@itcast:~/protwo/day2/git$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
来自 https://github.com/hackywit/firstGitPro
28707b9..1f62f2d master -> origin/master
自动合并 test_main.c
冲突(内容):合并冲突于 test_main.c
自动合并失败,修正冲突然后提交修正的结果。

返回上一个版本
itcast@itcast:~/protwo/day2/git$ git stash
test_main.c: needs merge
test_main.c: needs merge
test_main.c: unmerged (ba9e25dbeba570714d18dda9deec0c853e476e53)
test_main.c: unmerged (95fa9937d40a57150e828387044df65a675a2e13)
test_main.c: unmerged (e10cbd6497a3ce8006a12406c9c8175b4fdd9175)
fatal: git-write-tree: error building trees
无法保存当前索引状态

将最新的文件拉回本地
itcast@itcast:~/protwo/day2/git$ git pull

将本地最新的修改回复,并合并到本地文件夹
itcast@itcast:~/protwo/day2/git$ git stash pop
此时文件中代码
LOG(“111”, “222”, “abcd %s”, “456”);^M
21 <<<<<<< HEAD
22 LOG(“111”, “222”, “abcd %s”, “520”);^M
23 =======
24 LOG(“111”, “222”, “abcd %s”, “888”);^M
25 >>>>>>> 1f62f2de2b296bf2519b33a6b6389be69ab5180d
重复提交的步骤

有时候无法按照上面的办法解决
git rm test_main.c 这边的test_main.c是冲突文件,删除本地冲突文件
git add –all 处理一下
git commit -m”删除”
git pull 重新下载

12.删除github中的文件

先删除本地所有文件

itcast@itcast:~/protwo/day2/git$ git rm * -r
rm ‘Makefile’
rm ‘README.md’
rm ‘conf/client.conf’
rm ‘conf/storage.conf’
rm ‘conf/tracker.conf’
rm ‘fdfs_client_test.c’
rm ‘fdfs_client_test.o’
rm ‘fdfs_test’
rm ‘include/make_log.h’
rm ‘logs/111/2016/09/222-03.log’
rm ‘logs/test/2016/09/fdfs_test-03.log’
rm ‘make_log.c’
rm ‘make_log.o’
rm ‘start-up.sh’
rm ‘test’
rm ‘test_main.o’

更新本地git仓库的文件

itcast@itcast:~/protwo/day2/git$ git add –all

标记git仓库中要同步上去的文件

itcast@itcast:~/protwo/day2/git$ git commit -m”删除所有”
[master 57db33d] 删除所有
16 files changed, 949 deletions(-)
delete mode 100644 Makefile
delete mode 100644 README.md
delete mode 100644 conf/client.conf
delete mode 100644 conf/storage.conf
delete mode 100644 conf/tracker.conf
delete mode 100644 fdfs_client_test.c
delete mode 100644 fdfs_client_test.o
delete mode 100755 fdfs_test
delete mode 100644 include/make_log.h
delete mode 100644 logs/111/2016/09/222-03.log
delete mode 100755 logs/test/2016/09/fdfs_test-03.log
delete mode 100644 make_log.c
delete mode 100644 make_log.o
delete mode 100755 start-up.sh
delete mode 100755 test
delete mode 100644 test_main.o

执行同步操作

itcast@itcast:~/protwo/day2/git$ git push -u origin master
Username for ‘https://github.com‘: hackywit
Password for ‘https://hackywit@github.com‘:
Counting objects: 3, done.
Compressing objects: 100% (1/1), done.
Writing objects: 100% (2/2), 206 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/hackywit/firstGitPro.git
b09698f..57db33d master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

13.新建分支和下载别人的代码

在github新建分支之后需要先在本地同步一下

itcast@itcast:~/protwo/day2/git$ git pull
来自 https://github.com/hackywit/firstGitPro
* [新分支] branch -> origin/branch
Already up-to-date.

查看当前所属分支

itcast@itcast:~/protwo/day2/git$ git branch
* master

查看所有分支

itcast@itcast:~/protwo/day2/git$ git branch -a
* master
remotes/origin/branch
remotes/origin/master

切换分支
切换时最好不要在一个目录下不断切换branch,死都不知道怎么死的

itcast@itcast:~/protwo/day2/git$ git checkout branch
分支 branch 设置为跟踪来自 origin 的远程分支 branch。
切换到一个新分支 ‘branch’
itcast@itcast:~/protwo/day2/git$ git branch
* branch
master

克隆代码到git2目录下

itcast@itcast:~/protwo/day2/git2$ git clone -b branch https://github.com/hackywit/firstGitPro.git
正克隆到 ‘firstGitPro’…
remote: Counting objects: 49, done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 49 (delta 11), reused 40 (delta 6), pack-reused 0
Unpacking objects: 100% (49/49), done.
检查连接… 完成。
克隆的时候会init,内部会初始化一个.git隐藏文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值