一、什么是Git
Git是目前世界上最先进的分布式版本控制系统。Git是免费、开源的。
说到git就不得不提起版本控制系统......上一篇博客有介绍到
最初Git是为辅助 Linux 内核开发的,来替代 BitKeeper
作者:Linux和Git之父李纳斯·托沃兹(Linus Benedic Torvalds)1969、芬兰
优点:
- 适合分布式开发,强调个体。
- 公共服务器压力和数据量都不会太大。
- 速度快、灵活。
- 任意两个开发者之间可以很容易的解决冲突。
- 离线工作。
缺点:
- 模式上比SVN更加复杂。
- 不符合常规思维。
- 代码保密性差,一旦开发者把整个库克隆下来就可以完全公开所有代码和版本信息。
二、git的部署安装
1、git的安装以及初始化,以及工作目录的创建
[root@foundation58 demo]# pwd #创建的demo目录就是工作区
/root/demo
[root@foundation58 demo]# git init
Initialized empty Git repository in /root/demo/.git/
[root@foundation58 demo]# l.
. .. .git
[root@foundation58 demo]# ls .git/
branches config description HEAD hooks info objects refs
可以发现当前目录下多了一个.git的目录,这个目录是Git来管理版本库的,一般不要手动修改这个.git目录里面的文件。
2、添加用户信息
初始化完后,接下来就要设置用户名和邮件地址,每次Git提交都会附带上这些信息,
其实这些信息可以说时仓库所有者的标签,也可以说是联系人信息
[root@foundation58 demo]# touch file1
[root@foundation58 demo]# git status -s 查看此时的状态,??表示还没有提交到暂存区,还在本地
?? file1
[root@foundation58 demo]# git status #可以查看看下一步应该怎么办
[root@foundation58 demo]# git add file1 #add表示添加到暂存区
[root@foundation58 demo]# git status -s #A表示已经在暂存区
A file1
[root@foundation58 demo]# git commit -m "add file1" #提交到仓库中
根据提示,说必须先设置用户信息,由邮箱信息
[root@foundation58 demo]# git config --global user.name "yyz"
[root@foundation58 demo]# git config --global user.email [email protected]
[root@foundation58 demo]# git status -s #查看此时的邮件的状态,已经提交到仓库中去了
[root@foundation58 demo]# cat ~/.gitconfig
[user]
name = yyz
email = [email protected]
3、查看文件仓库的状态:
[root@foundation58 demo]# ls
file1
[root@foundation58 demo]# git status -s #由于已经提交到仓库中去了,所以此时没有显示
[root@foundation58 demo]# echo 123 > file1 #修改文件
[root@foundation58 demo]# git status -s #右边的M表示该文件被修改了但是还没放入暂存区
M file1
[root@foundation58 demo]# git add file1 #加入暂存区后
[root@foundation58 demo]# git status -s #M在左边表示该文件被修改了并放入了暂存区
M file1
[root@foundation58 demo]# echo yunyuzhu >> file1
[root@foundation58 demo]# git status -s #查看工作区
MM file1
MM表示工作区被修改并提交到暂存区后又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录
4、忽略文件
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。
通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。
在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。
[root@foundation58 demo]# git status -s
[root@foundation58 demo]# ls
file1
[root@foundation58 demo]# mkdir env
[root@foundation58 demo]# ls
env file1
[root@foundation58 demo]# cd env/
[root@foundation58 env]# touch file
[root@foundation58 env]# cd ..
[root@foundation58 demo]# ls
env file1
[root@foundation58 demo]# touch file2
[root@foundation58 demo]# git status -s
?? env/
?? file2
[root@foundation58 demo]# rm -f file2
[root@foundation58 demo]# ls
env file1
[root@foundation58 demo]# git status -s
?? env/
[root@foundation58 demo]# touch .file
[root@foundation58 demo]# git status -s
?? .file
?? env/
[root@foundation58 demo]# l.
. .. .file .git
[root@foundation58 demo]# vim .gitignore
[root@foundation58 demo]# git status -s
[root@foundation58 demo]# cat .
./ ../ .file .git/ .gitignore
[root@foundation58 demo]# cat .gitignore
.*
env
5、查看日志&版本回退
查看工作日志的三种方法:
[root@foundation58 demo]# git log
[root@foundation58 demo]# git log --pretty=oneline
5b142658aa705be10e485968590f31b262889e29 add file1
bd642498a80e36170cd9b452222e521952756fcc add file1
38c79e805b388c78d61efc2e974267daea21488c add file1
[root@foundation58 demo]# git reflog
5b14265 HEAD@{0}: commit: add file1
bd64249 HEAD@{1}: commit: add file1
38c79e8 HEAD@{2}: commit (initial): add file1
像下边那样,不断对文件进行修改,然后不断提交修改到版本库里,
Git也是一样,每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。
一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失
回到上一个状态:git reset --hard HEAD
回到上上一个状态: git reset --hard HEAD^
[root@foundation58 demo]# touch file2
[root@foundation58 demo]# echo 1 > file2
[root@foundation58 demo]# git add file2
[root@foundation58 demo]# git commit -m "add file2"
[master 2912168] add file2
1 file changed, 1 insertion(+)
create mode 100644 file2
[root@foundation58 demo]# cat file2
1
[root@foundation58 demo]# git reset --hard HEAD
HEAD is now at 2912168 add file2
[root@foundation58 demo]# git reflog
2912168 HEAD@{0}: commit: add file2
5b14265 HEAD@{1}: commit: add file1
bd64249 HEAD@{2}: commit: add file1
38c79e8 HEAD@{3}: commit (initial): add file1
[root@foundation58 demo]# git reset --hard HEAD^
HEAD is now at 5b14265 add file1
[root@foundation58 demo]# git reflog
5b14265 HEAD@{0}: reset: moving to HEAD^
2912168 HEAD@{1}: commit: add file2
5b14265 HEAD@{2}: commit: add file1
bd64249 HEAD@{3}: commit: add file1
38c79e8 HEAD@{4}: commit (initial): add file1
[root@foundation58 demo]# cat file2
cat: file2: No such file or directory
[root@foundation58 demo]# ls #此时已经回退到上上一个状态,上上一个状态就是还没有创建file2
env file1
回到指定状态
git reset --hard ... ...表示状态号
[root@foundation58 demo]# git reflog
2c69c6a HEAD@{0}: commit: add file2
5b14265 HEAD@{1}: reset: moving to HEAD^
2912168 HEAD@{2}: commit: add file2
5b14265 HEAD@{3}: commit: add file1
bd64249 HEAD@{4}: commit: add file1
38c79e8 HEAD@{5}: commit (initial): add file1
[root@foundation58 demo]# git reset --hard 5b14265
HEAD is now at 5b14265 add file1
[root@foundation58 demo]# ls
env file1
6、删除后恢复————回滚
git checkout – file2
以丢弃工作区的修改,就是让这个文件回到最近一次git commit或git add时的状态
删除后没有提交时 直接:git checkout – file 即可
删除且提交后: git reset --hard 指定状态
eg:
[root@foundation58 demo]# ls
env file1
[root@foundation58 demo]# git reflog
5b14265 HEAD@{0}: reset: moving to 5b14265
2c69c6a HEAD@{1}: commit: add file2
5b14265 HEAD@{2}: reset: moving to HEAD^
2912168 HEAD@{3}: commit: add file2
5b14265 HEAD@{4}: commit: add file1
bd64249 HEAD@{5}: commit: add file1
38c79e8 HEAD@{6}: commit (initial): add file1
[root@foundation58 demo]# git reset --hard 2c69c6a
HEAD is now at 2c69c6a add file2
[root@foundation58 demo]# ls #此时可以看到file2恢复
env file1 file2
三、将本地仓库中的代码上传到github远程仓库中
前提:
登录github然后创建一个仓库:
1、给github添加公钥:
[root@foundation58 ~]# cd .ssh/
[root@foundation58 .ssh]# ssh-keygen
[root@foundation58 .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@foundation58 .ssh]# cat id_rsa.pub
登陆github然后创建新的仓库,给github分发公钥~~~】
2、提交到仓库中:
[root@foundation58 demo]# git remote add origin [email protected]:yyzstupid/demo.git #提交到远程仓库
[root@foundation58 demo]# git remote -v #查看远程地址
origin [email protected]:yyzstupid/demo.git (fetch)
origin [email protected]:yyzstupid/demo.git (push)
3、最后一步push推送过去
[root@foundation58 demo]# git push -u origin master
[root@foundation58 demo]# git push -u origin master
The authenticity of host 'github.com (52.74.223.119)' 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,52.74.223.119' (RSA) to the list of known hosts.
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (12/12), 821 bytes | 0 bytes/s, done.
Total 12 (delta 0), reused 0 (delta 0)
To [email protected]:yyzstupid/demo.git
* [new branch] master -> master
Branch master set up to track remote branch master from origi
推送成功!~~~~~~
结果如下:
4、还可以本地从github上拿取仓库的东西:
[root@foundation58 mnt]# git clone [email protected]:yyzstupid/demo.git
Cloning into 'demo'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 12 (delta 0), reused 12 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), done.
[root@foundation58 mnt]# ls
demo hepp passwd vm_create.sh vm_create_snopshot.sh
可以看到demo已经在/mnt目录下了