Git——常用命令总结

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: Git——常用命令总结

前言

记录日常中常用的Git命令

安装

Git官网 | Git官网

Git下载 | Git下载

配置

配置user.nameuser.email

git config --global user.name 'your_name'
git cofig --global user.email 'your_email@domain.com'
# 将默认编辑器设置为vim
git config --global core.editor vim

作用域

# 某个仓库有效(默认)
git config --local
# 全局有效(当前用户所有仓库有效)
git config --global
# 系统所有登陆用户有效
git config --system

显示config配置

# 查看本地配置
git config --list --local
# 查看全局配置
git config --list --global
# 查看系统配置
git config --list --system

创建仓库

cd your_project 
git init 
git config --global user.name 'your_name'
git cofig --global user.email 'your_email@domain.com'
git remote add origin <git addr>
git add -A
git commit -m "add file"
git push

目录结构

.git
├─ COMMIT_EDITMSG
├─ FETCH_HEAD
├─ HEAD #该文件表示当前本地签出的分支,例如存储的值:ref: refs/heads/master
├─ ORIG_HEAD
├─ config
├─ description
├─ hooks
│    ├─ applypatch-msg.sample
│    ├─ commit-msg.sample
│    ├─ fsmonitor-watchman.sample
│    ├─ post-update.sample
│    ├─ pre-applypatch.sample
│    ├─ pre-commit.sample
│    ├─ pre-push.sample
│    ├─ pre-rebase.sample
│    ├─ pre-receive.sample
│    ├─ prepare-commit-msg.sample
│    └─ update.sample
├─ index #存储缓冲区(GitExtensions中的stage)的内容,内容包括它指向的文件的时间戳、文件名、sha1值等;(git三大区域:工作区,暂存区,历史记录区)
├─ info
│    ├─ exclude
│    └─ refs
├─ logs
│    ├─ HEAD
│    └─ refs
│           ├─ heads
│           ├─ remotes
│           └─ stash
├─ objects #存储对象的目录,本地仓库,git中对象分为三种:commit对象,tree对象(多叉树),blob对象
│    ├─ 06
│    │    └─ abbb19400a205bdbd1dc0a28b986323fc8b808
│    ├─ 0e
│    │    └─ 5d9122f24db7db88b7688759bf457be74e8643
│    ├─ 16
│    │    └─ 89c24264c1fde08df4773db3cfbf6316d7e2b3
│    ├─ 2c
│    │    └─ 936a4ca60dd5c8f4ceeb9fede83a2c2f2c69b5
│    ├─ 48
│    │    └─ 0be438bab34a5361d8949f690a346973b8eb79
│    ├─ 72
│    │    └─ 7c5abd67dfeb49ee214b6a112445fbb550d3c5
│    ├─ bd
│    │    └─ 3441fa796371e5f796a1d522f8052b763ad533
│    ├─ f1
│    │    └─ 894b05a9021dded510f71fef2be2cfb9864db6
│    ├─ info
│    │    └─ packs
│    └─ pack
│           ├─ pack-76de1ecd79b2e31879a027b482c07377610a8bd6.idx
│           └─ pack-76de1ecd79b2e31879a027b482c07377610a8bd6.pack
├─ packed-refs
└─ refs #存储指向branch的最近一次commit对象的指针,也就是commit对象的sha-1值(就是hash值,sha-1是一种散列算法),refs的目录下包括以下目录(git init后并没有remotes和stash,需要有从remote地址中pull code等交互性操作才会出现remotes目录,stash文件则是做了stash操作才会出现):
       ├─ heads
       │    ├─ dev
       │    ├─ master
       │    └─ tb-customer
       ├─ remotes
       │    └─ origin
       └─ stash

命令详解

add

# 将工作空间修改和删除的文件添加到暂存区
git add -u 
# 将工作空间新增和被修改的文件添加的暂存区
git add .
# 将工作空间新增、修改和删除的文件添加到暂存区
git add -A

mv

# 修改文件名称
git mv <old_name> <new_name>

rm

# 删除文件
git rm <file_name>

log

# 查看单行的简洁日志
git log --oneline
# 查看最近2条简洁日志
git log --oneline -n4
git log --oneline -4
# 查看所有分支的历史日志
git log --all
# 以图形化展示历史日志
git log --all --graph
# 以图形化展示所有分支的最近4条简洁日志
git log --all --oneline -4 --graph
# 在网页端查看git log命令文档
git help --web log
# 在GUI界面查看
gitk --all

commit

# 修改上一次提交commit信息
git commit --amend
# 修改指定commit信息
git rebase -i <commit_id>
r  <修改commit信息>
p  <保留commit信息>
# 把连续多个commit合并成一个
git rebase -i <last_commit_id>
p  <需保留的cmmit信息>
s  <需合并到上一个commit信息>
# 把间隔的commit合并成一个
git rebase -i <last_commit_id>
p  <需保留的commit信息>
s  <把需要合并的commit的移动到需要保留的commit下面并选择squash策略>

rebase策略

# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.

diff

# 比较暂存区和HEAD所含文件差异
git diff --cached
git diff --staged
# 比较工作区和暂存区所含文件差异
git diff 
# 比较工作去和暂存区指定文件差异(可比较多个文件)
git diff -- <file_name_1> <file_name_2>
# 比较两个commit之间的差别
git diff <commit_id_1> <commit_id_2>
# 比较两个commit之间的指定文件差别
git diff <commit_id_1> <commit_id_2> -- <file_name>

reset

# 将暂存区恢复成和HEAD一样
git reset HEAD
# 将暂存区指定文件恢复成和HEAD一样(可多个文件)
git reset HEAD -- <file_name_1> <file_name_2>
# 将暂存区恢复到指定的commit
git reset --hard <commit_id>
# 将指定文件的工作区恢复成和暂存区一样
git checkout -- <file_name>
git restore -- <file_name>

branch

# 查看本地所有分支
git branch -v
# 删除指定分支
git branch -d <branch_name>
# 强制删除指定分支
git branch -D <branch_name>

checkout

# 创建新的分支
git checkout -b <branch_name>
# 切换分支
git checkout <branch_name>
git switch <branch_name>

stash

# 存储当前修改
git stash
# 查看stash列表
git stash list
# 恢复之前存储内容(保留stash信息)
git stash apply
# 恢复之前存储内容(删除stash信息)
git stash pop

remote

# 添加远程分支(一般使用智能协议[http/https|ssh])
git remote add <local_barnch_name> <remote_address>
# 查看本地分支
git remote -v
# 查看所有分支
git remote -va

fetch

# 拉取远程所有分支
git fetch --all
# 拉取指定分支
git fetch <local_branch_name> <remote_branch_name>
#

merge

# 合并和远程不相关的分支
git merge --allow-unrelated-histories <remote_branch_name>
# 图形化合并界面
git mergetool

学无止境,谦卑而行.

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
2月前
|
存储 开发工具 git
|
2月前
|
开发工具 git
【GIT 第二篇章】GIT常用命令
Git常用命令涵盖初始化、状态管理、提交、分支处理、远程操作等关键流程。`git init`启动本地仓库,`git clone`下载远程仓库。通过`git status`和`git diff`检查工作状态与差异。利用`git add`暂存文件,`git commit`保存更改。借助`git branch`、`git checkout`、`git merge`和`git rebase`管理分支。使用`git fetch`、`git pull`和`git push`同步远程仓库。通过`git reset`、`git revert`和`git checkout`实现版本回退。
60 0
|
9天前
|
开发工具 git
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
这篇文章是关于Git常用命令的总结,包括初始化配置、基本提交、分支操作、合并、压缩历史、推送和拉取远程仓库等操作的详细说明。
39 1
git学习四:常用命令总结,包括创建基本命令,分支操作,合并命令,压缩命令,回溯历史命令,拉取命令
|
25天前
|
存储 Linux 开发工具
掌握 Git 命令:每个开发者的必备技能
无论团队项目还是个人开发,掌握 Git 命令都是必备技能。本文介绍 Git 的基本概念与命令,如初始化仓库 (`git init`)、添加文件 (`git add`)、提交更改 (`git commit`)、检出分支 (`git checkout`)、合并分支 (`git merge`) 等,还分享了高级技巧如查看差异 (`git diff`)、撤销提交 (`git revert`)、修复合并冲突 (`git mergetool`) 和使用别名简化命令 (`git config --global alias.ci commit`)。
|
28天前
|
机器学习/深度学习 Shell 开发工具
Python使用管道执行git命令报错|4-7
Python使用管道执行git命令报错|4-7
|
27天前
|
存储 Linux 开发工具
Git基础命令,分支,标签的使用【快速入门Git】
本文详细介绍了Git版本控制系统的基础概念和常用命令,包括工作区、暂存区和版本库的区别,文件状态的变化,以及如何进行文件的添加、提交、查看状态、重命名、删除、查看提交历史、远程仓库操作和分支管理,还涉及了Git标签的创建和删除,旨在帮助读者快速入门Git。
Git基础命令,分支,标签的使用【快速入门Git】
|
29天前
|
存储 Linux 开发工具
掌握 Git 命令:每个开发者的必备技能
本文介绍 Git 的核心概念,如仓库、提交、分支与合并,并提供了常用命令,如初始化仓库 (`git init`)、提交更改 (`git commit -m &quot;Commit message&quot;`)、拉取 (`git pull`) 和推送 (`git push`) 等。此外,还分享了高级技巧,如撤销提交 (`git revert &lt;commit&gt;`)、交互式暂存 (`git add -i`) 和使用别名简化命令 (`git config --global alias.ci commit`) 等,帮助开发者提升效率。无论是初学者还是资深开发者,都能从中受益。
|
1月前
|
开发工具 git 开发者
GIT命令的综合总结
Git的学习曲线可能比较陡峭,但熟练掌握这些命令后,你将能够更加高效地管理和协作你的项目。希望这份指南能帮助你成为Git的高效用户。
46 7
|
25天前
|
开发工具 git
深入理解Git中的git pull和git fetch命令
深入理解Git中的git pull和git fetch命令
23 0
|
2月前
|
存储 开发工具 git
Git常用命令汇总
这是Git命令速查表,涵盖从版本库创建、文件添加与提交、状态查询到分支管理、标签创建及撤销操作的各项常用指令。同时介绍了如何通过GitHub进行代码仓库的创建与同步,帮助用户高效地使用Git进行版本控制和协作开发。
Git常用命令汇总

相关实验场景

更多