EmmmuaCode EmmmuaCode
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)

wufan

海内存知己,天涯若比邻。
首页​
导航🚀​
  • 数据结构
  • 计算机网络
  • Java基础

    • JavaSE
    • JVM虚拟机
    • JUC并发编程
  • JavaWeb

    • Servlet
    • MVC
    • filter|listener
  • HTML
  • CSS
  • JavaScript
  • Vue
  • uni-app
  • Spring5
  • SpringMVC
  • SpringBoot2
  • SpringCloud
  • SpringSecurity
  • 搜索引擎

    • ElasticSearch
  • 消息队列

    • RabbitMQ
  • 服务器

    • Nginx🌐
  • 服务框架

    • Dubbo
  • Python基础
  • 数据分析
  • Hadoop
  • SQL 数据库

    • MySQL
  • NoSQL 数据库

    • NoSQL数据库概论
    • Redis
    • MongoDB
    • HBase
  • 框架

    • MyBatis
    • MyBatis-Plus
    • ShardingSphere
  • 部署

    • Linux
    • Docker
  • 管理

    • Maven
    • Git
  • 友情链接
  • 优秀博客文章
  • 索引

    • 分类
    • 标签
    • 归档
  • 其他

    • 关于
Github (opens new window)
  • Linux

    • Linux基础篇
    • Linux 目录结构
    • Linux 远程登录到Linux服务器
    • Linux Vi和Vim编辑器
    • Linux 虚拟机关机、重启和用户登录注销
    • Linux 用户管理
    • Linux 实用指令
    • Linux 组管理和权限管理
    • Linux 定时任务调度
    • Linux 磁盘分区、挂载
    • Linux 网络配置
    • Linux 进程管理
    • Linux RPM与YUM
    • Linux 搭建JavaEE环境
    • Linux Shell编程
    • Linux Python开发平台Ubuntu
    • Python定制篇-APT软件管理和远程登录
    • Linux 日志管理
    • Linux 定制自己的Linux
    • Linux 源码介绍&内核升级
    • Linux 备份与恢复
    • Linux 可视化管理-webmin和bt运维工具
  • Docker

    • Docker 简介
    • Docker 安装
    • Docker 常用命令
    • Docker 镜像原理
    • 本地镜像发布到阿里云
    • 将本地镜像推送到私有库
    • Docker 数据卷
    • Docker 软件安装
  • Maven

    • Maven 简介
    • Maven 基础
    • Maven 高级
  • Git 基础

    • Git 概述
    • Git 安装
    • Git 常用命令
      • Git 常用命令
      • 设置用户签名
      • 初始化本地库
      • 查看本地库状态
        • 首次查看(工作区没有任何文件)
        • 新增文件(hello.txt)
        • 再次查看(检测到未追踪的文件)
      • 添加暂存区
        • 将工作区的文件添加到暂存区
        • 查看状态(检测到暂存区有新文件)
      • 提交到本地库
        • 将暂存区的文件提交到本地库
        • 查看状态(没有文件需要提交)
      • 修改文件(hello.txt)
        • 查看状态(检测到工作区有文件被修改)
        • 将修改的文件再次添加暂存区
        • 将暂存区的文件再次提交到本地库
        • 查看状态
      • 历史版本
        • 查看历史版本
        • 版本穿梭
    • Git 分支操作
    • Git 团队协作机制
    • IDEA 集成Git
    • IDEA 集成 GitHub
    • 自建代码托管平台-GitLab
  • studynotes
  • project-management
  • Git
wufan
2022-01-26
目录

Git 常用命令

# Git 常用命令

命令名称 作用
git config --global user.name 用户名 设置用户签名
git config --global user.email 设置用户签名
git init 初始化本地库
git status 查看本地库状态
git add 文件名 添加到缓存区
git commit -m"日志信息" 文件名 提交到本地库
git reflog 查看历史记录
reset --hard 版本号 版本穿梭

# 设置用户签名

  1. 基本语法

git config --global user.name 用户名

git config --global user.email 邮箱

  1. 案例实操

全局范围的签名设置:

DELL@FRXcomputer MINGW64 /d/git-Space
$ git config --global user.name xustudyxu

DELL@FRXcomputer MINGW64 /d/git-Space
$ git config --global user.eamil 1812903531@qq.com

DELL@FRXcomputer MINGW64 /d/git-Space
$ cat ~/.gitconfig
[user]
        email = 1812903531@qq.com
        name = xustudyxu
1
2
3
4
5
6
7
8
9
10
11

提示

签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码。

这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任何关系。

# 初始化本地库

  1. 基本语法

git init

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space
$ git init
Initialized empty Git repository in D:/git-Space/.git/

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ ll -a
total 20
drwxr-xr-x 1 DELL 197121 0  1月 26 16:13 ./
drwxr-xr-x 1 DELL 197121 0  1月 26 16:08 ../
drwxr-xr-x 1 DELL 197121 0  1月 26 16:13 .git/
1
2
3
4
5
6
7
8
9
10
  1. 结果查看

01

# 查看本地库状态

  1. 基本语法

git status

  1. 案例实操

# 首次查看(工作区没有任何文件)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
1
2
3
4
5
6
7

# 新增文件(hello.txt)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ vim hello.txt

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ cat hello.txt
hello git!
1
2
3
4
5
6

# 再次查看(检测到未追踪的文件)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)
1
2
3
4
5
6
7
8
9
10
11

# 添加暂存区

# 将工作区的文件添加到暂存区

  1. 基本语法

git add 文件名

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
1
2
3
4

# 查看状态(检测到暂存区有新文件)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt
1
2
3
4
5
6
7
8
9

# 提交到本地库

# 将暂存区的文件提交到本地库

  1. 基本语法

git commit -m "日志信息" 文件名

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git commit -m"my first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master (root-commit) dca1cdc] my first commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
1
2
3
4
5
6
7

# 查看状态(没有文件需要提交)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git status
On branch master
nothing to commit, working tree clean
1
2
3
4

# 修改文件(hello.txt)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ vim hello.txt

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ cat hello.txt
hello git! hi
1
2
3
4
5
6

# 查看状态(检测到工作区有文件被修改)

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
1
2
3
4
5
6
7
8
9

# 将修改的文件再次添加暂存区

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
1
2
3
4

# 将暂存区的文件再次提交到本地库

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git commit -m"my second commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master 0765edd] my second commit
 1 file changed, 1 insertion(+), 1 deletion(-)
1
2
3
4
5
6

# 查看状态

DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git status
On branch master
nothing to commit, working tree clean
1
2
3
4

# 历史版本

# 查看历史版本

  1. 基本语法

git reflog 查看版本信息

git log 查看版本详细信息

  1. 案例实操
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git reflog
0765edd (HEAD -> master) HEAD@{0}: commit: my second commit
dca1cdc HEAD@{1}: commit (initial): my first commit
1
2
3
4
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git log
commit 0765eddb29d5ef8184ecb161d339398bb2ec8dff (HEAD -> master)
Author: xustudyxu <1812903531@qq.com>
Date:   Wed Jan 26 18:54:55 2022 +0800

    my second commit

commit dca1cdc5f1360eb7fff3d0269368664a49f21898
Author: xustudyxu <1812903531@qq.com>
Date:   Wed Jan 26 17:54:37 2022 +0800

    my first commit
1
2
3
4
5
6
7
8
9
10
11
12
13

# 版本穿梭

  1. 基本语法

git reset --hard 版本号

  1. 案例实操
  • 查看hello.txt内容
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ cat hello.txt
hello git! hi
1
2
3
  • 首先查看当前的历史记录,可以看到当前是在 0765edd 这个版本
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git reflog
0765edd (HEAD -> master) HEAD@{0}: commit: my second commit
dca1cdc HEAD@{1}: commit (initial): my first commit
1
2
3
4
  • 切换到 dca1cdc版本,也就是我们第一次提交的版本
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git reset --hard dca1cdc
HEAD is now at dca1cdc my first commit
1
2
3
  • 切换完毕之后再查看历史记录,当前成功切换到了dca1cdc版本
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ git reflog
dca1cdc (HEAD -> master) HEAD@{0}: reset: moving to dca1cdc
0765edd HEAD@{1}: commit: my second commit
dca1cdc (HEAD -> master) HEAD@{2}: commit (initial): my first commit
1
2
3
4
5
  • 然后查看文件 hello.txt,发现文件内容跟第一次提交完全一致!
DELL@FRXcomputer MINGW64 /d/git-Space (master)
$ cat hello.txt
hello git!
1
2
3

Git 切换版本,底层其实是移动的 HEAD 指针,具体原理如下图所示。

02

#Git
上次更新: 2024/04/21, 09:42:22
Git 安装
Git 分支操作

← Git 安装 Git 分支操作→

最近更新
01
微信支付功能的实现与流程
11-21
02
购物车与结算区域的深入优化与功能完善
11-21
03
购物车与结算区域的功能实现与优化
11-21
更多文章>
Theme by Vdoing | Copyright © 2023-2024 EmmmuaCode | 黔ICP备2022009864号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式