Git Tips

2025

一、折腾 diff & difftool

git diff - 内建 diff
git difftool - 外建 diff
起因是在 Terminal 中 diff 不够直观,想着能否找到 vscode 中的 diff A/B 展示模式,结论是 diff 不行,difftool 可以,最接近的方案是 vimdiff,但是会弹出源文件+diff两个页面
config.[pager] - diff 页面由此配置,默认是 `less`,可以配置成 `cat`、`bat`等等 config.[diff] - 外建 diff 配置,`git difftool --tool-help` 查看可用的tools,其中除了 `vimdiff`,其他的都是打开一个GUI; config.[merge] - 外建 merge 配置

二、checkout & reset & revert & rebase & reflog

c0 --> c1 --> c2 --> c3<HEAD - main>
|
c2b<branch>
  1. checkout - Safe,Move HEAD Only,检出指定 branch/commit
  2. reset - Medium ~ High Danger,Move the Branch Pointer
    1. --soft:Move Branch Only,keep staging and working directory。如合并多个提交,则可以 soft reset 之后重新commit。
    2. --mixed:Branch + Staging。如拆分提交
    3. --hard:Move branch, clear staging and working directory, data loss possible!
  3. revert - Safe,New commit
  4. rebase - Move branch commits to other branch‘s HEAD;永远不要对别人已经看见的 commits 进行变基,Only rebase local,unpushed commits;别人已经拉取变基前的 commits,如果进行了变基,Git 会认为它们是完全不相关的工作,造成大量合并重复、冲突
  5. reflog - 记录提交记录日志,方便撤回;记录条目:90 天内的 Reachable Commits & 30 天内的 Orphan Commits,所以新鲜的误删除数据不用担心丢失,但需要记得按期找回。

2024

Ref Video[1]

1. 将当前未 tracked files 一起暂存

git config --global alias.staash 'stash --all'

2. Conditional Configs

// root/.gitconfig
// 对于特定目录下的项目,使用特定的 git config
// 注意:current config = root configs + specific config(相同项会覆盖前者)
[includeIf "gitdir:~/t/"]
path= ~/t/.gitconfig

3. no git blame -L, just log -L

🚫 git blame -L <start>,<end> path/to/file
👍 git log -L <start>,<end>:path/to/file
👍 git log -L:<func_name>:path/to/file

4. diff word-by-word

👍 git diff --word-diff path/to/file

5. rerere[2][2]" role="complementary" aria-hidden="true">#

“重用记录的解决方案(reuse recorded resolution)”所示,它允许你让 Git 记住解决一个块冲突的方法, 这样在下一次看到相同冲突时,Git 可以为你自动地解决它。

6. signing commit with ssh

git config gig.format ssh
git config user.signingkey ~/.ssh/pub.key
对提交进行签名,防止伪造的 user.name/user.email 提交 需要将 pub.key 预先上传到 code repo server

Date:
Words:
748
Time to read:
3 mins