git实用命令
整理
目的 / 粒度 | 分支级指令(操作整条历史/指针) | 文件级指令(操作单个或一批文件内容) | 作用范围说明 |
---|---|---|---|
新建指针 | git branch <new-branch> git checkout -b <new-branch> [<start-point>] | —— | 仅新建/切换指针,不影响工作区文件 |
删除指针 | git branch -d <branch> 安全删除git branch -D <branch> 强制删除 | —— | 只删指针,commit 仍存直到 gc |
重命名指针 | git branch -m <old> <new> | —— | 本地分支改名 |
切换分支 | git switch <branch> git checkout <branch> | —— | 工作区、暂存区、HEAD 整体切过去 |
把当前分支历史整体回退到某 commit | git reset --hard <commit> | —— | 整条分支指针直接移动,后面 commits 脱离分支 |
把当前分支历史回退但保留改动到暂存区 | git reset --soft <commit> | —— | 同上,仅指针移动,改动留 staged |
把当前分支历史回退且把改动退到工作区 | git reset --mixed <commit> | —— | 同上,改动 unstaged |
将整条分支合并到当前分支 | git merge <source-branch> | —— | 生成 merge commit,两条历史保留 |
将当前分支 rebase 到另一条基线 | git rebase <upstream-branch> | —— | 把整条分支 commits 依次重放,历史线性 |
把文件加入暂存区 | —— | git add <file-or-dir> | 仅影响暂存区,与历史无关 |
把文件从暂存区撤回工作区 | —— | git restore --staged <file> git reset HEAD <file> | 仅 unstaged,不改历史 |
丢弃工作区对某文件的修改 | —— | git restore <file> git checkout -- <file> | 拿 HEAD 版本覆盖工作区,不改历史 |
丢弃最近一次 commit 且丢弃改动 | git reset --hard HEAD~1 | —— | 整条分支后退 1 个 commit,文件同步回退 |
丢弃最近一次commit,并把改动退回到工作区(不保留在暂存区) | git reset --mixed HEAD~1 | —— | 分支指针后退,改动变为unstaged |
丢弃最近一次 commit 但保留文件改动 | git reset --soft HEAD~1 | —— | 分支指针后退,改动仍在 staged |
交互式改写任意历史 commit | git rebase -i <commit> | —— | 可删、改、合、重排整条分支的任意 commits |
推送整条分支到远程 | git push <remote> <local-branch>:<remote-branch> | —— | 无文件级等价指令 |
删除远程分支 | git push <remote> --delete <branch> | —— | 同上 |