请求协助完善Git脚本:将所有分支的文件夹合并至master分支
Hey there! Let's build this script to pull all folders from every branch into your master branch—here's a safe, practical approach with clear explanations.
First, let's clarify the goal: I'll cover two common scenarios. The first organizes each branch's content into a dedicated folder in master (to avoid overwriting files), and the second merges everything directly into master's root (note: this will likely cause conflicts you'll need to resolve manually).
Scenario 1: Organize branch content into dedicated folders (safe, no conflicts)
This script exports each branch's files into a folder named after the branch in your master branch:
#!/usr/bin/env bash # Start on an up-to-date master branch git checkout master git pull origin master # Loop through all local branches (using short names like "feature/payment" instead of full refs) for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do # Skip master itself to avoid redundant work if [ "$branch" = "master" ]; then continue fi echo "Processing branch: $branch" # Create a temporary directory to hold the branch's content temp_dir=$(mktemp -d) # Export the branch's files to the temp dir (no need to switch branches!) git archive "$branch" | tar -x -C "$temp_dir" # Create a target folder in master named after the branch target_dir="./$branch" mkdir -p "$target_dir" # Copy all files from the temp dir to the target folder cp -r "$temp_dir"/* "$target_dir"/ # Clean up the temporary directory rm -rf "$temp_dir" # Stage the new folder for commit git add "$target_dir" done # Commit all the organized branch content git commit -m "Consolidate all branch contents into master (organized by branch name)" # Uncomment below to push to your remote repo if needed # git push origin master
Scenario 2: Merge all branch content directly into master's root (risk of conflicts)
If you want to merge files directly into master's root (not separated by folders), use this script. Note: Conflicts are almost guaranteed, so the script will pause for you to resolve them manually:
#!/usr/bin/env bash # Start on an up-to-date master branch git checkout master git pull origin master # Loop through all local branches for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do if [ "$branch" = "master" ]; then continue fi echo "Merging branch: $branch into master" # Attempt to merge the branch -- use --allow-unrelated-histories if branches have no common ancestor git merge "$branch" --no-ff --allow-unrelated-histories # Check if merge caused conflicts if [ $? -ne 0 ]; then echo "⚠️ Conflict detected merging $branch! Resolve conflicts manually, then run 'git commit' to finish." echo "Press any key to continue after resolving..." read -n 1 -s fi done # Push merged changes to remote (uncomment if needed) # git push origin master
Important Notes:
- Backup first! Before running any script, clone your repo to a backup location to avoid losing data:
git clone your-repo-url backup-repo - If you need to include remote branches, run
git fetch --allfirst, then modify the loop to userefs/remotes/origin/instead ofrefs/heads/(and strip theorigin/prefix from branch names with something likebranch=${branch#origin/}) - The first scenario is ideal if you just want to archive all branch content without conflicts. The second is for when you need to combine codebase changes across branches.
内容的提问来源于stack exchange,提问作者Raya Levinson

