惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

Echo

Codex 自制宠物 U-Boot、内核移植与根文件系统构建(BeagleBone Green Gateway&AM335X) 你们搞大模型的就是? 2025年终总结 实习小记 2024年终总结 2023年终总结 讯飞免费星火大模型部署教程 Argon主题博客美化
Git命令汇总
作者: Echo · 2023-12-21 · via Echo

前言

单纯的对一些经常用的 git 命令进行总结,方便以后查询使用,没啥营养

1. 基础

git add          # 提交到 暂存区
git commit -m "commnet"    # 提交到 版本库
git branch -M main # 重新命名分支
git remote add origin # 添加远程仓库
git pull origin master # 从名为 origin 的远程仓库的 master 分支拉取最新的提交,并将其合并到当前分支
git push origin main   # 将本地仓库的文件push到远程仓库(若 push 不成功,可加 -f 进行强推操作)
git diff read.txt # 查看文件变化

2. 版本回退

git reset --hard HEAD^ # 恢复到上一个版本
git reset --hard HEAD~10 # 恢复到网上10个版本
git reset --hard commitID # 恢复到指定commit版本

3. 撤销修改

git restore  # 工作区
git restore --staged  # 暂存区,工作区需要执行上一步 add
git reset --hard HEAD^ # 工作区、暂存区、本地仓库都回退 commit

4. 删除文件

rm file
git add file
git commit -m ""

5. 分支操作

git branch test # 创建分支 test
git branch # 查看当前分支
git switch -c test # 创建test分支,然后切换到test分支 git branch test git checkout test
git switch master # 切换
git merge test # 合并指定分支到当前分支
git merge --no-ff -m "no-ff" test # 禁用快进(fast-forward)合并,强制创建一个新的合并提交
git branch -d test # 删除分支
git branch -D test # 强制删除
git log --graph # 查看分支合并图

6. 保存和恢复

git stash save "Your stash message" # 保存工作进度
git stash list # 查看 stash 列表
git stash apply [] # 应用
git stash drop [] # 删除
git stash pop [] # 应用并删除
git cherry-pick # 将一个或多个提交从一个分支应用到另一个分支

7. 多人协作

git remote -v # 查看远程库的信息
git switch -c dev origin/dev # 本地创建一个新分支 dev,并将其设置为跟踪远程仓origin/dev 分支
git branch -u origin/dev dev # 本地分支 dev 设置为跟踪远程仓库的 origin/dev 分支
git push origin master   # 将本地仓库文件push到远程(若push不成功,可加-f进行强推)
git pull origin master # 从远程仓库origin的master分支拉取最新提交,并将其合并到当前分支
git rebase # 把本地未push的分叉提交历史整理成直线

8. 标签

标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签。
git tag v1.0 # 打一个新标签V1.0,默认是打在最新提交的commit上
git log --pretty=oneline --abbrev-commit # 每一行包含了一个提交的简略哈希和提交信息
git tag v0.9 f52c633 # 在特点commit上打标签
git tag -a v0.1 -m "v0.1 released" 1094adb # 创建带有说明的标签,用-a指定标签名,-m指定说明文字
git tag # 查看所有标签
git show v0.9 # 查看标签信息,标签不是按时间顺序列出,而是按字母排序
git tag -d v0.1 # 删除标签
git push origin v1.0 # 推送标签到远程
git push origin --tags # 一次性推送全部尚未推送到远程的本地标签
git push origin -d tag tagName # 删除一个远程标签
git ls-remote --tags origin # 查看删除远程tags执行效果