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
​
相关推荐
GlobalInfo27 分钟前
2026年全球机器人用超平执行器市场研究报告:市场规模达7.51亿美元,人形机器人商业化与轻量化运动控制需求推动行业增长
大数据·人工智能·数码相机·microsoft·机器人
IT古董2 小时前
《Agent开发工程师成长指南》- 第4章 第2节:Prompt设计五要素——任务、角色、上下文、约束与输出格式
大数据·人工智能·prompt
Allen_LVyingbo4 小时前
NVIDIA CUDA Tile 在医疗计算中的算子形式化、算法重构与内核实现
大数据·python·算法·重构·知识图谱·健康医疗·数据库架构
全栈陈序员5 小时前
【Git】从入门到精通(一)—— Git简介:什么是版本控制系统
大数据·git·elasticsearch
中国搜索直付通5 小时前
分账系统有风险吗?资金安全怎么保障?
大数据·支付通道·分账系统·分账支付
阿里云大数据AI技术5 小时前
云栖实录|阿里云AI搜索系列产品全新发布 - 新引擎
人工智能·elasticsearch
一枕槐安4875 小时前
微调 vs Prompt:业务场景下两种方案成本 & 效果对比
大数据·人工智能·prompt·混合架构·推理成本·行为固化·知识边界
SelectDB6 小时前
Apache Doris 与 SelectDB 是什么关系?从开源技术到面向 Agent 的极速分析数据库
大数据·数据库·数据分析
科技新资讯6 小时前
AI如何批量处理Excel文件? 从数据合并、清洗到自动生成报表,看看 AI Agent 怎么完成任务
大数据·人工智能·excel
SelectDB6 小时前
存算分离冷查询提速 7.5 倍!Apache Doris 4.2 Peer Read 实现跨计算组缓存复用
大数据·数据库·数据分析