原文链接地址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
一、概念
Git是一个分布式版本控制系统。
版本控制系统可以记录各个版本的改动情况(主要针对于文本文件),并实现多人协作编辑,主要分为两大类,集中式和分布式。
二、分布式和集中式的区别
集中式的版本控制,本地没有历史记录,完整的仓库只存在服务器上,如果服务器挂了,就全都挂了;而分布式版本控制系统的每个节点都是完整仓库,每个节点都包含各自的历史记录,个人可以在脱机环境下查看开发的版本和历史,如果远程仓库挂了,可以重建一个服务器,然后把任何一个节点的的仓库clone过去即可恢复。
三、Git的诞生
Linus将Linux开源后,世界各地开源爱好者为Linux贡献代码,起初,Linus手工合并这些代码,后来,Linux壮大之后,BitMover公司授权Linux社区商业软件BitKeeper免费使用权,由于有人试图破解BitKeeper协议,BitMover公司收回了Linux社区BitKeeper免费使用权,然后,Linus花两周时间用C语言写出了Git。
有趣的小知识:最初 GNU Interactive Tools软件也叫GIT,所以Linux老版本安装git分布式版本控制系统的时候,需使用yum install git-core。后来Git的名气越来越大,GNU Interactive Tools改名成了gnuit,git-core正式改名为git。
四、安装
yum install git -y 即可。
五、Git常用命令的使用
1.创建版本库
创建空目录,cd到目录下,使用 git init 命令初始化一个版本库。
[root@syztoo ~]# mkdir gittest
[root@syztoo ~]# cd gittest/
[root@syztoo gittest]# git init
Initialized empty Git repository in /root/gittest/.git/
[root@syztoo gittest]# ls -a
. .. .git
2.工作区、暂存区、版本库、远程仓库
工作区就是 git init 初始化过的目录,比如上面 gittest 目录。
不过,使用 git init 初始化后,在目录下包含一个隐藏目录 .git ,这个隐藏目录不属于工作区,而是 git 的版本库。
[root@syztoo gittest]# cd .git
[root@syztoo .git]# ls -a
. .. branches COMMIT_EDITMSG config description HEAD hooks index info logs objects refs
在 .git 目录下,有一个 index 文件,这个就是暂存区。
远程仓库:github、gitlab、码云等。
3.上传文件到版本库
分两步,git add 上传文件到暂存区,git commit 将暂存区的文件上传至版本库并添加说明。
[root@syztoo gittest]# git add test.txt
[root@syztoo gittest]# git commit -m 'add test.txt'
4.git status 和 git diff
在git add之前可以用git status查看工作区文件的状态,使用git diff查看文件具体修改内容。
[root@syztoo gittest]# 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@syztoo gittest]# git diff
diff --git a/test.txt b/test.txt
index 25ab921..79c752a 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-this is one
+this is one.
5.版本回退
使用git log查看commit提交的历史记录;
使用git reset --hard HEAD^回退到上一次commit版本,HEAD^^上上一次版本,HEAD~100表示往上100个版本。
[root@syztoo gittest]# git log
commit cd46813cedbd3590fc7b1477e1a98b04de51777f
Author: syztoo <184573035@qq.com>
Date: Tue Apr 9 15:27:07 2019 +0800
add file test2.txt
commit 12024d0e6b64a1e2dc13db5e16e73e40cb3a7cf0
Author: syztoo <184573035@qq.com>
Date: Tue Apr 9 14:14:20 2019 +0800
add test.txt
[root@syztoo gittest]# ls
test2.txt test.txt
[root@syztoo gittest]# git reset --hard HEAD^
HEAD is now at 12024d0 add test.txt
[root@syztoo gittest]# git log
commit 12024d0e6b64a1e2dc13db5e16e73e40cb3a7cf0
Author: syztoo <184573035@qq.com>
Date: Tue Apr 9 14:14:20 2019 +0800
add test.txt
[root@syztoo gittest]# ls
test.txt
也可直接跟commit id;
[root@syztoo gittest]# git reset --hard cd46813cedbd3590fc7b1477e1a98b04de51777f
HEAD is now at cd46813 add file test2.txt
[root@syztoo gittest]# ls
test2.txt test.txt
6.查看commit id
一般使用git reset –hard commit_id进行版本穿梭,此时commit_id尤其重要,使用git reflog查看历史命令可以确保回退到过去版本后依然能查询到未来版本的commit_id。
[root@syztoo gittest]# git reflog
cd46813 HEAD@{0}: reset: moving to cd46813cedbd3590fc7b1477e1a98b04de51777f
12024d0 HEAD@{1}: reset: moving to HEAD^
cd46813 HEAD@{2}: commit: add file test2.txt
12024d0 HEAD@{3}: commit (initial): add test.txt
7.撤销工作区修改
使用git checkout -- <file> 撤销工作区修改;
[root@syztoo gittest]# cat test.txt
this is one
[root@syztoo gittest]# vim test.txt
[root@syztoo gittest]# cat test.txt
this is one
this is 2
[root@syztoo gittest]# git checkout -- test.txt
[root@syztoo gittest]# cat test.txt
this is one
8.撤销暂存区修改
先使用git reset HEAD -- <file> 撤销暂存区修改,再使用git checkout -- <file> 撤销工作区修改;
[root@syztoo gittest]# vim test.txt
[root@syztoo gittest]# cat test.txt
this is one
this is 2
[root@syztoo gittest]# git add test.txt
[root@syztoo gittest]# git reset HEAD test.txt
Unstaged changes after reset:
M test.txt
[root@syztoo gittest]# git checkout -- test.txt
[root@syztoo gittest]# cat test.txt
this is one
9.删除文件
工作区删除文件后,接下来想删除版本库的文件,使用 git rm <file> ,再使用 git commit 提交;
[root@syztoo gittest]# ls
test2.txt test.txt
[root@syztoo gittest]# rm -f test2.txt
[root@syztoo gittest]# 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: test2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@syztoo gittest]# git rm test2.txt
rm 'test2.txt'
[root@syztoo gittest]# git commit -m 'remove test2.txt'
[master 22a6d2b] remove test2.txt
1 file changed, 1 deletion(-)
delete mode 100644 test2.txt
如果一不小心删除了工作区的文件,使用 git checkout -- <file> 撤销工作区修改即可恢复删除的文件;
10.添加远程仓库
1)首先配置git全局设置;
[root@syztoo gittest]# git config --global user.name 'syztoo'
[root@syztoo gittest]# git config --global user.email '184573035@qq.com'
[root@syztoo gittest]# git config --list
user.name=syztoo
user.email=184573035@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
2)本地版本仓库和远程仓库之间的文件传输通过ssh协议加密,配置 ssh key 进行身份认证(确保本地版本仓库与远程仓库之间的推送与拉取是本人在操作);
[root@syztoo ~]# ssh-keygen -t rsa -C "184573035@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9Chc2hINncxttvFe16pNve2o8kfj2KyPzhGciAaDqrk 184573035@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| .+ o |
| . o= = |
| . + +o + .|
| . . O +.o... o|
| . = S o.+. + |
| o + ..= .|
|o .X .o|
| . ..oo*o.|
|E +B=o..|
+----[SHA256]-----+
将/root/.ssh/id_rsa.pub文件内容复制到远程仓库ssh key认证处,使用码云如下;
3)新建远程仓库,克隆ssh地址;
4)在本地执行 "git remote add origin <远程仓库地址>" 命令,将本地版本仓库和远程仓库进行关联;
5)再执行"git push -u origin master"命令,将master分支推送到远程仓库上;第一次推送使用 -u 选项,后面再推送时,可省略,如果push出错,可先执行"git pull origin master"命令,拉取后再push;
[root@syztoo gittest]# git remote add origin git@gitee.com:syztoo/gittest.git
[root@syztoo gittest]# git push -u origin master
To git@gitee.com:syztoo/gittest.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@gitee.com:syztoo/gittest.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
[root@syztoo gittest]# git pull origin master
warning: no common commits
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.
From gitee.com:syztoo/gittest
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.en.md | 36 ++++++++++++++++++++++++++++++++++++
README.md | 39 +++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+)
create mode 100644 README.en.md
create mode 100644 README.md
[root@syztoo gittest]# ls
README.en.md README.md test.txt
[root@syztoo gittest]# git push -u origin master
Counting objects: 10, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 860 bytes | 0 bytes/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: Powered By Gitee.com
To git@gitee.com:syztoo/gittest.git
56fc42c..c94c856 master -> master
Branch master set up to track remote branch master from origin.
11.从远程仓库克隆
使用"git clone <仓库地址>" 直接将远程仓库拉取到本地;
[root@syztoo local]# git clone git@gitee.com:syztoo/gittest.git
Cloning into 'gittest'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 13 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (2/2), done.
[root@syztoo local]# ls
aegis bin etc games gittest include java lib lib64 libexec nginx sbin share src
[root@syztoo local]# cd gittest/
[root@syztoo gittest]# ls -a
. .. .git README.en.md README.md test.txt