git的安装与使用

单机使用git
1.安装git

[root@localhost ~]# yum install git -y

2.配置基本信息(用户名和邮箱地址)

[root@localhost ~]# git config --global user.name "lsk"
[root@localhost ~]# git config --global user.email "example@qq.com"

3.创建仓库

[root@localhost ~]# mkdir /data/gitroot

4.初始化仓库

[root@localhost ~]# cd /data/gitroot
[root@localhost gitroot]# git init
Initialized empty Git repository in /data/gitroot/.git/

有.git的目录
[root@localhost gitroot]# ll -a
total 4
drwxr-xr-x. 3 root root 17 Mar 2 08:56 .
drwxr-xr-x. 4 root root 34 Mar 2 08:55 …
drwxr-xr-x. 7 root root 4096 Mar 2 08:56 .git
创建一个新文件

[root@localhost gitroot]# echo "hello world" > 1.tx

把1.txt添加到仓库

[root@localhost gitroot]# git add 1.txt

提交add完了必须要commit才算真正把文件提交到git仓库里

[root@localhost gitroot]# git commit -m"add new file 1.txt"
[master (root-commit) 72c1e22] add new file 1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
 [root@localhost gitroot]# cat 1.txt
hello world

修改1.txt文件

[root@localhost gitroot]# echo "new" >>1.txt
[root@localhost gitroot]# cat 1.txt
hello world
new

查看git的状态

[root@localhost gitroot]# 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:   1.txt          (改变)
#
no changes added to commit (use "git add" and/or "git commit -a")

查看详情信息

[root@localhost gitroot]# git diff 1.txt
diff --git a/1.txt b/1.txt
index 3b18e51..3a4b9d0 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 hello world
+new

再次修改1.txt文件

[root@localhost gitroot]# git add 1.txt
[root@localhost gitroot]# git commit -m"1.txt change 1"
[master 5304ea2] 1.txt change 1
 1 file changed, 2 insertions(+)

查看所有历史记录

 [root@localhost gitroot]# git log
commit 5304ea230e3da1b9b4bcba2c67d41ead
Author: lsk <example@qq.com>
Date:   Tue Mar 2 09:22:30 2021 -0500

    1.txt change 1

commit 72c1e226e7af8637108d85b4c1a0b710
Author: lsk <example@qq.com>
Date:   Tue Mar 2 09:01:39 2021 -0500

    add new file 1.txt

一行显示

[root@localhost gitroot]# git log --pretty=oneline
5304ea230e3da1b9b4bcba2c67d41ead52ea17d
72c1e226e7af8637108d85b4c1a0b710a9f7647
(END)

回退版本

[root@localhost gitroot]# git reset --hard 72c1e226e7aHEAD is now at 72c1e22 add new file 1.txt
[root@localhost gitroot]# cat 1.txt
hello world

撤销修改
如果把1.txt删掉后想恢复

[root@localhost gitroot]# git checkout -- 1.txt
[root@localhost gitroot]# ls
1.txt
此时查看git log只有当前版本
[root@localhost gitroot]# git log
commit 72c1e226e7af8637108d85b4c1a0b710
Author: lsk <example@qq.com>
Date:   Tue Mar 2 09:01:39 2021 -0500


    add new file 1.txt


查看所有历史版本


[root@localhost gitroot]# git reflog
72c1e22 HEAD@{0}: reset: moving to 72c1
5304ea2 HEAD@{1}: commit: 1.txt change 
72c1e22 HEAD@{2}: commit (initial): add
(END)

回到指定版本

[root@localhost gitroot]# git reset --hard 5304ea2
HEAD is now at 5304ea2 1.txt change 1

删除文件

[root@localhost gitroot]# echo -e "11111111111\n2222222222" > 2.txt
[root@localhost gitroot]# ls
1.txt  2.txt
[root@localhost gitroot]# cat 2.txt
11111111111
2222222222
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m"new file 2.txt"
[master cb73f74] new file 2.txt
 1 file changed, 2 insertions(+)
 create mode 100644 2.txt
[root@localhost gitroot]# git rm 2.txt
rm '2.txt'
[root@localhost gitroot]# ls 
1.txt
[root@localhost gitroot]# git commit -m"rm 2.txt"
[master b527317] rm 2.txt
 1 file changed, 2 deletions(-)
 delete mode 100644 2.txt
[root@localhost gitroot]# 
[root@localhost gitroot]# git log --pretty=oneline
b527317dece93a16e10e4d761a2b3e42212811d
cb73f742dcf1ddcf979a6e955f1246c069ad58e
5304ea230e3da1b9b4bcba2c67d41ead52ea17d
72c1e226e7af8637108d85b4c1a
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值