Git基本操作

文章目录

    • [1. Git安装](#1. Git安装)
    • [2. 创建本地仓库](#2. 创建本地仓库)
    • [3. git配置](#3. git配置)
    • [4. 工作区、暂存区、版本库](#4. 工作区、暂存区、版本库)
      • [4.1 git add](#4.1 git add)
      • [4.2 git commit](#4.2 git commit)
      • [4.3 git cat-file](#4.3 git cat-file)
    • [5. 版本回退](#5. 版本回退)
    • [6. 撤销修改](#6. 撤销修改)
    • [7 .删除版本库文件](#7 .删除版本库文件)

1. Git安装

查看是否安装过:

shell 复制代码
git --version

如果是-bash: git: command not found,就表明没安装

安装git:

bash 复制代码
sodu yum install git -y

2. 创建本地仓库

bash 复制代码
git init

3. git配置

配置用户名和邮箱地址:

bash 复制代码
git config [--global] user.name "用户名"
git config [--global] user.email "邮箱"

如果加上--global表明在当前机器所有仓库都生效

查看配置:

bash 复制代码
git congifg -l

删除配置:

bash 复制代码
git config [--global] --unset user.name	#删除用户名
git config [--global] --unset user.email	#删除邮箱地址

4. 工作区、暂存区、版本库

在本地仓库创建文件之后,git是不能够直接管理这个文件的

工作区、版本库中的暂存区和版本库之间的关系图片来源(侵权删):Git 工作区、暂存区和版本库)

不能够直接在.git文件里面手动进行修改

4.1 git add

  • git add [file1] [flie2] [-A]:添加指定文件,包括删除的文件
  • git add [dir]:添加指定目录
  • git add .:添加当前目录所有改动文件

4.2 git commit

  • git commit -m "message":提交暂存区全部内容到本地仓库(.git)
  • git commit -m [file1] [file2] -m "message":提交暂存区指定文件到本地仓库

可采用git log [--pretty=oneline] 查看提交记录

4.3 git cat-file

bash 复制代码
git cat-file -p commit_id	#查看版本库对象内容

git追踪管理的是修改,而不是文件

bash 复制代码
git diff file_name
  • a/ReadMe:改动前的文件
  • b/ReadMe:改动后的文件
  • -1:-表明改动前,1表明第一行内容
  • +1,2:+改动后的内容,1,2连续2行的内容

5. 版本回退

bash 复制代码
git reset [--soft | --mixed | --hard] [HEAD]
  • soft:只回退版本库内容
  • mixed(默认项):为默认选项,使⽤时可以不⽤带该参数。该参数将暂存区的内容退回为指定提交版本内容,⼯作区文件保持不变。
  • hard(慎用):回退所有区内容
  • HEAD:回退到当前版本,与版本库的版本一致(HEAD^回退到上一个版本,HEAD^^回退到前2个版本...)

查看log获取commit id,然后选择要回退的版本,如果选择了--hard之后后悔了,可采用:

bash 复制代码
git reflog	#记录本地每一次提交命令

回退为什么快:

指向修改master指向的对象(commit id)即可

6. 撤销修改

工作区 暂存区 版本库 解决方法
add code 1.手动撤销(容易出错、费时间) 2.git checkout -- [file_name]
add code add code git reset HEAD
add code add code add code 前提commit之后没有push git reset --hard HEAD^

7 .删除版本库文件

bash 复制代码
git rm file_name

普通的删除:

bash 复制代码
rm file_name
git add file_name -A
git commit -m "message"
相关推荐
Apipi*4 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
天启HTTP5 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
广州浮点FLOATLIC7 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
BLUcoding8 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
AIgorithmGEEK8 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil2088 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)8 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter6258 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器
xh didida10 小时前
Linux -- 基础IO
linux·服务器·开发语言·c++
zly350010 小时前
centos7 mount设备挂载 /dev/sda1/硬盘挂载
linux·运维·服务器