首页 > 技术文章 > Git 笔记

tofengz 2021-09-10 19:40 原文

git

  • config
    • -l
  • init
  • status
  • branch
    • <branch-name>
    • -d <branch-name>
  • add
    • <file-name>
    • .
  • commit
    • -m ""
  • remote
    • -v
    • add <origin> <git-url>
    • rm <origin>
  • push
    • <origin> <branch-name>
  • pull
    • <origin> <branch-name>:<branch-name>
命令 选项 作用
config --local, --global, --system
init
status
remote -v, add
branch
log --graph
reflog
add
rm
restore --staged 用暂存区内容刷掉工作区,或者用仓库内容刷掉暂存区
commit
push
clone
fetch
pull
switch -c
stash list, pop, apply 将手头未提交的改动暂时“藏起来”,可以切换到别的分支上进行别的工作,最后再恢复。
cherry-pick 应用某个现有的 commit 造成的改动
rebase 把本地未push的分叉提交历史整理成直线

git clone 指定的单个目录

  • 创建空仓库
mkdir devops
cd devops
git init  # 初始化
  • 拉取远程仓库信息
git remote add -f origin http://your/git/repo.git  # 拉取远程仓库信息
  • 开启 sparse clone
git config core.sparsecheckout true  # 开启 sparse clone
  • 设置过滤
echo "devops" >> .git/info/sparse-checkout  # 设置过滤条件
  • 更新仓库
git pull origin master # 拉取仓库

使用 git restore & git switch 替代 git checkout

git checkout 这个命令承担了太多职责,既被用来切换分支,又被用来恢复工作区文件,对用户造成了很大的认知负担。

Git社区发布了Git的新版本2.23。在该版本中,有一个特性非常引人瞩目,就是新版本的Git引入了两个新命令 git switchgit restore,用以替代现在的 git checkout。换言之,git checkout 将逐渐退出历史舞台。

git checkout 的核心功能包括两个方面,一个是分支的管理,一个是文件的恢复。这两个核心功能,未来将由 git switchgit restore 分别负责。

工作流程

  • 主分支保持稳定;
  • 创建临时分支进行修改;
  • 合并后及时删除临时分支;

推荐阅读