Git Cheat Sheet
部分参考官方的 Cheat Sheet。
基础操作
仓库初始化
1 | git init |
克隆远程仓库
1 | git clone <url> xxx |
查看仓库状态
1 | git status |
添加文件到暂存区
1 | git add <file> |
提交更改
1 | git commit |
查看提交历史
1 | git log |
分支基本操作
查看分支
1 | git branch -va |
查看相对于当前分支的未合并分支
1 | git branch --no-merged |
创建新分支
1 | git branch <branch_name> |
切换分支
1 | git switch <branch_name> |
新建并切换到分支
1 | git switch -c <branch_name> |
重命名分支
1 | git branch -m master main |
删除分支
1 | git branch -d <branch_name> |
删除分支并在远程仓库同步
1 | git branch --delete branch_nam |
分支合并操作
普通合并
1 | git switch master |
遇到冲突时直接撤销合并
1 | git merge --abort |
变基合并
1 | git switch experiment |
遇到冲突时直接撤销变基
1 | git rebase --abort |
压缩提交历史
1 | git rebase -i HEAD~4 |
撤销操作
撤销工作目录的修改(基于 index 重置工作目录)
1 | git restore filename |
撤销 index 的修改(基于 HEAD 重置 index),但不改动工作目录中的内容
1 | git restore --staged filename |
撤销 index 和工作目录的修改(基于 HEAD 重置 index 和工作目录),改动影响工作目录中的内容
1 | git restore --staged --worktree filename |
修正上一次的提交
1 | git commit --amend |
远程仓库基础操作
添加远程仓库为 orign
1 | git remote add origin <repo_url> |
查看远程仓库
1 | git remote -v |
查看远程仓库详细信息(联网更新)
1 | git remote show <remote_name> |
重命名远程仓库
1 | git remote rename <remote_name_old> <remote_name_new> |
移除远程仓库
1 | git remote remove <remote_name> |
远程仓库同步操作
获取远程仓库的信息(但是不会合并)
1 | git fetch |
这几个命令只在存在多个远程仓库时有区别。
拉取远程仓库的分支
1 | git pull <remote_repo> <remote_branch>:<local_branch> |
推送远程仓库的分支
1 | git push <remote_repo> <local_branch>:<remote_branch> |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
