个人Git超级备忘录

本文最后更新于 64 天前, 如有失效请评论区留言.

git的一些常用的命令及配置

配置config

  1. 设置全局git的用户名和邮箱
git config --global user.name "Your Name"
git config --global user.email you@example.com
  1. 生成相关密钥
ssh-keygen -t ed25519 -C "you@example.com"
  1. 列出你的git相关配置
git config --list --show-origin
  1. ~/.gitconfig 你的git配置文件

示例:

[core]
	excludesfile = /Users/ysicing/.gitignore_global

[user]
	name = ysicing
	email = ysicing@12306.work

[https]
	proxy = https://127.0.0.1:7890

[includeIf "gitdir:~/Work/gitea/oss"]
	path = ~/.oss.gitconfig
  1. ~/.gitignore_global 你的全局忽略文件

示例:

*~
.DS_Store
.scannerwork
venv
.venv
.idea
.vscode

__pycache__

dist/
_output/

*.db
.history

初始化或者clone

git init
git clone git@example.com:user/repo.git

检测状态

git status

用于查看 Git 仓库状态

  • 当前分支
  • 未暂存的变更(列出已修改但尚未执行 git add 的文件)
  • 暂存区的变更(已暂存但尚未执行 git commit 的文件)
  • 未跟踪的文件(新文件)

比较

git diff

使用最多的大概有如下几种了

  • git diff --stat 显示简洁的统计信息,包括文件的添加、修改和删除数量
  • git diff --color-words 比没参数简洁,少了+/-
  • git diff <commit>..<commit> 比较两个不同提交之间的差异

暂存

  • git add . 添加所以变更到暂存区
  • git add [file] 添加某个文件(支持通配符)到暂存区
  • git rm --cached [file] 从暂存区移出,不会对文件有影响
  • git clean -fd 是一个用于清理工作目录中未被 Git 跟踪的文件和目录(很少用)

提交

git commit

只会commit😂

暂存stash

通常只用在多任务场景下,避免互相干扰的情况,将当前工作目录中的所有已修改但未暂存的文件和已经暂存但未提交的文件保存到一个临时存储区域,恢复工作目录到最后一次提交时。

  • git stash 默认会暂存所有已修改但未暂存的文件和已经暂存但未提交的文件
  • git stash list 查看当前暂存列表
  • git stash drop 删除暂存
  • git stash apply 应用暂存且不删除
  • git stash pop 应用暂存删除暂存(一次性)

历史

git log  --oneline --since="2023-03-07"

我常用的大概是这些。时间格式推荐YYYY-MM-DD

分支

  • git branch 查看当前分支
  • git branch -a 查看所有分支
  • git branch [name] 新建分支[name]
  • git branch -d [branch] 删除本地分支
  • git branch -dr [remote/branch] 删除远端分支
  • git checkout [branch/commit] 切换分支
  • git checkout -b [branch] 基于当前分支新建一个分支并切到该分区

标签

  • git tag -a [name] -m [message] 添加标签
  • git tag -d [name] 删除标签
  • git push [remote] [tagname] 推送标签

Remote

  • git remote -v 查看远端
  • git remote rename origin [dst] 重命名
  • git remote remove origin 删除
  • git remote add origin [url] 新添加,通常和第二或者第三搭配用

Pull&Push

  • git fetch [remote] 查询远端
  • git pull/push [remote] [branch]
  • git push --tags 推送所有标签

其他

  • git checkout . 丢弃未提交的所有变更
  • git checkout path/to/file 丢弃特定文件
  • git reset HEAD~1 撤销最近一次的提交

rebase

命令行几乎很少用

Sponsor

Like this article? $1 reward

Comments

优惠券平台 ·v1 安徽马鞍山 Reply

已收藏!!!多谢大佬分享这么好的资源教程!!!已关注博主网站!!!会长期访问!!!

According to the relevant laws and regulations of the People's Republic of China, the comment function of the current website has been disabled. If you need to comment, please visit ysicing.me, but the comments still need to be reviewed by AI.