Git分支总是引入其他分支文件的问题及多分支目录配置求助
I see exactly what's going on here—when you ran git checkout -b BBB, you were still sitting on the AAA branch. Git branches inherit all the commit history and working directory files from the branch you're currently on, so your BBB branch started with the AAA directory already present. When you added and committed BBB, you accidentally included those existing AAA files right along with it.
Here's how to fix this properly, whether you need to clean up your current setup or start fresh with the correct workflow:
Option 1: Clean Up Your Existing Repository
First, let's establish a clean, empty master branch to use as the base for all other branches:
Create an empty initial
masterbranch (if you haven't already set one up):# Check out an orphaned branch with no prior commit history git checkout --orphan master # Remove all files from the working directory git rm -rf . # Create an empty initial commit to give master a valid starting point git commit --allow-empty -m "Initial empty commit" # Push this clean master to your remote repository git push origin masterFix the broken
BBBbranch:# Delete the faulty remote BBB branch git push origin --delete BBB # Delete the local broken BBB branch git branch -D BBB # Create a new BBB branch based on the clean master git checkout -b BBB master # Add only the BBB directory (no other files will be present now) git add BBB git commit -m "Created BBB branch with only BBB directory" # Push the corrected BBB branch to remote git push origin BBBRepeat the process for the
CCCbranch:git checkout master git checkout -b CCC git add CCC git commit -m "Created CCC branch with only CCC directory" git push origin CCCPopulate the
masterbranch with all directories:git checkout master # Add all three directories to the master branch git add AAA BBB CCC git commit -m "Master branch containing all directories" git push origin master
Option 2: Start Fresh (If You're Early in the Process)
If you haven't pushed too much to remote yet, you can reset your local repo and start over with the correct workflow:
Reset your local repo to an empty state:
# Create a temporary orphaned branch git checkout --orphan temp git rm -rf . git commit --allow-empty -m "Reset to empty initial state" # Delete existing problematic branches git branch -D master AAA # Rename the temp branch to master git branch -m master # Force-push the clean master to remote (only do this if no one else is using the repo!) git push -f origin masterCreate each directory branch from the clean master:
# Set up AAA branch git checkout -b AAA master git add AAA git commit -m "Created AAA branch" git push origin AAA # Set up BBB branch git checkout master git checkout -b BBB git add BBB git commit -m "Created BBB branch" git push origin BBB # Set up CCC branch git checkout master git checkout -b CCC git add CCC git commit -m "Created CCC branch" git push origin CCC # Populate master with all directories git checkout master git add AAA BBB CCC git commit -m "Master branch with all directories" git push origin master
Key Takeaway
The critical mistake was creating new branches from existing directory branches (like making BBB from AAA). Always create your directory-specific branches from the clean empty master branch—this ensures each branch only contains the files you intend to include in it.
内容的提问来源于stack exchange,提问作者minecraftplayer1234

