首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【Git:基本操作】深度解析Git:从初始Git到熟悉基本操作

【Git:基本操作】深度解析Git:从初始Git到熟悉基本操作

作者头像
艾莉丝努力练剑
发布2025-11-13 10:58:33
发布2025-11-13 10:58:33
860
举报
文章被收录于专栏:C / C++C / C++

🔥艾莉丝努力练剑:个人主页

专栏传送门:《C语言》《数据结构与算法》C/C++干货分享&学习过程记录Linux操作系统编程详解笔试/面试常见算法:从基础到进阶测试开发要点全知道

⭐️为天地立心,为生民立命,为往圣继绝学,为万世开太平


🎬艾莉丝的简介:

艾莉丝的Gitee地址

艾莉丝努力练剑的gitee

1 ~> 初始Git


2 ~> CentOs版本安装git

2.1 指令

查看安装版本:git --verion 下载:(sudo) yum install git -y 卸载:(sudo) yum remove git -y

2.2 最佳实践

代码语言:javascript
复制
// 查看安装版本
git --verion

// 下载
(sudo) yum install git -y

// 卸载
(sudo) yum remove git -y 

这里博主因为已经安装过git了,所以就只演示一下【查看安装版本】的操作——


3 ~> ubuntu版本安装git

如果你的的平台是ubuntu,安装git相当简单,以ubuntu20.04为例——

代码语言:javascript
复制
$ git
Command 'git' not found, but can be installed with:

sudo apt install git

3.1 安装git

代码语言:javascript
复制
$sudo apt-get install git -y

3.2 查看git安装的版本

代码语言:javascript
复制
$ git --version

4 ~> 基本操作:创建本地仓库

4.1 创建

代码语言:javascript
复制
mkdir gitcode

4.2 在当前目录下创建git本地仓库

代码语言:javascript
复制
git init

4.3 查看目录内容

代码语言:javascript
复制
tree .git/

运行如下——

代码语言:javascript
复制
[root@VM-4-17-centos dir]# mkdir gitcode
[root@VM-4-17-centos dir]# ls
gitcode  test.c
[root@VM-4-17-centos dir]# git init
Initialized empty Git repository in /home/jqj/dir/.git/
[root@VM-4-17-centos dir]# ls
gitcode  test.c
[root@VM-4-17-centos dir]# tree .git/
.git/
|-- branches
|-- config
|-- description
|-- HEAD
|-- hooks
|   |-- applypatch-msg.sample
|   |-- commit-msg.sample
|   |-- post-update.sample
|   |-- pre-applypatch.sample
|   |-- pre-commit.sample
|   |-- prepare-commit-msg.sample
|   |-- pre-push.sample
|   |-- pre-rebase.sample
|   `-- update.sample
|-- info
|   `-- exclude
|-- objects
|   |-- info
|   `-- pack
`-- refs
    |-- heads
    `-- tags

9 directories, 13 files

5 ~> 基本操作:配置本地仓库

5.1 指令

代码语言:javascript
复制
git config user.name "[名字]"
git config --unset user.name

git config user.email "[email]"
git config --unset user.email

git config --global user.name "[名字]"
git config --global --unset user.name

git config --global user.email "[email]"
git config --global --unset user.email

5.2 查看

代码语言:javascript
复制
git config -l

5.3 最佳实践1:局部环境配置

5.3.1 名字
代码语言:javascript
复制
[root@VM-4-17-centos dir]# git config user.name "jqj"
[root@VM-4-17-centos dir]# git config -l
user.name=jqj
user.email=18106882575@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=jqj
5.3.2 邮箱
代码语言:javascript
复制
[root@VM-4-17-centos dir]# git config user.name "181xxxxxxxx@163.com"
[root@VM-4-17-centos dir]# git config -l 
user.name=jqj
user.email=181xxxxxxxx@163.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
user.name=181xxxxxxxx@163.com

5.4 最佳实践2:全局环境配置

代码语言:javascript
复制
[root@VM-4-17-centos dir]# git config --global user.name "[名字]"
[root@VM-4-17-centos dir]# git config --global user.email "[email]"
[root@VM-4-17-centos dir]# git config -l
user.name=[名字]
user.email=[email]
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

5.5 最佳实践3:删除

5.6 --global选项

一台服务器上可以创建多个本地仓库,加了--global选项,目的是在当前机器的所有git仓库生效。


6 ~> 基本操作:认识工作区、暂存区、版本区


7 ~> 基本操作:添加文件

7.1 场景1

代码语言:javascript
复制
[jqj@VM-4-17-centos gitcode]$ touch file1 file2 file3
[jqj@VM-4-17-centos gitcode]$ ls
file1  file2  file3
[jqj@VM-4-17-centos gitcode]$ git add file "add 3 file"
fatal: pathspec 'file' did not match any files
[jqj@VM-4-17-centos gitcode]$ git add file1 file2 file3
[jqj@VM-4-17-centos gitcode]$ git commit -m "add 3 file"

7.2 场景2


8 ~> 基本操作:查看.git文件

8.1 指令

代码语言:javascript
复制
cat .git/HFAD
ref refs/heads/master
cat .git/refs/heads/master // 对象
git cat -file -p [commit ID]

查看对象内容:cat -file。

8.2 图示

8.3 手记

8.4 学习思路

刚开始学习git的时候,进行一些操作,都要与.git目录中的结构变化对应着来看,有利于了解git目录细节流程。


9 ~> 基本操作:修改文件


10 ~> 基本操作:版本回退


11 ~> 基本操作:撤销修改——情况一 / 二 / 三

11.1 情况一

11.2 情况二

11.3 情况三

11.4 验证


12 ~> 基本操作:删除文件

以上就是文件删除的两种流程git rm [文件名]更快捷


Git:基本操作部分博主手记


结尾

创作过程中难免有所缺漏,如果uu们发现艾莉丝的文章哪里有所疏忽,望不吝赐教!感谢支持!

往期回顾:

本文为本专栏的开篇之作,因此暂时还没有【往期回顾】环节,后期会加入的。

结语:都看到这里啦!那请大佬不要忘记给博主来个“一键四连”哦!

🗡博主在这里放了一只小狗,大家看完了摸摸小狗放松一下吧!🗡 ૮₍ ˶ ˊ ᴥ ˋ˶₎ა

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-11-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 艾莉丝的Gitee地址
  • 1 ~> 初始Git
  • 2 ~> CentOs版本安装git
    • 2.1 指令
    • 2.2 最佳实践
  • 3 ~> ubuntu版本安装git
    • 3.1 安装git
    • 3.2 查看git安装的版本
  • 4 ~> 基本操作:创建本地仓库
    • 4.1 创建
    • 4.2 在当前目录下创建git本地仓库
    • 4.3 查看目录内容
  • 5 ~> 基本操作:配置本地仓库
    • 5.1 指令
    • 5.2 查看
    • 5.3 最佳实践1:局部环境配置
      • 5.3.1 名字
      • 5.3.2 邮箱
    • 5.4 最佳实践2:全局环境配置
    • 5.5 最佳实践3:删除
    • 5.6 --global选项
  • 6 ~> 基本操作:认识工作区、暂存区、版本区
  • 7 ~> 基本操作:添加文件
    • 7.1 场景1
    • 7.2 场景2
  • 8 ~> 基本操作:查看.git文件
    • 8.1 指令
    • 8.2 图示
    • 8.3 手记
    • 8.4 学习思路
  • 9 ~> 基本操作:修改文件
  • 10 ~> 基本操作:版本回退
  • 11 ~> 基本操作:撤销修改——情况一 / 二 / 三
    • 11.1 情况一
    • 11.2 情况二
    • 11.3 情况三
    • 11.4 验证
  • 12 ~> 基本操作:删除文件
  • Git:基本操作部分博主手记
  • 结尾
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档