Skip to content

Instantly share code, notes, and snippets.

@mitoop
Last active June 21, 2021 02:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitoop/9316266cf14c4dc003d58d38d3a52dcd to your computer and use it in GitHub Desktop.
Save mitoop/9316266cf14c4dc003d58d38d3a52dcd to your computer and use it in GitHub Desktop.
git tips

git 三个区

  1. 工作区 working directory
  2. 暂存区 staging area
  3. 本地版本库 local repository

git area

恢复工作树的三种方式以及区别

  1. git checkout . git 2.23版本 git checkout 命令 拆分为 git switchgit restore 两个命令,更直观了
  2. git reset --hard HEAD
  3. git clean -df
`git checkout .` 只对工作区内跟踪过的文件有作用
`git restore --staged .` 可把暂存区内文件回退到工作区

`git reset --head HEAD` 对工作区和暂存区的跟踪过的文件都起作用 对未跟踪的文件不起作用

`git clean -df` 只对工作区内未跟踪的文件生效

所以 彻底恢复工作树的命令为 git reset --hard && git clean -f -d

checkout-reset-clean

git 忽略一个文件

.gitignore 只对未跟踪的文件起作用

如果文件已经跟踪过则需要移除

  1. 添加文件到 .gitignore
  2. 执行 git rm --cached 文件 并提交
  3. 重新创建这个文件就不会被跟踪了

git reset --hard --soft --mixed 区别

  1. git reset --soft commit id 回退到指定版本, 变动的文件会保留在工作区和暂存区
  2. git reset --hard commit id 回退到指定版本, 变动的文件不会保留
  3. git reset --mixed commit id 回退到指定版本, 变动的文件会保留在工作区

--soft

--soft

--hard

--hard

--mixed

--mixed

回退命令

  1. git reset --hard commit id 回退到指定版本, 这个版本之后的记录都会删除 可以用 git reflog 查看删除过的记录
  2. git revert commit id 撤回某次提交

分支合并 待补充

  1. git merge branch
  2. git rebase branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment