Git basic commands, git 常用命令

Git basic commands 目前比较流行的版本管理系统 相比同类软件,Git有很多优点。其中很显著的一点,就是版本的分支 (branch) 和合并 (merge) 十分方便。 有些传统的版本管理软件,分支操作实际上会生成一份现有代码的物理拷贝,而Git只生成一个指向当前版本 (又称"快照") 的指针,因此非常快捷易用。 commands git status --porcelain #用于以简洁、脚本友好的格式显示 Git 仓库当前的状态,方便自动化处理和快速查看文件的变动情况。 git ls-files --unmerged git diff --name-only --diff-filter=U #显示出 HEAD 对应的提交的 hash 值 git rev-parse HEAD git rev-parse --short HEAD # windows install git winget install Git.Git # 显示工作树状态, (已经修改但是没 git add, 或者 没有 git commit) # 显示索引文件和当前HEAD提交有差异的路径,工作树和索引文件有差异的路径,以及工作树中不被Git追踪的路径(也不被gitignore[5]忽略)。前者是你通过运行 "git commit "会提交的东西;第二和第三者是你在运行 "git commit "之前通过运行 "git add "可以提交的东西。 git status -s # 查看本地仓库的当前分支和远程分支的差异(已经 commit 但是还没 push), 展示方式类似 git log git log master ^origin/master # 计算对象数和磁盘消耗 git count-objects -vH # 指定目录 1.8.5 以前 git --git-dir=/Users/jhoffmann/tmp/my-project/.git --work-tree=/Users/jhoffmann/tmp/my-project/ pull # 指定目录 >=1.8.5 git -C /Users/jhoffmann/tmp/my-project/ pull options -C, 大写 C 指定目录, .git 所在的目录 –version, 查看 git 版本 origin https://www.zhihu.com/question/27712995 ...

Created 18186-03-18 · Updated 9096-10-09 · 20 min · 4069 words · -

Git Shallow Clone 浅克隆与部分克隆

为什么要用 git clone 默认会把整个历史的所有对象都拉下来。有些场景只需要最新的一份代码,甚至只需要其中一个文件,例如 CI 里构建镜像、流水线里改一行 GitOps manifest 的镜像 tag。这时完整克隆既慢又浪费,网络差的时候还可能超时。 Git 提供了三种互相独立、可以叠加的裁剪方式: 方式 参数 裁掉什么 浅克隆 shallow clone --depth N 历史:只要最近 N 个 commit 部分克隆 partial clone --filter=blob:none 等 文件内容:blob 用到时再按需下载 稀疏检出 sparse checkout --sparse + git sparse-checkout 工作区:只检出指定路径 浅克隆 –depth # only the latest commit git clone --depth 1 https://github.com/foo/bar.git # --depth implies --single-branch; name the branch explicitly git clone --depth 1 --branch main https://github.com/foo/bar.git # by time instead of count git clone --shallow-since=2026-01-01 https://github.com/foo/bar.git --depth 默认带上 --single-branch,只拉一个分支。需要其它分支时,加 --no-single-branch。 ...

Created 2026-10-02 · Updated 2026-10-02 · 2 min · 413 words · -

Git

Git graph LR work["Working tree工作树/工作区"] stage["Index / Staging Area暂存区"] repo["Local Repository本地仓库"] remote["Remote Repository远程仓库"] discard["✖ discard丢弃"] work -->|add| stage stage -->|commit| repo repo -->|push| remote remote -->|pull| work remote -->|"fetch/clone"| repo stage -->|checkout| work stage -->|"restore --staged"| work repo -->|merge| work repo -->|"reset --soft"| stage repo -->|rebase| stage repo -->|"reset --mixed"| work repo -->|"reset --hard"| discard 工作区 (working tree) working tree, 2.9.1 之前被称作 Working Directory https://stackoverflow.com/questions/39128500/working-tree-vs-working-directory 也称工作区, 工作目录、工作副本 我们日常开发操作是在工作区中进行的。 工作区的内容会包含提交到暂存区和版本库(当前提交点)的内容,同时也包含自己的修改内容。 索引 (Index, stage, Staging Area) 索引文件也在 .git 目录里 ...

Created 6066-03-06 · Updated 15156-09-15 · 4 min · 707 words · -

git reset

丢弃单个文件的本地修改 git reset <file> 只能取消暂存(把文件从 staging area 退回工作区),不能丢弃工作区的修改。 要丢弃工作区对某个文件的修改,应使用 git restore: # 丢弃工作区修改,还原为最近一次 commit 的版本 git restore <file> # 等价的旧语法(Git 2.23 之前) git checkout -- <file> # 还原为远程分支的版本(先 fetch 确保远程是最新的) git fetch origin git restore --source=origin/main <file> # 或者用旧语法 git checkout origin/main -- <file> 如果文件已经 git add 进了暂存区,需要先 unstage 再丢弃: # Step 1: 取消暂存(推荐,Git 2.23+) git restore --staged <file> # 旧语法(Git 2.23 之前),效果相同但已不推荐 git reset HEAD <file> # Step 2: 丢弃工作区修改 git restore <file> git restore –staged git restore --staged <file> 是 Git 2.23 引入的新命令,用于取消暂存(unstage),即把文件从暂存区退回工作区,是 git reset HEAD <file> 的现代替代语法。 ...

Created 29296-01-29 · Updated 14146-05-14 · 6 min · 1175 words · -