You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Git分支总是引入其他分支文件的问题及多分支目录配置求助

Fix: Git Branches Including Files From Other Branches

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:

  1. Create an empty initial master branch (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 master
    
  2. Fix the broken BBB branch:

    # 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 BBB
    
  3. Repeat the process for the CCC branch:

    git checkout master
    git checkout -b CCC
    git add CCC
    git commit -m "Created CCC branch with only CCC directory"
    git push origin CCC
    
  4. Populate the master branch 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:

  1. 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 master
    
  2. Create 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:13:08