最先进的分布式版本控制系统之git部署和github远程仓库

原理部分转载自:https://www.liaoxuefeng.com
Git简介:

Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
Git 与常用版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

分布式版本控制系统的特点:

首先,分布式版本控制系统根本没有“中央服务器”,每个人的电脑上都是一个完整的版本库,这样,你工作的时候,
就不需要联网了,因为版本库就在你自己的电脑上。既然每个人电脑上都有一个完整的版本库,那多个人如何协作呢?
比方说你在自己电脑上改了文件A,你的同事也在他的电脑上改了文件A,这时,你们俩之间只需把各自的修改推送给对方,
就可以互相看到对方的修改了。

和集中式版本控制系统相比,分布式版本控制系统的安全性要高很多,因为每个人电脑里都有完整的版本库,某一个人
电脑坏掉了不要紧,随便从其他人那里复制一个就可以了。而集中式版本控制系统的中央服务器要是出了问题,所有人
都没法干活了。

在实际使用分布式版本控制系统的时候,其实很少在两人之间的电脑上推送版本库的修改,因为可能你们俩不在一个局
域网内,两台电脑互相访问不了,也可能今天你的同事病了,他的电脑压根没有开机。因此,分布式版本控制系统通常
也有一台充当“中央服务器”的电脑,但这个服务器的作用仅仅是用来方便“交换”大家的修改,没有它大家也一样干活,
只是交换修改不方便而已。

git的简单部署:

[root@foundation38 kiosk]# mkdir demo
[root@foundation38 kiosk]# cd demo/
[root@foundation38 demo]# ls
[root@foundation38 demo]# git init  git初始化,通过git init命令把这个目录变成Git可以管理的仓库
Initialized empty Git repository in /home/kiosk/demo/.git/
[root@foundation38 demo]# ls
[root@foundation38 demo]# l.
.  ..  .git
[root@foundation38 demo]# cd .git/
[root@foundation38 .git]# ls
branches  config  description  HEAD  hooks  info  objects  refs
[root@foundation38 .git]# pwd
/home/kiosk/demo/.git
[root@foundation38 .git]# ls
branches  config  description  HEAD  hooks  info  objects  refs
[root@foundation38 .git]# cd ..
[root@foundation38 demo]# pwd
/home/kiosk/demo
[root@foundation38 demo]# ls
[root@foundation38 demo]# echo westos > readme.md   写入文件

这里写图片描述
查看git的状态:

[root@foundation38 demo]# git status   查看状态Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   readme.md
nothing added to commit but untracked files present (use "git add" to track)
[root@foundation38 demo]# git status -s  查看状态为??表示文件未提交
?? readme.md
[root@foundation38 demo]# git status  查看状态Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   readme.md
nothing added to commit but untracked files present (use "git add" to track)
[root@foundation38 demo]# git add readme.md  将文件添加到git仓库,git add命令实际上就是把要提交的所有修改放到暂存区

这里写图片描述

[root@foundation38 demo]# git status  查看状态
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   readme.md
#
[root@foundation38 demo]# git status -s  查看状态为A表示文件已经处于暂存状态
A  readme.md
[root@foundation38 demo]# git commit -m "add readme.md"  

执行git commit就可以一次性把暂存区的所有修改提交到分支  
git commit命令执行成功后会告诉你,1 file changed:1个文件被改动(我们新添加的readme.txt文件);
1 insertions:插入了1行内容(readme.txt有1行内容)

[master (root-commit) cc1fc5d] add readme.md
 Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 readme.md

这里写图片描述
将文件提交到版本库中以后,就无法查看状态:

[root@foundation38 demo]# git status -s
[root@foundation38 demo]# git config --global user.name xfl
[root@foundation38 demo]# git config --global user.email "[email protected]"
[root@foundation38 demo]# cd
[root@foundation38 ~]# l.
.              .config     .ipython          .ssh          .xauthP5nb8L
..             .cshrc      .java             .tcshrc       .xauthTo12Hm
.bash_history  .dbus       .kde              .viminfo      .xauthUt9EKh
.bash_logout   .docker     .local            .xauthcLT5DY
.bash_profile  .gitconfig  .mysql_history    .xauthlqP98u
.bashrc        .gnome      .PyCharmCE2016.3  .xauthnDfsoO
.cache         .gvfs       .rnd              .Xauthority

这里写图片描述
Git里面的一些标志提示:

新添加的未跟踪文件前面有 ?? 标记,新添加到暂存区中的文件前面有 A 标记,修改过的文件前面有 M 标记。 
你可能注意到了 M有两个可以出现的位置,出现在右边的 M 表示该文件被修改了但是还没放入暂存区,出现在
靠左边的 M 表示该文件被修改了并放入了暂存区。 
[root@foundation38 ~]# cat .gitconfig 默认写到.gitconfig里面
[user]
    name = xfl
    email = 1757159864@qq.com
[root@foundation38 ~]# cd /home/kiosk/demo/
[root@foundation38 demo]# ls
readme.md
[root@foundation38 demo]# vim test.txt
[root@foundation38 demo]# cat test.txt   修改test文件
test

这里写图片描述

[root@foundation38 demo]# git status -s   test文件表示未被跟踪
?? test.txt
[root@foundation38 demo]# vim readme.md 
westos
westos
[root@foundation38 demo]# git status -s    表示readme.md文件被修改了但是还没放入暂存区
 M readme.md
?? test.txt
[root@foundation38 demo]# git add readme.md   将readme.md文件放入暂存区
[root@foundation38 demo]# git status -s      表示readme.md文件被修改了而且已经放入暂存区
M  readme.md
?? test.txt
[root@foundation38 demo]# vim readme.md 
[root@foundation38 demo]# cat readme.md 
westos
westos
westos
[root@foundation38 demo]# git status -s    MM表示工作区被修改并提交到暂存区后又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录
MM readme.md   
?? test.txt
[root@foundation38 demo]# git add readme.md
[root@foundation38 demo]# git status -s   提交之后变成左边的M表示放到暂存区
M  readme.md
?? test.txt
[root@foundation38 demo]# git add test.txt

这里写图片描述

[root@foundation38 demo]# git status -s   A表示test.txt文件处于暂存状态
M  readme.md
A  test.txt
[root@foundation38 demo]# git commit -m "add test.txt"  提交
[master b17121b] add test.txt
 2 files changed, 3 insertions(+)
 create mode 100644 test.txt
[root@foundation38 demo]# git log   可以查看具体的日志
commit b17121b8323d6c66da7b7c69768e8530f4e3c5f5
Author: xfl <1757159864@qq.com>
Date:   Fri Aug 24 09:31:16 2018 +0800

    add test.txt

commit cc1fc5d8561210730662ddd65df089a22eb51bd7
Author: root <root@foundation38.ilt.example.com>
Date:   Fri Aug 24 09:27:09 2018 +0800

    add readme.md
[root@foundation38 demo]# git status -s   已经提交查看不到状态

这里写图片描述
忽略文件:

一般我们总会有些文件无需纳入Git的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,
比如日志文件,或者编译过程中创建的临时文件等。 
Git忽略所有以 .o.a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的. 
Git忽略所有以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存副本
[root@foundation38 demo]# git status -s
[root@foundation38 demo]# ls
readme.md  test.txt
[root@foundation38 demo]# touch .file1   建立一个隐藏文件
[root@foundation38 demo]# git status -s  
?? .file1
[root@foundation38 demo]# cd .git/
[root@foundation38 .git]# cd ..
[root@foundation38 demo]# vim .gitignore  .*表示隐藏所有文件
[root@foundation38 demo]# cat .gitignore 
.*
[root@foundation38 demo]# git status -s   查看状态自动忽略隐藏文件
[root@foundation38 demo]# ls
readme.md  test.txt
[root@foundation38 demo]# git log --pretty=oneline  查看简略日志信息
b17121b8323d6c66da7b7c69768e8530f4e3c5f5 add test.txt
cc1fc5d8561210730662ddd65df089a22eb51bd7 add readme.md

这里写图片描述
版本回退:

像这样,你不断对文件进行修改,然后不断提交修改到版本库里,就好比玩RPG游戏时,每通过一关就会自动把游戏状态存盘,
如果某一关没过去,你还可以选择读取前一关的状态。有些时候,在打Boss之前,你会手动存盘,以便万一打Boss失败了,
可以从最近的地方重新开始。Git也是一样,每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在
Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把
几个月的工作成果全部丢失。
[root@foundation38 demo]# git log  查看历史记录
commit b17121b8323d6c66da7b7c69768e8530f4e3c5f5
Author: xfl <1757159864@qq.com>
Date:   Fri Aug 24 09:31:16 2018 +0800

    add test.txt

commit cc1fc5d8561210730662ddd65df089a22eb51bd7
Author: root <root@foundation38.ilt.example.com>
Date:   Fri Aug 24 09:27:09 2018 +0800

    add readme.md
[root@foundation38 demo]# git reset --hard HEAD^  

首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

HEAD is now at cc1fc5d add readme.md
[root@foundation38 demo]# git status -s  已经回退到上一次提交状态
[root@foundation38 demo]# ls
readme.md
[root@foundation38 demo]# cat readme.md 
westos
[root@foundation38 demo]# git log --pretty=oneline    查看简略日志信息
cc1fc5d8561210730662ddd65df089a22eb51bd7 add readme.md

这里写图片描述
撤销修改,将修改过的内容恢复原样:

[root@foundation38 demo]# git reflog   查看历史记录
cc1fc5d HEAD@{0}: reset: moving to HEAD^
b17121b HEAD@{1}: commit: add test.txt
cc1fc5d HEAD@{2}: commit (initial): add readme.md
[root@foundation38 demo]# git reset --hard b17121b  将test.txt退回到上层
HEAD is now at b17121b add test.txt
[root@foundation38 demo]# git reflog   查看提交ID已经变化
b17121b HEAD@{0}: reset: moving to b17121b
cc1fc5d HEAD@{1}: reset: moving to HEAD^
b17121b HEAD@{2}: commit: add test.txt
cc1fc5d HEAD@{3}: commit (initial): add readme.md
[root@foundation38 demo]# ls
readme.md  test.txt
[root@foundation38 demo]# cat readme.md   文件内容退回到上次提交前
westos
westos
westos
[root@foundation38 demo]# cat test.txt 
test

这里写图片描述

[root@foundation38 demo]# vim test.txt 
[root@foundation38 demo]# git status -s
 M test.txt
[root@foundation38 demo]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@foundation38 demo]# cat test.txt   加一个空行,改变内容即可
test

这里写图片描述
git checkout – file可以丢弃工作区的修改,就是让这个文件回到最近一次git commit或git add时的状态:

[root@foundation38 demo]# git checkout -- test.txt  丢弃修改
[root@foundation38 demo]# cat test.txt   文件回到修改之前的状态
test
[root@foundation38 demo]# vim test.txt 
[root@foundation38 demo]# cat test.txt 
test
test
[root@foundation38 demo]# git add test.txt  修改错了而且将文件放到了暂存区
[root@foundation38 demo]# git status   查看状态文件在暂存区
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   test.txt
#
[root@foundation38 demo]# git reset HEAD test.txt  

用命令git reset HEAD <file>可以把暂存区的修改撤销掉(unstage),重新放回工作区:
git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。

Unstaged changes after reset:
M   test.txt   将文件放到工作区
[root@foundation38 demo]# cat test.txt 
test
test

这里写图片描述

[root@foundation38 demo]# git status 再用git status查看一下,现在暂存区是干净的,工作区有修改:
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@foundation38 demo]# git checkout -- test.txt  回退版本
[root@foundation38 demo]# cat test.txt  文件已经恢复
test

这里写图片描述
不小心务删除了文件:

[root@foundation38 demo]# rm -f test.txt 
[root@foundation38 demo]# git status  

Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@foundation38 demo]# git checkout -- test.txt 直接回退版本
[root@foundation38 demo]# ls  查看文件已经恢复
readme.md  test.txt
[root@foundation38 demo]# rm -f test.txt 
[root@foundation38 demo]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

这里写图片描述
命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

[root@foundation38 demo]# git rm test.txt   直接使用git命令从版本库删除文件
rm 'test.txt'
[root@foundation38 demo]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    test.txt
#
[root@foundation38 demo]# git status -s  查看状态已经删除
D  test.txt
[root@foundation38 demo]# git commit -m "delete test.txt"   提交即可从版本库删除文件
[master ba68dda] delete test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
[root@foundation38 demo]# git status -s  看不到信息已经删除成功

注册开源网站github:
这里写图片描述
会通过邮箱认证:
这里写图片描述
按照步骤走即可:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
建立免密传输:

[root@foundation38 demo]# git remote add origin https://github.com/xuefeilong/test.git
[root@foundation38 demo]# cd 
[root@foundation38 ~]# cd .ssh/
[root@foundation38 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@foundation38 .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNeD0hcnCreyvvLHo5fMcQ4MszPyzAihqHQa9plB7Hxn+Qau4SSo8h2jJbhIDtvGfQdrCcB7823+skP6QFrfhUWBUbwZ7UjleSbumA3YhHK/ooCEzDx1kQY9pXxOEIp7jZ4PWfOhvINyFC1RH/kPC7Tx7697lq3mcWQCRS1wArF3vg3AHsNDtswwiYLitHkiZDlUVBBBnwH4GM1xe4YKtQXDOoqjnfxoq2LHFh8JKb/92NvqzYr1w+E8ps/jx+AoeUnv9pU4qtHierA3B/DQawI+wH3ChABZAjmr7zONbdINj1QJIjFmjDOgXQIc2eyYTFJb7/qaCwvm+Cwm6SVitx root@foundation38.ilt.example.com

这里写图片描述
这里写图片描述
这里写图片描述
由于之前虽然建立了免密传输但是点击的是http认证方式,所以需要输入密码:

[root@foundation38 demo]# git push -u origin master  有时候push不上去加参数-f强制上传数据,但有可能造成文件丢失
Username for 'https://github.com': xuefeilong
Password for 'https://[email protected]': 
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (9/9), 677 bytes | 0 bytes/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/xuefeilong/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

在网页查看已经有了对应数据变化:
这里写图片描述
这里写图片描述

[root@foundation38 demo]# vim test.txt
[root@foundation38 demo]# cat test.txt 
test
test
[root@foundation38 demo]# git add test.txt
[root@foundation38 demo]# git commit -m "add test.txt"
[master da0f806] add test.txt
 1 file changed, 2 insertions(+)
 create mode 100644 test.txt
[root@foundation38 demo]# git push -u origin master   可以远程push
Username for 'https://github.com': xuefeilong
Password for 'https://[email protected]': 
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 274 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/xuefeilong/test.git
   ba68dda..da0f806  master -> master
Branch master set up to track remote branch master from origin.

在网页添加一个文件:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
在本地拉取可以查看网页修改内容:

[root@foundation38 demo]# git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
From https://github.com/xuefeilong/test
 * branch            master     -> FETCH_HEAD
Updating da0f806..acbf46a
Fast-forward
 aa.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 aa.txt
[root@foundation38 demo]# ls
aa.txt  readme.md  test.txt
[root@foundation38 demo]# cat aa.txt 
hello

这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/82192344
今日推荐