3.1 删除远程分支
在 Git 中删除远程分支的方法如下:
git push [remote-name] --delete [branch-name]
或者
git push [remote-name] :[branch-name]
3.2 更改 Git 远程仓库 URL
查看现有远程仓库:
git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/usernam/repo.git (push)
更改仓库 URL:
git remote set-url origin https://github.com/username/repo2.git
# 更改 “origin” 远程仓库的 URL
验证新的远程仓库 URL:
git remote -v
# origin https://github.com/username/repo2.git (fetch)
# origin https://github.com/username/repo2.git (push)
3.3 列出现有远程仓库
列出与当前仓库关联的所有现有远程仓库:
git remote
详细列出与当前仓库关联的所有现有远程仓库,包括 fetch 和 push 的 URL:
git remote --verbose
或简单地:
git remote -v
3.4 删除本地已删除远程分支的副本
如果远程分支已被删除,你的本地仓库需要被指示删除对它的引用。
从特定远程中删除已删除的分支:
git fetch [remote-name] --prune
从所有远程中删除已删除的分支:
git fetch --all --prune
3.5 从上游仓库更新
假设你设置了上游仓库(如 “设置上游仓库” 中所述)
git fetch remote-name
git merge remote-name/branch-name
pull 命令结合了 fetch 和 merge 操作。
git pull
带有 --rebase 标志的 pull 命令结合了 fetch 和 rebase 操作,而不是 merge。
git pull --rebase remote-name branch-name
3.6 ls-remote
git ls-remote 是一个独特的命令,允许你在无需先克隆或获取的情况下查询远程仓库。
它将列出指定远程仓库的 refs/heads 和 refs/tags。
有时你会看到 refs/tags/v0.1.6 和 refs/tags/v0.1.6^{}:其中的 ^{} 用于列出被引用的注释标签(即该标签所指向的提交)
自 Git 2.8(2016 年 3 月)以来,你可以避免为标签出现双重条目,并直接列出这些被引用的标签:
git ls-remote --ref
它还可以帮助解决实际用于远程仓库的 URL,当你有 “url..insteadOf” 配置设置时。如果 git remote --get-url <aremotename> 返回 https://server.com/user/repo ,并且你设置了 git config url.ssh://git@server.com:.insteadOf https://server.com/:
git ls-remote --get-url <aremotename>
ssh://git@server.com:user/repo
3.7 添加新的远程仓库
git remote add upstream git-repository-url
将由 git-repository-url 表示的远程 Git 仓库作为名为 upstream 的新远程仓库添加到 Git 仓库中。
3.8 在新分支上设置上游
你可以使用以下命令创建新分支并切换到该分支:
git checkout -b AP-57
在使用 git checkout 创建新分支后,你需要使用以下命令将该上游设置为 origin,以便推送:
git push --set-upstream origin AP-57
之后,你可以在该分支上使用 git push。
3.9 入门
推送至远程分支的语法:
git push <remote_name> <branch_name>
示例:
git push origin master
3.10 重命名远程仓库
使用 git remote rename 命令重命名远程仓库。
git remote rename 命令接受两个参数:
一个现有的远程仓库名称,例如:origin
一个新的远程仓库名称,例如:destination
获取现有的远程仓库名称:
git remote
# origin
检查带有 URL 的现有远程仓库:
git remote -v
# origin https://github.com/username/repo.git (fetch)
# origin https://github.com/usernam/repo.git (push)
重命名远程仓库:
git remote rename origin destination
# 将远程仓库名称从 “origin” 更改为 “destination”
验证新的名称:
git remote -v
# destination https://github.com/username/repo.git (fetch)
# destination https://github.com/usernam/repo.git (push)
可能出现的错误
-
无法将配置部分 “remote.[旧名称]” 重命名为 “remote.[新名称]”
这个错误意味着你尝试的旧远程仓库名称(origin)不存在。
-
远程仓库 “[新名称]” 已存在。
错误信息一目了然。
3.11 显示特定远程仓库的信息
输出有关已知远程仓库(origin)的一些信息:
git remote show origin
仅打印远程仓库的 URL:
git config --get remote.origin.url
从 2.7 版本开始,也可以使用以下方法,这比上述使用 config 命令的方法更好:
git remote get-url origin
3.12 为特定远程仓库设置 URL
你可以通过以下命令更改现有远程仓库的 URL:
git remote set-url remote-name url
3.13 获取特定远程仓库的 URL
你可以使用以下命令获取现有远程仓库的 URL:
git remote get-url <name>
默认情况下,这将是:
git remote get-url origin
3.14 更改远程仓库
要更改你希望你的远程仓库指向的仓库的 URL,可以使用 set-url 选项,如下所示:
git remote set-url <remote_name> <remote_repository_url>
示例:
git remote set-url heroku https://git.heroku.com/fictional-remote-repository.git