如何从远程仓库中的文件夹名称中删除尾随空格?

4
我正在使用Windows电脑,尝试检出远程存储库中的最新提交。在最近的提交中,存储库所有者添加了一个以空格结尾的文件夹名称。由于我使用的是Windows电脑,这会阻止我使用git checkout main来检出最新的提交。当我尝试这样做时,无论是使用git checkout main还是git checkout --force main,它都只能完成一半就返回错误。虽然它没有将我切换到main分支,但它修改了一些(但不是全部)需要修改的文件,以便将我切换到main分支,这会污染我的工作目录。
我想要做的是重命名文件夹(在本问题中为foldername /),以删除尾随的空格(将其重命名为foldername/)。最大的问题是我无法检出任何包含foldername /的提交。
在设置了git config core.protectNTFS false之后,我尝试使用git checkout --force main来检出主分支,然后使用git mv "foldername /" "foldername/"将文件夹重命名为不包含空格的名称。但是这并不起作用,因为由于foldername /已经存在,所以我无法检出主分支。我尝试在检出main之前和之后使用git mv "foldername /" "foldername/"(分别在干净或污染的工作目录中)。这两种尝试都没有成功,因为工作目录中不存在"foldername /"文件夹。从我所看到的情况来看,我陷入了一个进退两难的境地:我必须先将文件夹放入工作目录中才能重命名它,但我需要在将其带入工作目录之前重命名该文件夹。
有没有办法让我在不将其带入工作目录的情况下重命名该文件夹?有没有办法检出提交,尽管文件夹名称以空格结尾?也许我错过了其他打破这个进退两难的方法吗?
1个回答

2
其中一种解决方案(假设foldername/在根目录下):
  1. 将commit的根树对象转储到文件。
  2. 编辑文件并删除尾随空格。
  3. 从更新后的文件创建新的树对象。
  4. 从新的根树对象创建一个新的commit。

第一步

# Dump the root tree object of the commit to a file.
git ls-tree main > temp_tree.txt

步骤2

# Edit the file and remove the trailing whitespace.
# temp_tree.txt has a line like this, where I use * to represent a space.
# The sha1 value is different in your case.
040000 tree 09a13b897d3d0f528d487c704da540cb952d7606    foldername*

# Remove the trailing space, save and quit.

步骤三

# Create a new tree object from the updated file.
git mktree < temp_tree.txt

# The command prints a sha1 value which is the new tree object
# Suppose it's f060989b137790b66267c165da36bf829275b88f

步骤4

# Create a new commit from the new root tree object.
# Here we need to specify a parent for the new commit.
# Two of the candidates would be the parent of the current main head,
# or right the head. It depends on what you plan to do with the new commit,
# replacing the original head or creating a new commit based on the head.
# Suppose you plan to create a new commit base on the head.
# If you know the commit sha1 value of origin/main, use the sha1 value instead
# as origin/main could be updated unexpectedly.
git commit-tree -p origin/main -m "commit message" f060989b137790b66267c165da36bf829275b88f

# git commit-tree creates a new commit and prints its sha1 value.
# Suppose the new commit is 7d94856b1b5b650a085c43f0f5e28b3aa7f70f02
# We can update main to the new commit
git update refs/remotes/origin/main 7d94856b1b5b650a085c43f0f5e28b3aa7f70f02

然后你可以尝试 git checkout main 或者 git checkout 7d94856b1b5b650a085c43f0f5e28b3aa7f70f02

如果 foldername / 在递归文件夹中很深,那就有点麻烦了。但解决方案是一样的。假设它在 foo/foldername /

在第一步中,我们需要获取 foldername / 的父目录 foo 的树对象,而不是根目录树。

git ls-tree origin/main:foo > temp_tree.txt

步骤2和步骤3是相同的。我们编辑树文件并删除尾随空格。之后,我们为foo创建一个新的树对象。

在最终创建新提交之前,我们需要生成一个新的根树,因为根树中foo的sha1值已更新,我们需要用新值替换原始的sha1值。

同样地,我们将根树对象转储到文件中。

git ls-tree origin/main > temp_tree.txt
# There is a line like this
040000 tree 09a13b897d3d0f528d487c704da540cb952d7606    foo

这次,我们将sha1value更改为步骤3中生成的新树,并保存并退出。然后,我们从更新后的temp_tree.txt创建新的根树。得到根树对象后,我们可以像在上一步骤4中那样再次创建新的提交。
如果深度更深,我们需要为每个父文件夹向上创建一个新的树对象,直到创建新的根树。
可能还有其他更简单的解决方案。这是我尝试解决类似问题(修改已损坏的存储条目)时使用的方法。在您的情况下,我认为在支持尾随空格的计算机上修改文件夹名称会更容易些。

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