github 语法流程图

本地初始化仓库
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:user_name/test_init.git
git push -u origin main
推送现有仓库到远程仓库
git remote add origin git@github.com:user_name/test_init.git
git branch -M main
git push -u origin main
克隆远程仓库到本地
git clone git@github.com:user_name/test_init.git
仓库管理
git init # 初始化新的Git仓库
git clone <url> # 克隆远程仓库
git remote add origin <url> # 添加远程仓库
git remote -v # 查看远程仓库
推送与拉取
git push origin <branch> # 推送到远程分支
git push -u origin <branch> # 设置上游分支并推送
git pull origin <branch> # 从远程拉取并合并
git fetch origin # 获取远程更新
git fetch --all # 获取所有远程更新
状态查看
git status # 查看工作区状态
git status -s # 简短格式显示状态
git log # 查看提交历史
git log --oneline -n # 单行显示最近n条提交历史
git log --graph # 图形化显示分支历史
git diff # 查看工作区与暂存区的差异
git diff --staged # 查看暂存区与HEAD的差异
文件操作
git add <file> # 添加文件到暂存区
git add . # 添加所有文件到暂存区
git add *.c # 添加所有.c文件
git rm <file> # 将文件从暂存区和工作区中删除
git rm --cashed <file> # 将文件从暂存区删除,工作区得以保留
git rm -f <file> # 将文件从暂存区和工作区中删除
git mv <old> <new> # 重命名文件
git checkout -- <file> # 从最近的一次提交中恢复文件,撤销工作区的修改,如果该文件曾经提交到 暂存区,则暂存区中的文件不受影响
git restore <file> # 恢复特定文件到暂存区状态
git restore . # 恢复所有文件到暂存区状态
提交管理
git commit -m "消息" # 提交更改
git commit -am "消息" # 添加并提交已跟踪的文件
git commit --amend # 打开编辑器,修改最后一次提交
git commit --amend -m "消息" # 直接修改最后一次提交
git reset HEAD <file> # 工作区不变,将本地仓库和暂存区修改到指定的<commit-hash>
git reset --soft <commit-hash> # 暂存区,工作区不变,将本地仓库修改到指定的<commit-hash>
git reset --hard HEAD # 完全重置到最后一次提交,暂存区,工作区,本地仓库均变化
分支管理
git branch # 查看本地分支
git branch -a # 查看所有分支,本地和远程
git branch <name> # 创建新分支
git checkout <branch> # 切换到指定分支
git checkout -b <name> # 创建并切换到新分支
git branch -d <name> # 删除分支
git branch -m <old> <new> # 重命名分支
合并与变基
git merge <branch> # 合并当前分支到<branch>分支,在流程图中有交叉
git rebase <branch> # 变基操作,将当前分支合并到<branch>分支,在流程图中没有交叉
git cherry-pick <commit-hash> # 将特定的<commit-hash>提交从一个分支引入到当前分支
标签管理
git tag # 查看所有标签
git tag <name> # 创建标签
git tag -a <name> -m "消息" # 创建带注释的标签
git push origin --tags # 推送所有标签
搜索查看
git show <commit> # 显示指定提交的详细信息
git blame <file> # 查看文件的每一行是谁修改的
git grep "关键词" # 在仓库中搜索关键词
git log -S "关键词" # 搜索包含关键词的提交
清理维护
git clean -n # 预览要清理的文件
git clean -f # 强制清理未跟踪的文件
git gc # 垃圾回收
git prune # 清理孤立的提交
stash操作
git stash # 将工作区,暂存区放置到 stash 区域,会删除工作区,暂存区的修改
git stash list # 查看 stash 列表
git stash pop <commit-hash> # 将stash区域保存的<commit-hash>状态恢复到工作区,暂存区,并删除该stash状态
git stash apply <commit-hash> # 将stash区域保存的<commit-hash>状态恢复到工作区,暂存区,不删除该stash状态