个人Git超级备忘录
                 本文最后更新于 605 天前, 如有失效请评论区留言.
            
            
        git 的一些常用的命令及配置
配置 config
- 设置全局 git 的用户名和邮箱
 
git config --global user.name "Your Name"
git config --global user.email you@example.com
- 生成相关密钥
 
ssh-keygen -t ed25519 -C "you@example.com"
- 列出你的 git 相关配置
 
git config --list --show-origin
~/.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
~/.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
命令行几乎很少用
