1.安装git
[root@code-node ~]# dnf install git -y
#设定命令补全功能
[root@code-node ~]# echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
[root@code-node ~]# source ~/.bashrc
2.版本库初始化
[root@code-node ~]# mkdir timinglee
[root@code-node ~]# cd timinglee/
[root@code-node timinglee]# git init
提示: 使用 'master' 作为初始分支的名称。这个默认分支名称在 Git 3.0 中将会修改为
提示: "main"。要为您的所有新仓库配置初始分支名称(这将禁用此警告),请运行:
提示:
提示: git config --global init.defaultBranch <名称>
提示:
提示: 除了 'master' 之外,通常选定的名字有 'main'、'trunk' 和 'development'。
提示: 可以通过以下命令重命名新创建的分支:
提示:
提示: git branch -m <name>
提示:
提示: Disable this message with "git config set advice.defaultBranchName false"
已初始化空的 Git 仓库于 /root/timinglee/.git/
[root@code-node timinglee]# ls -a
. .. .git
[root@code-node timinglee]# git config --global user.name "timinglee"
[root@code-node timinglee]# git config --global user.email "timinglee@timinglee.org"
二.git常用操作
1.状态查看
[root@code-node timinglee]# git status
位于分支 master
尚无提交
无文件要提交(创建/拷贝文件并使用 "git add" 建立跟踪)
2.增加文件
[root@code-node timinglee]# echo hello timinglee > timinglee
[root@code-node timinglee]# git status -s
?? timinglee #版本库中没有此版本
3.文件未被最终时提交
#提交到缓冲区
[root@code-node timinglee]# git add timinglee
[root@code-node timinglee]# git status -s
A timinglee #此版本已经在stage中,但是没有提交到版本库
#提交到版本库
[root@code-node timinglee]# git commit -m "timinglee v1"
[master(根提交) 20af68c] timinglee v1
1 file changed, 1 insertion(+)
create mode 100644 timinglee
4.版本库中已经最终改文件时的操作
#版本库中已经记录文件后更改文件
[root@code-node timinglee]# echo 123 >> timinglee
[root@code-node timinglee]# git status -s
M timinglee #版本库中的文件和本地文件有差异,本地文件修改带未提交
[root@code-node timinglee]# git add timinglee
[root@code-node timinglee]# git status -s
M timinglee #本地更改提交到暂存区
#从暂存区中撤回
[root@code-node timinglee]# git restore --staged timinglee
[root@code-node timinglee]# git status -s
M timinglee
#撤回本地更改
[root@code-node timinglee]# git status -s
M timinglee
[root@code-node timinglee]# git checkout -- timinglee
[root@code-node timinglee]# cat timinglee
hello timinglee
#更新提交一次完成
[root@code-node timinglee]# git commit -a -m "timinglee v2"
[master 782e755] timinglee v2
1 file changed, 1 insertion(+)
5.多次更改文件后处理方式
[root@code-node timinglee]# echo 123 >> timinglee
[root@code-node timinglee]# git add timinglee
[root@code-node timinglee]# echo 123 >> timinglee
[root@code-node timinglee]# git status -s
MM timinglee #本地更改和暂存区更改都存在
[root@code-node timinglee]# git commit -m "timinglee v3"
[master 953521f] timinglee v3
1 file changed, 1 insertion(+)
[root@code-node timinglee]# git status -s
M timinglee
[root@code-node timinglee]# git add timinglee
[root@code-node timinglee]# git commit -m "timinglee v4"
[master 4040c80] timinglee v4
1 file changed, 1 insertion(+)
#对比差异
[root@code-node timinglee]# git diff
[root@code-node timinglee]# echo hello lee >> timinglee
[root@code-node timinglee]# git diff
diff --git a/timinglee b/timinglee
index d2dadb2..30c93a5 100644
--- a/timinglee
+++ b/timinglee
@@ -2,3 +2,4 @@ hello timinglee
123
123
123
+hello lee
6.撤销工作区动作
#撤销未提交内容
[root@code-node timinglee]# echo hello lee >> timinglee
[root@code-node timinglee]# git diff
diff --git a/timinglee b/timinglee
index d2dadb2..30c93a5 100644
--- a/timinglee
+++ b/timinglee
@@ -2,3 +2,4 @@ hello timinglee
123
123
123
+hello lee
[root@code-node timinglee]# ls
lee timinglee
[root@code-node timinglee]# vim timinglee
[root@code-node timinglee]# git checkout -- timinglee
[root@code-node timinglee]# cat timinglee
hello timinglee
123
123
123
#从缓存区撤回
[root@code-node timinglee]# echo hello lee >> timinglee
[root@code-node timinglee]# git add timinglee
[root@code-node timinglee]# git add timinglee ^C
[root@code-node timinglee]# git status -s
M timinglee
[root@code-node timinglee]# git restore --staged timinglee
[root@code-node timinglee]# git status -s
M timinglee
[root@code-node timinglee]# git checkout -- timinglee
[root@code-node timinglee]# git status -s
7.从版本库中删除项目
#如果本地删除是可以撤回的
[root@code-node timinglee]# git checkout -- timinglee
[root@code-node timinglee]# ls
timinglee
#删除版本库
[root@code-node timinglee]# git rm timinglee
rm 'timinglee'
[root@code-node timinglee]# ls
[root@code-node timinglee]# git rm lee
rm 'lee'
[root@code-node timinglee]# git status -s
D lee
[root@code-node timinglee]# git commit -a -m "delete timinglee"
[master baecea8] delete timinglee
1 file changed, 4 deletions(-)
delete mode 100644 timinglee
8.恢复删掉的项目
[root@code-node timinglee]# git reflog
baecea8 (HEAD -> master) HEAD@{0}: commit: delete timinglee
7450f0b HEAD@{1}: commit: delete lee
4040c80 HEAD@{2}: commit: timinglee v4
953521f HEAD@{3}: commit: timinglee v3
782e755 HEAD@{4}: commit: timinglee v2
a2bad8f HEAD@{5}: commit: lee v1
20af68c HEAD@{6}: checkout: moving from master to master
20af68c HEAD@{7}: commit (initial): timinglee v1
[root@code-node timinglee]# git reset --hard 4040c80
HEAD 现在位于 4040c80 timinglee v4
[root@code-node timinglee]# ls
lee timinglee
9.忽略提交
[root@code-node timinglee]# vim .file
[root@code-node timinglee]# git status -s
?? .file
[root@code-node timinglee]# vim .gitignore
[root@code-node timinglee]# cat .gitignore
.*
[root@code-node timinglee]# git status -s