git使用手册

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
​
相关推荐
乌鲁木齐德润恒圣信息科技有限公司9 小时前
GEO生成式引擎优化:技术原理与43引擎自动测试系统
大数据·前端·人工智能
数字孪生视频孪生14 小时前
场景自生长:无需人工建模的空间数字化路径
大数据·人工智能·重构·架构·空间计算
Lansonli15 小时前
大数据Spark(九十三):Spark HistoryServer
大数据·分布式·spark
Rain的Java大神之路15 小时前
大表 JOIN 不必靠堆内存:Dojao 的跨源 SQL、减量溢写与 Python UDF 实战
java·大数据·后端
Elasticsearch16 小时前
一文看懂 Elasticsearch BBQ 向量压缩如何降本增效
elasticsearch
乌鲁木齐德润恒圣信息科技有限公司18 小时前
智推纪元 子站架构技术拆解
大数据·人工智能·爬虫
大大大大晴天1 天前
每天认识一个组件:VictoriaLogs
大数据
学长毕设1 天前
基于SpringBoot 博物馆文物租赁及修复管理系统-附源码
大数据·数据库·spring boot
玖米GEO运营王总监2 天前
成都AI数字人视频怎么批量生产?流程与合规标注
大数据·人工智能·音视频
龙腾AI白云2 天前
数字孪生与计算机视觉:让机器看懂现场
大数据·数据库·机器学习·知识图谱·pygame