Git问题:main分支版本回退,如何同步至目标分支状态?
Git分支同步与误提交修复方案
问题描述
我已将所有变更推送至开发分支,拉取后合并到main分支,随后误将xcuserdata/文件推送到main分支。切换到我的开发分支时,我暂存了xcuserdata文件,当前分支已与origin/branch保持同步,但切换回main分支时发现代码回退到了旧版本。请问如何将main分支同步至当前开发分支的状态,确保分支的变更能在main分支中显示?
Git日志信息
日志1:main、origin/main与xcuserstate文件的提交历史
% git log --oneline --graph --decorate main origin/main r2nr/r2nr.xcodeproj/project.xcworkspace/xcuserdata/vhrao.xcuserdatad/UserInterfaceState.xcuserstate * d42e9da (origin/main, main) Ignore some files * 429d79a coredata to cloudkit works across two phones and two different cloud accounts but still exports data from one user to other user * f737f3b Reset CloudKit dev environment and code to successfully sync and export Core data to CloudKit * 34354cd crated Coredata entity LocationInfra and code to save it along with Location * 3694149 Added Core Data to CloudKit synchronization for public database functionality |\ | * d9922fc Added Core Data to CloudKit synchronization for public database functionality * | fd94fe0 added Firebase API message notification code in AppDelegate |/ * 290e343 Removed .DS_Store from all directories * 1d529fd added code to track and get a list of apartment units at a given location * a328e0a merged to main |\ | * 0307a07 fixed bugs in add location and calculator | * 38dc5c4 xcuserstate | * 4461edc corrections to stage characters in source code | * 22cbcb3 xcode files * | 6c9d4cf xcuserstate * | 5b30dbf xcuserstate * | ccb9ae3 changes |/ * cd7e27b Add location - If address is outside Texas then API will be called with user input * 27faa5b fixed add location- verified creating iCloud records and linking provider to location * 5f7c041 Fixed ESID lookup in Add Location * 0284d6d fixed bugs in location ID while adding load or capacity via LoadAndCapacityTableViewController, after selecting a row from end point profile * f90bb38 uncommented code in profilemapviewcontroller * 2416a04 uncommented code for Details button on profilemapviewcontroller * ec34a1c Added apartment option to household * 6d8ac7e second commit of full code
日志2:main、origin/main与断点文件的提交历史
git log --oneline --graph --decorate main origin/main r2nr/r2nr.xcodeproj/xcuserdata/vhrao.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist * d42e9da (origin/main, main) Ignore some files * 429d79a coredata to cloudkit works across two phones and two different cloud accounts but still exports data from one user to other user * f737f3b Reset CloudKit dev environment and code to successfully sync and export Core data to CloudKit * 34354cd crated Coredata entity LocationInfra and code to save it along with Location * 3694149 Added Core Data to CloudKit synchronization for public database functionality |\ | * d9922fc Added Core Data to CloudKit synchronization for public database functionality * | fd94fe0 added Firebase API message notification code in AppDelegate |/ * 290e343 Removed .DS_Store from all directories * 1d529fd added code to track and get a list of apartment units at a given location * 0307a07 fixed bugs in add location and calculator * 22cbcb3 xcode files * cd7e27b Add location - If address is outside Texas then API will be called with user input * 27faa5b fixed add location- verified creating iCloud records and linking provider to location * 5f7c041 Fixed ESID lookup in Add Location * 0284d6d fixed bugs in location ID while adding load or capacity via LoadAndCapacityTableViewController, after selecting a row from end point profile * 2416a04 uncommented code for Details button on profilemapviewcontroller * ec34a1c Added apartment option to household * 6d8ac7e second commit of full code
日志3:main、origin/main与开发分支branch02_vr的提交历史
% git log --oneline --graph --decorate main origin/branch02_vr * d42e9da (origin/main, main) Ignore some files * dc617ba Merge pull request #4 from vihrao/branch02_vr |\ | | * 18202e1 (HEAD -> branch02_vr, origin/branch02_vr) Added Firebase Integration | |/ | * 2520ef3 Added Firebase Integration |/ * 429d79a coredata to cloudkit works across two phones and two different cloud accounts but still exports data from one user to other user * f737f3b Reset CloudKit dev environment and code to successfully sync and export Core data to CloudKit * 34354cd crated Coredata entity LocationInfra and code to save it along with Location * 3694149 Added Core Data to CloudKit synchronization for public database functionality |\ | * d9922fc Added Core Data to CloudKit synchronization for public database functionality * | fd94fe0 added Firebase API message notification code in AppDelegate |/ * 290e343 Removed .DS_Store from all directories * 1d529fd added code to track and get a list of apartment units at a given location * a328e0a merged to main |\ | * 0307a07 fixed bugs in add location and calculator | * 798f467 Added .gitignore file to contain xcuserdata/ dir that overlooks both UserInterfaceState.xcuserstate and Breakpoints_v2.xcbkptlist | * 38dc5c4 xcuserstate | * 4461edc corrections to stage characters in source code | * 22cbcb3 xcode files | * b48b81c removed junk charecters from code * | 6c9d4cf xcuserstate * | 5b30dbf xcuserstate * | 76f8244 corrections to stage characters in source code * | ccb9ae3 changes |/ * cd7e27b Add location - If address is outside Texas then API will be called with user input * 27faa5b fixed add location- verified creating iCloud records and linking provider to location * 5f7c041 Fixed ESID lookup in Add Location * 78daa78 fixed ESID look up in Add Location TableView Controller * 0284d6d fixed bugs in location ID while adding load or capacity via LoadAndCapacityTableViewController, after selecting a row from end point profile * dd5bfe5 (origin/branch091922_vr) Merge pull request #3 from vihrao/branch02_ed |\ | * d5e1879 (origin/branch02_ed) endPtSearch * | 4c77e77 Merge pull request #2 from vihrao/branch02_ed |\| | * 822fcf9 UI Changes * | 9baaa9c Merge pull request #1 from vihrao/branch02_ed |\| | * 9da5e89 ProviderPlanTVC |/ * f90bb38 uncommented code in profilemapviewcontroller * 2416a04 uncommented code for Details button on profilemapviewcontroller * ec34a1c Added apartment option to household * f3bffd9 (origin/branch01_vr) changes to README.md * 6d8ac7e second commit of full code * 8e9d5f1 first commit (END)
解决步骤
回到你的开发分支(如branch02_vr),确认本地与远程分支同步:
git checkout branch02_vr git pull origin branch02_vr git status确保输出显示
nothing to commit, working tree clean,说明分支状态正常。切换到main分支:
git checkout main将开发分支的最新代码合并到main分支:
git merge branch02_vr若出现冲突,打开冲突文件手动调整代码,解决后执行
git add <冲突文件名>,再git commit完成合并。清理main分支上误提交的xcuserdata文件:
确认.gitignore已添加xcuserdata/规则,然后执行:git rm -r --cached xcuserdata/ git commit -m "移除误提交的xcuserdata文件,更新忽略规则"将修复后的main分支推送到远程仓库:
git push origin main
操作完成后,main分支会与开发分支状态完全同步,同时清理了误提交的xcuserdata文件。
内容的提问来源于stack exchange,提问作者vrao
相关产品推荐
相关产品推荐

