Windows上的git重命名问题

3

我将一个目录从tools重命名为Tools并提交。此后,我的远程仓库中有两个目录。由于某种原因,git没有删除旧的tools目录。如何避免这种情况或解决当前问题?


git add --all 包括删除和创建。 - TheBuzzSaw
2个回答

3
您没有告知git您已重命名一个目录,因此git将您的Tools目录视为全新的目录,并将其添加到版本控制中。
  • To fix the issue, you could ask git to delete the older directory named tools:

    git rm -r tools
    git commit
    
  • An alternative would be git add -u, which checks the current state of your working directory and updates the staging area to reflect this state. So git would become aware of any directories that were removed behind its back.

    # From the root of your repo
    git add -u .
    git commit
    
  • And for the future, use git mv to perform the rename, instead of performing the rename directly in the Shell or Command prompt. Although git is smart enough to track the move if it finds a significant similarity between deletions and additions, I prefer informing git explicitly about the move operation.

    git mv tools Tools
    git commit
    

我无法执行 git rm 命令,因为我没有这个目录。我使用的是 Windows 操作系统(所以文件系统不区分大小写)。git add -u 命令也没有任何作用。再次尝试使用 git mv 命令也不起作用,因为我没有这个目录。 - user14416

0
如果:
  • 你没有做任何其他提交
  • 没有人从您的远程存储库克隆或拉取

我建议你:

  • 重置到先前的提交(其中包含工具),git reset --hard HEAD^(确保您没有任何进行中的工作,或者先将其隐藏)
  • 正确地重命名您的目录:git mv tools Tools
  • 强制推送:git push --force

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接