非Git项目合并Git项目及分支创建与合并操作咨询
Hey there! Let's tackle your two Git questions one by one—first the basic branch creation, then your specific merge scenario.
Creating a new branch from an existing local project is straightforward, with a few flexible options depending on your workflow:
Create a branch but stay on your current branch:
Rungit branch <your-new-branch-name>— this copies your current branch's state into the new one, but you’ll remain on the original branch to keep working.Create and switch to the new branch in one step (the most common go-to):
Use the classic command:git checkout -b <your-new-branch-name>
Or the more modern, intuitive version (Git 2.23+):git switch -c <your-new-branch-name>Create a branch from a specific existing branch (e.g., master/main, not your current working branch):
First switch to the target branch:git switch master
Then create your new branch:git switch -c <your-new-branch-name>
Or do it all in one line:git switch -c <your-new-branch-name> master
Pro tip: It’s always a good idea to run git pull first to make sure your base branch is up-to-date with the remote, so your new branch starts with the latest code.
Let’s break down what’s going on with your current setup, then fix the pull issue and get your branches merged properly.
The core problem here is: Your local master branch has an initial commit you made (Initial), but it shares no common history with the remote master branch (since you started with a brand-new local repo). Git can’t automatically merge these two unrelated histories, which is why your pull attempt failed.
Here’s the step-by-step fix to keep both the remote master and your local changes, merging them cleanly into newbranch:
Confirm you’re on
newbranch:
Rungit switch newbranchto make sure you’re working in the right branch.Pull the remote master into a temporary local branch:
Instead of trying to pull directly into your existing branches, fetch the remote master into a separate branch to avoid messing up your current work:git fetch origin master:remote-masterThis creates a local branch named
remote-masterthat’s an exact copy of the remote’s master. You can check its contents withgit switch remote-masterif you want, then switch back tonewbranchwhen you’re done.Merge the remote master into your
newbranch:
Since these two branches have unrelated histories, you need to explicitly tell Git to allow the merge with the--allow-unrelated-historiesflag:git merge remote-master --allow-unrelated-historiesResolve merge conflicts (if any):
Git will flag files where the remote master and your local changes overlap. Open those files, fix the conflicting sections, then stage the resolved files:git add <conflicted-file-name>Once all conflicts are fixed, finish the merge with:
git commitGit will auto-generate a merge commit message, but you can edit it to add context if you’d like.
Optional: Sync your local master with the remote:
If you want your localmasterto match the remote’s master (since your initial local master commit is already innewbranch), switch to master and reset it to the remote version:git switch master git reset --hard origin/master⚠️ Warning: This will completely overwrite your local master branch with the remote’s version—only do this if you don’t need the initial
Initialcommit in master anymore.Push your merged
newbranchto the remote:
Once everything looks good, push yournewbranchto the remote so others can access it:git push origin newbranch
That should get you sorted out! You’ll have your local changes combined with the remote project’s code in newbranch, while keeping the remote master intact.
内容的提问来源于stack exchange,提问作者Pavel Zorin

