首次升级 Git 的 3 个理由
Git 的发展速度惊人 —— 截至 2021 年 8 月,Git 的当前版本 ( git --version
) 为 2.33。
我通常不知道或关心我的 Git 版本是什么,但在过去的一年里,情况发生了变化。Git 团队肯定做了一些正确的事情,因为我注意到,即使我相对较少使用 Git,也发生了一些变化:
init.defaultBranch
Git 中的默认分支名为master
。从 Git 2.28 开始,您可以将其配置为其他任何名称。当然,这只是象征性的,但值得赞赏。
git config --global init.defaultBranch main
此设置适用于新的仓库。对于现有仓库,以及与 和 都能良好兼容的 git 别名master
,main
您可以查看我的从 Master 迁移到 Main 的备忘单。
git sparse-checkout
你可能知道,git clone -–depth 1 [remote-url]
有些仓库你不想下载完整的历史记录。Git 2.25 为巨型仓库引入了一个额外的命令,即使当前版本也太大了。因此,稀疏检出类似于,git clone --depth 1
但对于文件来说:
git clone --no-checkout https://github.com/my/big-repo
cd big-repo/
# only files at the root
git sparse-checkout init --cone
# only files in specific directories (recursive)
git sparse-checkout set <dir1> <dir2> ...
# do it
git checkout main
git restore
我今天需要这个。基本上,如果你搞砸了一个文件,需要从 Git 中恢复它,你可以使用git restore
v2.23 中的新命令:
git restore path/to/file # discard current changes
git restore --staged path/to/file # unstage staged file
git restore --source [hash] path/to/file ## restore from specific commit hash
⚠️ 请小心 - 一旦运行此命令,您将无法恢复丢弃的更改。
checkout
是的,在、restore
、reset
甚至之间确实存在一些容易混淆的类似命令switch
。StackOverflow上的这个答案给出了相当权威的解释:
git-revert
是关于进行一次新的提交,以恢复其他提交所做的更改。git-restore
是从索引或其他提交恢复工作树中的文件。此命令不会更新您的分支。该命令也可用于从其他提交恢复索引中的文件。git-reset
更新分支,移动尖端以添加或删除分支中的提交。此操作会更改提交历史记录。git reset 也可用于恢复索引,与 git restore 功能重叠。