编辑
2023-08-18
笔记
0

目录

从主仓库更新代码到 fork 仓库
从 github 中拉取变更同步到本地,并提交到 fork 仓库
配置 Git 提交时的邮箱和用户名
设置 HTTP/HTTPS 免密登录(git clone 第二次不需要再次输入密码了)
设置 HTTP 代理
线上Git仓库把 master 主分支改为了 main 主分支
我的一些全局 Git 配置
一些脚本
git-pull-all
git-pull-all-safes
git-pull-remote-favor
git-pull-auto-remote
一些错误的解决方案

从主仓库更新代码到 fork 仓库

shell
git remote -v git remote add github https://github.com/chenxiangfang/Main.git git fetch github git merge github/master git push

从 github 中拉取变更同步到本地,并提交到 fork 仓库

shell
git fetch github git merge github/master git push git fetch github --all git push origin --all

配置 Git 提交时的邮箱和用户名

shell
# 全局修改 git config --global user.email "XXX@XXX.com" git config --global user.name "cainiao" # 当前仓库修改 git config user.email "XXX@XXX.com" git config user.name "cainiao"

设置 HTTP/HTTPS 免密登录(git clone 第二次不需要再次输入密码了)

bash
git config --global credential.helper store

设置 HTTP 代理

bash
# 设置代理 git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port git config --global https.proxy https://proxyUsername:proxyPassword@proxy.server.com:port # 取消代理 git config --global --unset http.proxy

线上Git仓库把 master 主分支改为了 main 主分支

bash
# Switch to the "master" branch: git checkout master # Rename it to "main": git branch -m master main # Get the latest commits (and branches!) from the remote: git fetch # Remove the existing tracking connection with "origin/master": git branch --unset-upstream # Create a new tracking connection with the new "origin/main" branch: git branch -u origin/main

我的一些全局 Git 配置

shell
git config --global http.proxy socks5://127.0.0.1:1080 git config --global https.proxy socks5://127.0.0.1:1080 git config --global user.email "565499699@qq.com" git config --global user.name "Xiangfang Chen" git config --global core.autocrlf false git config --global core.filemode false git config --global core.preloadindex true git config --global core.fscache true git config --global branch.autosetuprebase always git config --global core.longpaths true

一些脚本

git-pull-all

  • git-pull-all.sh
bash
#!/bin/bash echo "Running git pull on all subdirectories..." # Find all .git directories recursively and cd to their parent to run git pull find . -name ".git" -type d | while read gitdir; do # Extract the parent directory of the .git folder workdir=$(dirname "$gitdir") echo "" echo "========================================" echo "Pulling latest changes in: $workdir" echo "========================================" # Change to the repository directory and pull (cd "$workdir" && git pull) done echo "" echo "========================================" echo "Finished pulling all repositories" echo "========================================"
  • git-pull-all.bat
@echo off echo Running git pull on all subdirectories... REM Loop through all subdirectories recursively for /d /r %%i in (*) do ( REM Check if the directory contains a .git folder if exist "%%i\.git" ( echo. echo ======================================== echo Pulling latest changes in: %%i echo ======================================== cd /d "%%i" git pull ) ) echo. echo ======================================== echo Finished pulling all repositories echo ======================================== pause

git-pull-all-safes

  • git-pull-all-safe.sh
bash
#!/bin/bash echo "Running git pull on all subdirectories..." # Find all .git directories recursively and cd to their parent to run git pull find . -name ".git" -type d | while read gitdir; do # Extract the parent directory of the .git folder workdir=$(dirname "$gitdir") echo "" echo "========================================" echo "Checking repository: $workdir" echo "========================================" # Change to the repository directory ( cd "$workdir" # Check if there are uncommitted changes if ! git diff-index --quiet HEAD -- || [ -n "$(git ls-files --exclude-standard --others)" ]; then echo "Warning: Uncommitted changes detected in $workdir" echo "Skipping git pull for this repository" echo "To manually pull, run: cd $workdir && git pull" else echo "Pulling latest changes in: $workdir" git pull fi ) done echo "" echo "========================================" echo "Finished checking all repositories" echo "========================================"
  • git-pull-all-safe.bat
bat
@echo off echo Running git pull on all subdirectories... REM Loop through all subdirectories recursively for /d /r %%i in (*) do ( REM Check if the directory contains a .git folder if exist "%%i\.git" ( echo. echo ======================================== echo Checking repository: %%i echo ======================================== cd /d "%%i" REM Check if there are uncommitted changes git diff-index --quiet HEAD -- >nul 2>&1 if errorlevel 1 ( echo Warning: Uncommitted changes detected in %%i echo Skipping git pull for this repository echo To manually pull, run: cd %%i && git pull ) else ( REM Check for untracked files git ls-files --exclude-standard --others | findstr . >nul 2>&1 if errorlevel 1 ( echo Pulling latest changes in: %%i git pull ) else ( echo Warning: Untracked files detected in %%i echo Skipping git pull for this repository echo To manually pull, run: cd %%i && git pull ) ) ) ) echo. echo ======================================== echo Finished checking all repositories echo ======================================== pause

git-pull-remote-favor

  • git-pull-remote-favor.sh
bash
#!/bin/bash echo "Running git pull on all subdirectories (favoring remote changes)..." # Find all .git directories recursively and cd to their parent to run git pull find . -name ".git" -type d | while read gitdir; do # Extract the parent directory of the .git folder workdir=$(dirname "$gitdir") echo "" echo "========================================" echo "Processing repository: $workdir" echo "========================================" # Change to the repository directory ( cd "$workdir" # Check if there are any changes to stash if ! git diff-index --quiet HEAD -- || [ -n "$(git ls-files --exclude-standard --others)" ]; then echo "Uncommitted changes detected, stashing..." git stash push -m "Auto stash before git pull ($(date))" stashed=true else stashed=false fi # Perform git pull echo "Pulling latest changes in: $workdir" git pull # Restore stashed changes if we stashed any, but favor remote changes on conflict if [ "$stashed" = true ]; then echo "Restoring stashed changes (favoring remote changes on conflict)..." if ! git stash pop; then echo "Conflict detected. Favoring remote changes and discarding local stash..." # Abort the merge conflict git merge --abort 2>/dev/null git rebase --abort 2>/dev/null # Drop the stash since we're discarding local changes git stash drop echo "Remote changes kept. Local stash discarded." else echo "Stashed changes successfully applied" fi fi ) done echo "" echo "========================================" echo "Finished processing all repositories" echo "========================================"
  • git-pull-remote-favor.bat
bat
@echo off echo Running git pull on all subdirectories (favoring remote changes)... REM Loop through all subdirectories recursively for /d /r %%i in (*) do ( REM Check if the directory contains a .git folder if exist "%%i\.git" ( echo. echo ======================================== echo Processing repository: %%i echo ======================================== cd /d "%%i" REM Check if there are any changes to stash git diff-index --quiet HEAD -- >nul 2>&1 set stashed=false if errorlevel 1 ( echo Uncommitted changes detected, stashing... git stash push -m "Auto stash before git pull" set stashed=true ) else ( REM Check for untracked files git ls-files --exclude-standard --others | findstr . >nul 2>&1 if errorlevel 0 ( echo Uncommitted changes detected, stashing... git stash push -m "Auto stash before git pull" set stashed=true ) ) REM Perform git pull echo Pulling latest changes in: %%i git pull REM Restore stashed changes if we stashed any, but favor remote changes on conflict if "%stashed%"=="true" ( echo Restoring stashed changes (favoring remote changes on conflict)... git stash pop if errorlevel 1 ( echo Conflict detected. Favoring remote changes and discarding local stash... REM Abort any merge/rebase that might be in progress git merge --abort >nul 2>&1 git rebase --abort >nul 2>&1 REM Drop the stash since we're discarding local changes git stash drop echo Remote changes kept. Local stash discarded. ) else ( echo Stashed changes successfully applied ) ) ) ) echo. echo ======================================== echo Finished processing all repositories echo ======================================== pause

git-pull-auto-remote

  • git-pull-auto-remote.sh
bash
#!/bin/bash echo "Running git pull on all subdirectories (auto-resolving conflicts to favor remote)..." echo "WARNING: Local changes will be discarded where conflicts occur!" echo "警告:发生冲突时本地更改将被丢弃!" # Find all .git directories recursively and cd to their parent to run git pull find . -name ".git" -type d | while read gitdir; do # Extract the parent directory of the .git folder workdir=$(dirname "$gitdir") echo "" echo "========================================" echo "Processing repository: $workdir" echo "========================================" # Change to the repository directory ( cd "$workdir" # Check if there are any changes to stash if ! git diff-index --quiet HEAD -- || [ -n "$(git ls-files --exclude-standard --others)" ]; then echo "Uncommitted changes detected, stashing..." git stash push -m "Auto stash before git pull ($(date))" has_stash=1 else has_stash=0 fi # Perform git pull echo "Pulling latest changes in: $workdir" git pull # Restore stashed changes if we stashed any, but resolve conflicts in favor of remote if [ $has_stash -eq 1 ]; then echo "Restoring stashed changes..." if ! git stash pop; then echo "Conflicts detected. Resolving in favor of remote changes..." # Cancel any ongoing merge/rebase git merge --abort 2>/dev/null || true git rebase --abort 2>/dev/null || true # Reset the index and working tree to match the remote branch # This discards local changes and keeps remote ones git reset --hard HEAD echo "Repository reset to remote state. Local changes discarded." else echo "Stashed changes successfully applied" fi # Clean up the stash entry git stash drop 2>/dev/null || true fi ) done echo "" echo "========================================" echo "Finished processing all repositories" echo "========================================"
  • git-pull-auto-remote.bat
bat
@echo off echo Running git pull on all subdirectories (auto-resolving conflicts to favor remote)... echo WARNING: Local changes will be discarded where conflicts occur! echo "警告:发生冲突时本地更改将被丢弃!" REM Loop through all subdirectories recursively for /d /r %%i in (*) do ( REM Check if the directory contains a .git folder if exist "%%i\.git" ( echo. echo ======================================== echo Processing repository: %%i echo ======================================== cd /d "%%i" REM Check if there are any changes to stash git diff-index --quiet HEAD -- >nul 2>&1 set has_stash=0 if errorlevel 1 ( echo Uncommitted changes detected, stashing... git stash push -m "Auto stash before git pull" set has_stash=1 ) else ( REM Check for untracked files git ls-files --exclude-standard --others | findstr . >nul 2>&1 if errorlevel 0 ( echo Uncommitted changes detected, stashing... git stash push -m "Auto stash before git pull" set has_stash=1 ) ) REM Perform git pull echo Pulling latest changes in: %%i git pull REM Restore stashed changes if we stashed any, but resolve conflicts in favor of remote if "%has_stash%"=="1" ( echo Restoring stashed changes... git stash pop if errorlevel 1 ( echo Conflicts detected. Resolving in favor of remote changes... REM Cancel any ongoing merge/rebase git merge --abort >nul 2>&1 git rebase --abort >nul 2>&1 REM Reset the index and working tree to match the remote branch REM This discards local changes and keeps remote ones git reset --hard HEAD echo Repository reset to remote state. Local changes discarded. ) else ( echo Stashed changes successfully applied ) REM Clean up the stash entry git stash drop >nul 2>&1 ) ) ) echo. echo ======================================== echo Finished processing all repositories echo ======================================== pause

一些错误的解决方案

Git command returns fatal error: "detected dubious ownership" fatal: unsafe repository(XXXX is owned by someone else.)

shell
# windows: takeown /r /d y /f <PATH> # linux: chown -R username:group <PATH> # 或者忽略全部文件夹 git config --global --add safe.directory "*"

Filename too long.

shell
git config --global core.longpaths true

本文作者:菜鸟

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 许可协议。转载请注明出处!