清屏:ctrl+l
1 在linux下安装git
yum -y install git
查看版本 git --version
4 设置git的用户名和邮箱地址
git config --global user.name "savadika"
git config --global user.email "ylf8708@126.com"
2 生成ssh的key文件
ssh-keygen -t rsa -C "ylf8708@126.com"
此时会出来两个确认:
enter file which to save the key(/root/.ssh/id_rsa): 点回车
enter the passphrase(empty for no passphrase): 意思是确认密码,点回车
enter same passphrase again: 再次确认密码,点回车
然后找到/root/.ssh 下面的文件,找到id_rsa.pub(公钥文件),复制其中的key
3 然后打开网页上的github,在github上填写key,见图
此时进入/root 下可以看到这个y文件和y.pub文件,打开y.pub(公钥密匙),看到内容为很长一串文件,copy下来:
copy的办法:使用ssh终端连接登录到linux,然后ctrl+c,需要的地方再ctrl+v;
4 测试连接,OK
[root@localhost .ssh]# ssh -T git@github.com
The authenticity of host 'github.com (192.30.252.129)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.129' (RSA) to the list of known hosts.
Hi savadika! You've successfully authenticated, but GitHub does not provide shell access.
[root@localhost .ssh]#
到目前为止,git已经安装成功,先来学习本地仓库
在/tmp下新建gitpro文件夹,进入gitpro,然后建立本地仓库
初始化命令: git init
利用ls -ah 命令可以看到这个.git的隐藏文件,这个就是仓库
[root@localhost gitpro]# git init
Initialized empty Git repository in /tmp/gitpro/.git/
[root@localhost gitpro]# ls -ah
. .. .git
[root@localhost gitpro]# cd .git/
[root@localhost .git]# ls
branches config description HEAD hooks info objects refs
[root@localhost .git]#
在 gitpro 文件夹下,注意:不要进.git文件
新建readme.txt,然后随便写点内容,保存
提交到git仓库,两步
git add readme.txt
git commit -m "create a txt"
修改readme.txt 后,就可以查看状态了,
git status 显示git状态
[root@localhost gitpro]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
如果你要看具体修改了什么
git diff
[root@localhost gitpro]# git diff
diff --git a/readme.txt b/readme.txt
index 69f9b26..d64807a 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-hello ,this is a new txt
+hello ,this is a new distrube txt
let us start
[root@localhost gitpro]#
总结下,就是三步:1 修改文件 2 git add 文件 3 git commit -m "备注"
如果需要看日志的话
git log
[root@localhost gitpro]# git log
commit adedce902c5bf1f57cedd4f971b2ce98f6b86509
Author: savadika <ylf8708@126.com>
Date: Tue Jan 5 17:18:07 2016 +0800
this is a third work
commit 55ac577532aa97fbb4295303a664a5ad7505e540
Author: savadika <ylf8708@126.com>
Date: Tue Jan 5 17:14:51 2016 +0800
add distribu
commit c80957880f0618ba0f682a94498d50e8afe5a674
Author: savadika <ylf8708@126.com>
Date: Tue Jan 5 17:02:35 2016 +0800
create a txt
好了,现在我们启动时光穿梭机,准备把readme.txt回退到上一个版本,也就是“add distributed”的那个版本,怎么做呢?
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),
上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
[root@localhost gitpro]# git reset --hard HEAD^
HEAD is now at 55ac577 add distribu
也就是说,此时已经退回到了55ac57这个版本了
那万一回退错了,想撤销呢?先要保证这个版本号你记得
使用git reset --hard adedce(想回退过去的那个版本的前6位)
万一实在忘记了呢,没事,还是有后悔药
git reflog,能够看到那个版本的东西
[root@localhost gitpro]# git reflog
adedce9 HEAD@{0}: adedce: updating HEAD
55ac577 HEAD@{1}: HEAD^: updating HEAD
adedce9 HEAD@{2}: commit: this is a third work
55ac577 HEAD@{3}: commit: add distribu
c809578 HEAD@{4}: commit (initial): create a txt
现在,你又理解了Git是如何跟踪修改的,每次修改,如果不add到暂存区,那就不会加入到commit中。
关于删除:
在工作区的删除回滚:
git checkout --readme.txt
已经add,后怎么删除全部文件
git rm test.txt
git checkout --readme.txt
测试远程仓库
上传:
问题1 :出现已有origin仓库出错
1 删除原有的origin仓库(有出现错误的时候做这个操作)
git remote rm origin
问题2 :直接在远程仓库里面修改后再推送报错,因为已经修改过,所以需要先pull到本地
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.
解决:[root@localhost gitpro]# git pull 将github仓库上的文件覆盖到本地
然后再
[root@localhost gitpro]# git push origin master
2 在web上新建一个项目用来测试推送,地址为:git@github.com:savadika/gitpro.git
推送上去:
[root@localhost gitpro]# git remote add origin git@github.com:savadika/gitpro.git //添加远程git仓库
[root@localhost gitpro]# git push -u origin master //第一次添加
$ git push origin master //以后添加
下载:
git clone https://github.com/yiiext/chart 或者使用ssh协议更快 git clone git@github.com:yiiext/chart
然后就能看到下载的这个软件了,可以进行查看等
oK.大工告成!