Section1: Git Basic Commands
- git config
git config --global初始化配置用户信息
- git add A
- 工作区 to 缓存区
- A 有文件名、大小、内容和类型等属性
- 向 缓存区 add git object 对象,使用 SHA1 算法
- 这里不涉及文件名
- 多次 git add 产生多个 block 对象,但只有 git commit 前最后一次的 block 对象才会被引用,其他中间对象都属于垃圾对象
- git commit
- 缓存区 to 远程(local origin)
git commit -m 'message'提交信息git commit -a展示'Merge branch..' 这句提示- commit 后 add git object 两个对象
- commit 类型,包括所指向的 tree,author 信息和 commit message
- tree 类型,记录了目录结构/文件的信息
- history commit 记录,可以找到对应的修改记录/内容
- git status,查看工作区文件的状态
- 比较工作区和缓存区的文件差异,从而判断状态
- type
- untracked,工作区 new add A
- staged,git add A,添加到缓存区
- modified
- git add 后再次修改 A
- 比较得到 A 前后版本不一样,再 git add 后可进行同步
- unmodified,git commit 后
- staged to untracked
- 在缓存区删除,
git rm --cached A - 在缓存中回滚,
git restore -stage A
- 在缓存区删除,
- untracked 回滚
git restore A
- git log,查看提交记录
- 具体参数
git log --online一行显示 commitgit log -2显示最新的两个 commitgit log--author='Mia'搜作者git log--merges搜 mergegit log--grep='feat:'搜索关键字的 commit
- git shortlog,以 author 查看提交记录
git shortlog -n提交的 commit 数量排序git shortlog -sauthor 和 commit 次数
- 具体参数
Notice:
- git object type
- blob:存储文件内容
- commit
- tree
- tag
- hash 算法:随意长度到固定长度
- MD5
- SHA1
- input: 'blob 大小\0 内容'
- output:长度 160bits 的 hash 值
- 由文件大小、内容和类型决定
- SHA256
- SHA516
Section2: Branch and Head
- git branch,查看已有的分支
git branch 分支名创建分支git branch -d如果分支的改变没有合到 master,会提示 not mergedgit branch -D强制删除分支,不提示信息git branch -m old new重命名分支名- *,当前分支
- git checkout 分支名,切换分支
git checkout dev切换到 dev 分支,dev 指向分支上最新 commit,等价于 git checkout 最新 commit 的 hash 值git checkout -b 分支名创建分支+切换分支,已有的分支不可以
- git reflog,查看曾经的操作
- git diff,比较文件在工作区/索引区/远程的区别
Notice:
- branch,是有名字的 poniter(也就是分支名)
- Head,是 pointer,指向当前分支,且是 lastest commite
- 删除的分支与当前分支,不能一致
- 误删除 dev 分支,现想恢复 dev 分支的最新 commit
git reflog找到 dev 分支的最新 commit 的 hash 值git checkout hash值- ls,查看是否恢复
- 文件回滚到 commit 前的状态的方法有
git restore 文件名git checkout 文件名
Section3: Branch Merge Methods
- Fast forward
- master 的最新 commit 是 C2,bugfix 分支的最新 commit 是 C3,C2 是 C3 的 parent,此时 bugfix 分支需要合到 master
- (master)git merge bugfix
- 产生一个 ORIG_HEAD 文件,用于保存 merge 前的 commit 信息,用于回滚
- Head 从 master 指向 bugfix 分支
- 应用场景 * bugfix 分支有 master 最新的 commit * master 之后不再有额外的 commit
- 3-way merge
- bugfix 分支的最新提交是 C3,C2 是 C3 的 parent,master 有新 commit C4,此时 bugfix 分支需要合到 master
- (master)git merge bugfix
- 产生新的 commit 对象 C5,有两个 parent C3 和 C4
- Head 指向 master
- 若 C3 和 C4 是同一个文件的改动,需要解决 conflict,然后再产生 C5
- (master)git merge bugfix
- rebase
- 与 3 way merge 的情况一致
- (dev) git rebase master
- 由于 dev 分支的 commit 已经提交到远程,rebase 之后,可能会有新改动/conflict,导致重写
- 使得正常 push 会出错,使用 git push -force
- Head 指向 dev 分支
* git rebase -i HEAD~4,合并最近四次提交记录
Notice
- 在解决冲突时
- current change,指 C4
- incoming change,指 C3
- git rebase master
- git rebase --continue,继续解决冲突(保存退出:wq),直至 successfully rebased
- git push -force
- 区别:
- Merge 会将两个分支合并到一起,并生成一个新的 commit 记录。新生成的 commit 节点会有两个父节点;
- rebase 的好处是能保持线性的提交历史,从而使历史更加清晰,还可用于整理 commit 记录;
- cherry-pick 而是将一些 commit 复制到当前的分支的 HEAD 上
- rebase黄金法则
- 不能在一个共享的分支上进行git rebase操作
- 所谓共享的分支,即是指那些存在于远端并且允许团队中的其他人进行Pull操作的分支,比如master分支
- 会把之前的commit重新check作为新的commit进行提交,
- 特别注意!
- 到公共分支的时候用,只能git merge
- 不影响原分支的提交记录,只多了一个merge commit
- 到开发分支的时候用,git rebase
- 到公共分支的时候用,只能git merge
Section4:Git remote
- git remote,查看 local 的远程仓库名
- -v,详细的列出远程仓库名以及对应的 URL,fetch 和 pull
- add my-origin [url]
- 本地仓库添加一个 my-origin 的远程仓库
- remove my-origin
- 删除 my-origin 远程仓库
- show my-origin
- 查看远程仓库 my-origin 与 local 的关联情况
- git branch,查看 local 分支
- -a,local 和 remote 的分支
- -r,remote 的分支
- -vv,查看 local branch 和 local origin 的 关联
- git fetch
- remote 的改动更新到 local origin
- local origin 的改动不会同步到 remote
- branch,会存在 local 有 remote 没有的 branch
- -v,查看真正的 remote branch 和 local origin 的 关联
- git pull
- git fetch and git merge
- fetch,将 remote 的改动拉到 local origin
- merge,将 remote 改动与 local origin 改动进行合并,方式有 fast-forward 和 3 way merge
- 如果一定有冲突,可以先 fetch,再 git diff origin/master 查看是否有冲突,最后 merge 解决冲突
- --rebase,以免去分支中出现大量的merge commit,基本原理就像上面rebase一样,合并过程会直接融合到目标分支的顶端,形成线性历史提交。
- git fetch and git merge
- git push
- my-origin dev,向远程 dev push
- my-origin -d dev,删除远程 dev 分支
- -u my-origin dev,
- 向远程 dev push,
- 且与 origin/dev 分支进行关联
- 若已经关联,则-u 不必要
- git reset
- --hard Hash 值/远程分支,回滚
Notice
- local branch,remote branch 包括 local origin 和真正的 remote branch
- git remote show my-origin,可以查看 remote 分支和 local 分支的同步
- remote 有,local 有,tracked 表示关联
- remote 有,local 没有,new 表示是新分支
- remote 没有,local 有,stale 表示分支失效
- git remote prune
- git branch --prune
- 通过以上命令,删除无效的分支
- FETCH_HEAD 文件
- 记录了分支的 fetch 记录
- 在哪个分支上 fetch, FETCH_HEAD 保存该分支的最新 commit
- commit/push的修改
- push到远程后,撤回最新的提交且保留在本地
- git reset --soft HEAD^1
- git push -f
- 撤回更改之后,需要推上远程才能保证分支干净,commit也会覆盖;否则,还是原来的提交
- 未push到远程后,更新commit
- push到远程后,撤回最新的提交且保留在本地
- git reset --soft之后,发现reset错分支,需要撤回
- git reflog,看一下操作记录
- git reset --hard commitid
- commitid,reflog的应该和repo的一致
- 本地直接回滚到最新的commit,远程无影响
Section5:Git SSH KEY
- SSH key,用于 client 和 serive 之间的验证
Section6:Git Tags 两种类型:
- lightweight
- 创建一个 tag name 的指针指向 commit
- annotated
- pointer and
- tag 对象
- tag author and date
- tag message
Notice
- tags(打标签),本地和远程的同步操作
- Tags 与 branch 的区别
- tags 是静态 pointer
- branch 是动态的 pointer,一直指向最新的 commit
Section7:Git hook 例如,
- commit 之前有 pre-commit 的 hook
Section8:Git Best Pratice
- 写 commit message
- .gitigore 文件,记录了忽略项目中的哪些文件或文件夹
- 基于 branch 的开发
- git commit --amend,修改本地已有 commit 历史
- 那么,本地和远程的 commit 历史不一样,git push origin master 会有冲突
- git push origin master -force,强制 push
- 不修改 author 信息
- --reset-author,可修改 author
- git stash,缓存当前改变,栈
- list,查看缓存内容
- pop,取出缓存内容
- WIP:work in progress
- git worktree
Git LowLevel Commands
- git ls-files
- -s 查看缓存区的内容
- git cat-file
- -t 查类型
- -p 查内容
详细内容见:www.escapelife.site/posts/da895… merge和rebase区别:dingjingmaster.github.io/2022/05/000…