【前端】Git 常用

文章目录

  • [一、git 配置](#一、git 配置)
    • [1.1 查看系统配置](#1.1 查看系统配置)
    • [1.2 查看当前用户配置](#1.2 查看当前用户配置)
    • [1.3 查看当前git用户名](#1.3 查看当前git用户名)
    • [1.4 查看当前git邮箱](#1.4 查看当前git邮箱)
    • [1.5 查看当前仓库配置信息](#1.5 查看当前仓库配置信息)
    • [1.6 修改git用户名](#1.6 修改git用户名)
    • [1.7 修改git邮箱](#1.7 修改git邮箱)
  • [二、git 常用命令](#二、git 常用命令)
    • [2.1 git init](#2.1 git init)
    • [2.2 git clone](#2.2 git clone)
    • [2.3 git status](#2.3 git status)
    • [2.4 git add](#2.4 git add)
    • [2.5 git commit](#2.5 git commit)
    • [2.5 git log](#2.5 git log)
    • [2.6 git pull](#2.6 git pull)
    • [2.7 git push](#2.7 git push)
    • [2.8 git fetch](#2.8 git fetch)
  • 三、分支
    • [3.1 git branch](#3.1 git branch)
    • [3.2 git checkout](#3.2 git checkout)
    • [3.3 git merge](#3.3 git merge)
  • 四、回滚
    • [4.1 仓库+暂存区回滚](#4.1 仓库+暂存区回滚)
    • [4.2 暂存区回滚](#4.2 暂存区回滚)
    • [4.3 仓库回滚](#4.3 仓库回滚)
    • [4.4 本地+仓库+暂存区回滚](#4.4 本地+仓库+暂存区回滚)
    • [4.5 远程仓库回滚](#4.5 远程仓库回滚)

一、git 配置

查看当前git环境详细配置

复制代码
git config -l

1.1 查看系统配置

复制代码
git config --system --list

配置文件在git安装目录/etc/gitconfig

1.2 查看当前用户配置

复制代码
git config --global --list

1.3 查看当前git用户名

复制代码
git config user.name

1.4 查看当前git邮箱

复制代码
git config user.email

1.5 查看当前仓库配置信息

复制代码
git config --local --list

配置文件在:当前项目的/.git/config

1.6 修改git用户名

复制代码
git config --global user.name "用户名"

1.7 修改git邮箱

复制代码
git config --global user.email "邮箱"

二、git 常用命令

2.1 git init

当前目录新建一个仓库

复制代码
 git init

2.2 git clone

克隆一个远程仓库

复制代码
git clone [url]

2.3 git status

查看所有文件状态

复制代码
git status

查看指定文件状态

复制代码
git status [file-name]

2.4 git add

添加当前目录下的所有文件到暂存区:

复制代码
git add .

将工作区被修改的文件和被删除的文件提交到暂存区,不包括新增的文件

u指update

复制代码
git add -u .

将工作区被修改,被删除,新增的文件都提交到暂存区

-A指all

复制代码
git add -A .

从工作区添加指定文件到暂存区

复制代码
git add [file1] [file2] ...

2.5 git commit

提交暂存区到仓库区

复制代码
git commit -m [message]

提交暂存区的指定文件到仓库区

复制代码
git commit [file1] [file2] -m [message]

示例:git commit src/commit.js -m "提交commit.js修改"

2.5 git log

显示所有commit日志

复制代码
git log

查看分支合并情况

复制代码
git log --graph --pretty=oneline --abbrev-commit

查看分叉历史

复制代码
git log --oneline --decorate --graph --all

查看最新3条commit日志数据

复制代码
git log -3

2.6 git pull

从远程仓库拉取代码到工作区

复制代码
git pull

2.7 git push

将文件提交到远程仓库

复制代码
git push

强制提交

复制代码
git push -f

推送当前本地分支到指定远程分支

复制代码
git push origin [branch-name]

2.8 git fetch

下载远程仓库的所有变动

复制代码
git fetch [remote]

三、分支

3.1 git branch

查看所有本地分支

复制代码
git branch

查看所有远程分支

复制代码
git branch -r

查看本地和远程的所有分支

复制代码
git branch -a

新建一个分支,但依然停留在当前分支

复制代码
git branch [branch-name]

新建一个分支,并切换到该分支

复制代码
git checkout -b [branch-name]

删除分支

复制代码
git branch -d [branch-name]

删除远程分支

复制代码
git push origin --delete [branch-name]

3.2 git checkout

切换到指定分支,并更新工作区

复制代码
git checkout [branch-name]

新建一个分支,并切换到该分支

复制代码
git checkout -b [branch-name]

切换到上一个分支

复制代码
git branch -

3.3 git merge

合并指定分支到当前分支

复制代码
git merge [branch-name]

四、回滚

4.1 仓库+暂存区回滚

将仓库与暂存区回滚到指定版本,工作区不变

也就是说恢复到git add .之前的状态

不支持仅回退单个文件

命令:

复制代码
git reset [HEAD]

HEAD说明:

  • HEAD 表示当前版本
  • HEAD^ 上一个版本
  • HEAD^^ 上上一个版本
  • HEAD^^^ 上上上一个版本

可以使用 ~数字表示

  • HEAD~0 表示当前版本
  • HEAD~1 上一个版本
  • HEAD^2 上上一个版本
  • HEAD^3 上上上一个版本示例:
  • 示例:
    回退上个版本

    复制代码
    git reset HEAD^
    或
    git reset HEAD~1

回退指定版本(版本号通过git log查看):

复制代码
git reset 9e8f2f858ae03944bab4ac19c927699ba367b941

回退上上个版本:

复制代码
  git reset HEAD^^
  或
  git reset HEAD~2

4.2 暂存区回滚

通过git add .添加到暂存区后,重置暂存区,与上一次commit保持一致,但工作区不变

撤回所有:

复制代码
git reset

复制代码
git reset HEAD

撤回指定文件:

复制代码
git reset [file]

例:

复制代码
git reset src/index.js

4.3 仓库回滚

已经通过git commit提交后,回滚到暂存区

也就是说,恢复到add后的状态

命令:

复制代码
git reset --soft [HEAD]

HEAD说明与4.1一致

示例:回退上个版本

复制代码
git reset --soft HEAD^
或
git reset --soft HEAD~1

4.4 本地+仓库+暂存区回滚

将本地、暂存、git仓库都回滚到指定版本

也就是所有内容都会恢复到上个版本,包括本地工作区内的所有修改

使用的时候要注意,它会删除回退点之前的所有信息

命令:

复制代码
git reset --hard [HEAD]

HEAD说明与4.1一致

示例:

回退上个版本

复制代码
git reset --hard HEAD^
或
git reset --hard hEAD~1

回退指定的版本

复制代码
git reset --hard 4f0cf3710b839e31ce3b5093f0d8fb0ad36e8c95

4.5 远程仓库回滚

步骤:先将本地版本回退到上一个版本,再push推送到远程仓库

示例:

复制代码
// 先回退本地仓库版本(注意--hard所有内容都会恢复到上个版本,包括本地工作区内的所有修改)
git reset --hard HEAD^
  
// 再提交到远程仓库
git push -f
相关推荐
jingshaoqi_ccc10 小时前
GitKraken最后一个免费版本和下载地址
git·github·gitkraken·版本管理工具
乌云暮年11 小时前
Git简单命令
git·gitee·github·batch命令
用户12592654232014 小时前
使用 Docker 搭建 Gitea 并实现 Git HTTP 自动登录
git
一只毛驴16 小时前
谈谈对git stash的理解?
git
长风破浪会有时呀21 小时前
Git 学习笔记
笔记·git·学习
中微子1 天前
Git Rebase 详解:概念、原理与实战示例
git
荔枝吻1 天前
【保姆级喂饭教程】Windows下安装Git Flow
windows·git·git flow
云和数据.ChenGuang1 天前
git中的指令解释
git
小Lu的开源日常2 天前
在 macOS 上设置 SSH 和 Git
git·macos·ssh
eleven_h2 天前
ERROR: Permission to Splode/pomotroid.git deni
git