Batch update local Git repository and legacy permission issues

The projects within the team have dependencies on each other. Due to historical reasons, submodules were not used to manage these dependencies. As a result, developers need to manually update the repository code sequentially during daily development; otherwise, they may encounter various strange issues.

Referencing online resources, the structures are generally similar. Maintain a local repository directory manually: git_list.txt. Write a script to traverse the directory and perform an update in one go. Before starting work, run the script first.

linux

create new file: batch_pull.sh

#!/bin/bash

echo "============ 更新仓库 ==================="

# 检查 git_list.txt 是否存在
if [ ! -f "git_list.txt" ]; then
  echo "git_list.txt 文件不存在!请创建并添加要拉取的 git 仓库 URL。"
  exit 1
else
  echo "============ 检测到了 git 仓库清单文件 ===="
fi

# 逐行读取 git_list.txt 中的 URL,并执行拉取操作
while read -r url; do
  if [ -d "$url" ]; then
    cd "$url" || continue
    git pull
    cd ..
    echo "Pull $url 完成!"
    echo "========================================"
  else
    echo "目录 $url 不存在,跳过拉取。"
  fi
done < "git_list.txt"

windows

create a new file: batch_pull.bat

@echo off
chcp 65001 > nul
rem 进入脚本所在目录
cd /d "%~dp0"

rem 检查 git_list.txt 是否存在
if not exist "git_list.txt" (
  echo git_list.txt 文件不存在!请创建并添加要拉取的 git 仓库 URL。
  exit /b 1
) else (
  echo ============ 检测到了 git 仓库清单文件 ====
)

rem 逐行读取 git_list.txt 中的 URL,并执行拉取操作
for /f %%i in (git_list.txt) do (
  if exist "%%i" (
    pushd "%%i"
    git pull
    popd
    echo Pull %%i 完成!
    echo ========================================
  ) else (
    echo 目录 %%i 不存在,跳过拉取。
  )
)

Historical legacies

Handling the git folder permission files after reinstalling the system: Fatal error “unsafe repository (’/home/repon’ is owned by someone else)”

Most of the suggested practices online come from stack overflow

  1. Add trusted directory: git config --global --add safe.directory /home/repon
  2. Manually modify the .gitconfig configuration file and specify the directory to be trusted
[safe]
    directory = /home/repon

After handling it in the above manner, the warehouse update is normal, but there are a lot of warning messages in the console when executing git pull, indicating an owner error

Reinstalling the system on a desktop computer

The development machine hasn’t been reinstalled in a long time, and the system disk is full of junk files. There was nothing I could do, so I took the time to reinstall the system. I encountered this permission issue again, and previous scripts won’t run because the permissions are incomplete.

Use the new scheme, directly add *, and git will automatically trust all directories

git config --global --add safe.directory "*"

I’m not sure if it’s a user permissions issue, or whether people aren’t used to the fact that the windows platform also has commands similar to chown. You can modify the folder owner, of course. If you don’t have many directories, manually modifying the owners is fine. However, this work computer has domain information added, and I’m not sure if it’s an anomaly with the company’s deployed domain or a local system setting issue. The user used for login couldn’t be found in the user list, so I ultimately handled it through the command line.

Administrator privileges, execute the powershell script change_ower.ps1. Remember to adjust the script file encoding to gbk, for Chinese operating systems, otherwise it will display garbled characters.

# 获取当前用户的用户名
$currentUserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

# 设置 PowerShell 的字符编码为 UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 要更改所有者的根目录路径
$rootDirectory = "G:\workspace"  # 替换为实际的目录路径

# 递归遍历目录并更改文件和文件夹的所有者
Get-ChildItem -Path $rootDirectory -Recurse | ForEach-Object {
    $itemPath = $_.FullName

    # 检查是文件还是文件夹
    if ($_ -is [System.IO.DirectoryInfo]) {
        # 如果是文件夹,使用 icacls 更改所有者权限
        $icaclsResult = icacls $itemPath /setowner "$currentUserName" 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Host "已更改文件夹 $itemPath 的所有者为 $currentUserName"
        } else {
            Write-Host "无法更改文件夹 $itemPath 的所有者。错误信息: $icaclsResult"
        }
    } else {
        # 如果是文件,使用 icacls 更改所有者权限
        $takeownResult = icacls $itemPath /setowner "$currentUserName" 2>&1
        if ($LASTEXITCODE -eq 0) {
            # Write-Host "已更改文件 $itemPath 的所有者为 $currentUserName"
        } else {
            Write-Host "无法更改文件 $itemPath 的所有者。错误信息: $takeownResult"
        }
    }
}

As expected, something unexpected still happened. The Chinese information output by the script execution was garbled. I tried setting the console character encoding and adjusting the script encoding, but everything was still garbled. It felt like my brain wasn’t working properly. I then tried enabling the beta feature of Control Panel-Region-Language settings and globally enabled Unicode encoding. The script executed normally, but several development software stopped working correctly. Later, when reviewing the materials, I remembered to adjust the script file’s encoding to gbk.

Materials

Licensed under CC BY-NC-SA 4.0
Last updated on May 25, 2025 02:57
A financial IT programmer's tinkering and daily life musings
Built with Hugo
Theme Stack designed by Jimmy