Git

git 本地仓库添加和修改远程仓库

Posted by KalosAner on August 1, 2025

问题:

本地已经有了一个项目,想上传到远程仓库。

解决:

1
2
3
4
5
6
7
8
9
10
11
12
13
git init  # 初始化本地仓库
# 添加远程仓库
git remote add origin https://github.com/user/project.git
git push -u origin main  # 首次推送并关联分支

# 从 HTTPS 切换到 SSH
git remote set-url origin git@github.com:user/project.git

# 旧仓库:https://github.com/user/old-repo.git
# 新仓库:https://github.com/user/new-repo.git
git remote set-url origin https://github.com/user/new-repo.git
# 修改远程 URL 后,本地分支需重新关联远程分支
git branch --set-upstream-to=origin/main main

其他:

其他可能用到的 git 指令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 1. 添加远程仓库
git remote add <远程仓库名称> <远程仓库URL>
git remote add origin https://github.com/username/repo.git

# 2. 修改远程仓库的 URL
git remote set-url <远程仓库名称> <新URL>
git remote set-url origin https://github.com/username/new-repo.git

# 3. 查看已配置的远程仓库
git remote -v  # 查看所有远程仓库及其URL
git remote show origin  # 查看指定远程仓库的详细信息

# 4. 重命名远程仓库
git remote rename <旧名称> <新名称>
git remote rename origin upstream  # 将 origin 改为 upstream

# 5. 删除远程仓库
git remote remove <远程仓库名称>
git remote remove origin  # 删除名为 origin 的远程仓库