Git 各个区的区别及状态

Git有三大区(工作区、暂存区、版本库)以及几个状态(untracked、unstaged、uncommited)

三个存储区状态的演变顺序

在这里插入图片描述

存储区流转

1.执行 git add,把工作区文件修改添加到暂存区
2.执行 git commit,把暂存区的所有内容提交到版本库

状态流转

1.打开项目目录,除了隐藏的.git文件夹,其他项目文件位于的地方便是工作区
2.在工作区新增一个文件,状态是 untracked(未跟踪)
3.在工作区编辑一个文件,状态是 unstaged(未暂存)
3.git add 命令实际上就是把要提交的所有修改放到暂存区(stage),Git 便会在暂存区中生成一个该文件的索引,文件此时处于 uncommited 状态
4.执行 git commit 就可以一次性把暂存区的所有修改提交到版本库

restore、reset 和 rm

git restore

> git restore -h
usage: git restore [<options>] [--source=<branch>] <file>...

    -s, --source <tree-ish>
                          which tree-ish to checkout from
    -S, --staged          restore the index
    -W, --worktree        restore the working tree (default)
    --ignore-unmerged     ignore unmerged entries
    --overlay             use overlay mode
    -q, --quiet           suppress progress reporting
    --recurse-submodules[=<checkout>]
                          control recursive updating of submodules
    --progress            force progress reporting
    -m, --merge           perform a 3-way merge with the new branch
    --conflict <style>    conflict style (merge or diff3)
    -2, --ours            checkout our version for unmerged files
    -3, --theirs          checkout their version for unmerged files
    -p, --patch           select hunks interactively
    --ignore-skip-worktree-bits
                          do not limit pathspecs to sparse entries only

其中 index 代表暂存区,working tree 代表工作区

  • 丢弃工作区目录中的更改:git restore <file>…
  • 丢弃暂存区目录中的更改:git restore --staged <file>…

git reset

> git reset -h
usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
   or: git reset [-q] [<tree-ish>] [--] <paths>...
   or: git reset --patch [<tree-ish>] [--] [<paths>...]

    -q, --quiet           be quiet, only report errors
    --mixed               reset HEAD and index
    --soft                reset only HEAD
    --hard                reset HEAD, index and working tree
    --merge               reset HEAD, index and working tree
    --keep                reset HEAD but keep local changes
    --recurse-submodules[=<reset>]
                          control recursive updating of submodules
    -p, --patch           select hunks interactively
    -N, --intent-to-add   record only the fact that removed paths will be added later

其中 HEAD 代表版本库,index 代表暂存区,working tree 代表工作区

  • 撤销已提交的版本库,不会修改暂存区和工作区:git reset --soft 版本库ID
  • 撤销已提交的版本库和暂存区,不会修改工作区:git reset --mixed 版本库ID
  • 彻底将工作区、暂存区和版本库记录恢复到指定的版本库:git reset --hard 版本库ID

git rm

git rm -h
usage: git rm [<options>] [--] <file>...

    -n, --dry-run         dry run
    -q, --quiet           do not list removed files
    --cached              only remove from the index
    -f, --force           override the up-to-date check
    -r                    allow recursive removal
    --ignore-unmatch      exit with a zero status even if nothing matched
  • 将文件从暂存区和工作区中删除:git rm <file>…
  • 如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f:git rm -f <file>…
  • 如果想把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 --cached 选项即可:git rm --cached <file>…

git diff 比较文件

  • 比较工作区和暂存区的所有文件差异:git diff
  • 比较工作区和暂存区的指定文件的差异:git diff <file name>
  • 比较工作区与 HEAD ( 当前工作分支) 的指定文件的差异:git diff HEAD <file name>
  • 比较暂存区与 HEAD 的指定文件的差异:git diff --staged 或 --cached <file name>
  • 比较当前分支与 branchName 分支的指定文件的差异:git diff branchName <file name>
  • 比较某一次提交的指定文件的差异:git diff commitId <file name>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值