Git技术问答:如何将第二个本地仓库关联至已有GitHub仓库?
Hey there! It looks like you’ve already checked off the foundational steps—adding your Django files, committing changes, and setting up the remote origin with git remote add origin https://github.com/Frank-Mascarell/LibrosWeb.git. Let’s wrap this up smoothly to get your local code onto GitHub:
1. Double-Check Your Remote Connection First
You ran git remote -v already, but let’s confirm it’s pointing to the right place. Re-run the command to verify:
git remote -v
You should see output like this (confirm the URLs match your GitHub repo):
origin https://github.com/Frank-Mascarell/LibrosWeb.git (fetch) origin https://github.com/Frank-Mascarell/LibrosWeb.git (push)
If the URLs are wrong, fix them with this command:
git remote set-url origin https://github.com/Frank-Mascarell/LibrosWeb.git
2. Push Your Local Code (First-Time Setup)
Since this is the first time pushing this Django repo to your GitHub remote, you need to link your local branch to the remote branch. Here’s how to do it based on your branch name:
If your local branch is master (matches your earlier workflow)
Run this to push and set up upstream tracking (so future pushes only need git push):
git push -u origin master
If your GitHub repo uses the default main branch (common for newer repos)
If your GitHub repo’s default branch is main (check this in your repo’s settings on GitHub), you have two options:
- Rename your local branch to
mainfirst:git branch -m master main git push -u origin main - Or push your local
masterbranch directly to the remotemainbranch:git push -u origin master:main
3. Fix Push Failures (If Remote Has Existing Content)
If you get an error saying the remote has files you don’t have (like a README or .gitignore), you’ll need to pull that content first:
git pull origin <remote-branch-name>
For example, if the remote branch is master:
git pull origin master
This merges remote changes into your local repo. If there are conflicts, Git will flag them—open those files, resolve the differences, then run:
git add . git commit -m "Resolve merge conflicts with remote" git push -u origin master
Once you run the successful push, your Django 2.0 site files will be live on your GitHub repo!
内容的提问来源于stack exchange,提问作者Frank Mascarell

