跳转到内容

git 问题记录

Git 在 macOS / Windows 上默认对文件名大小写不敏感,直接改文件名往往不会被识别为变更。

  1. 开启大小写敏感(建议只在当前仓库设置,避免影响其他项目)

    Terminal window
    $ git config --local core.ignorecase false
    # 查看当前配置(false 表示敏感)
    $ git config --get core.ignorecase
  2. git mv 两步重命名(以 demoDemo 为例)

    Terminal window
    $ git mv demo demo.tmp # 先改为临时名
    $ git mv demo.tmp Demo # 再改为目标名
  3. 若目标大小写版本已经存在于远程(历史遗留),需先清除旧索引再提交

    Terminal window
    $ git rm --cached demo # 移除旧名称的缓存记录
    $ git add Demo
    $ git commit -m "fix: rename demo to Demo"
Terminal window
$ git fetch --all
$ git reset --hard origin/main # 将本地 main 重置为远程最新状态
  1. 在现有代码目录重新初始化,并关联远程地址

    Terminal window
    $ git init
    $ git remote add origin <origin-url>
    $ git fetch
  2. 以远程 main 为准建立本地分支(会覆盖本地与远程不一致的内容)

    Terminal window
    $ git checkout -B main origin/main
    # 等价写法:
    # git reset --hard origin/main
    # git branch --set-upstream-to=origin/main main
Terminal window
$ git config --global http.sslVerify false

情况一:改动已经 commit 过

Terminal window
# 查看所有 HEAD 变动记录(含已被 reset 的提交)
$ git reflog
# 恢复到目标 commit(将 <commit> 替换为 reflog 中的 hash)
$ git reset --hard <commit>

情况二:改动尚未 commit

本地工作区文件被覆盖或未保存时,可借助编辑器的历史记录功能恢复,例如 VS Code 的 Timeline 面板查看文件本地历史版本。

Terminal window
# 先拉取远程变更,并将本地提交变基到远程之上
$ git pull --rebase
# 若有冲突:手动解决后
$ git add .
$ git rebase --continue
# 若要放弃本次 rebase,回到操作前状态
$ git rebase --abort
  1. 切换到 feature 分支,将其变基到 test 之上

    Terminal window
    $ git checkout feature
    $ git rebase test
    • 若出现冲突:解决冲突后执行 git rebase --continue
    • 若要放弃变基:执行 git rebase --abort
  2. 切回 test 分支,快进合并 feature

    Terminal window
    $ git checkout test
    $ git merge feature

    此时 feature 已在 test 之上,merge 为快进(fast-forward),不会生成合并节点。