git remote
git remote
命令管理一组跟踪的存储库。罗列、添加和删除远端仓库别名。
语法
git remote [-v | --verbose] git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url> git remote rename <old> <new> git remote remove <name> git remote set-head <name> (-a | --auto | -d | --delete | <branch>) git remote set-branches [--add] <name> <branch>… git remote get-url [--push] [--all] <name> git remote set-url [--push] <name> <newurl> [<oldurl>] git remote set-url --add [--push] <name> <newurl> git remote set-url --delete [--push] <name> <url> git remote [-v | --verbose] show [-n] <name>… git remote prune [-n | --dry-run] <name>… git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)…]
示例
列出远端别名
如果没有任何参数,Git 会列出它存储的远端仓库别名了事。默认情况下,如果你的项目是克隆的(与本地创建一个新的相反), Git 会自动将你的项目克隆自的仓库添加到列表中,并取名“origin”。如果你执行时加上-v
参数,你还可以看到每个别名的实际链接地址。
git remote origingit remote -v origin git@github.com:github/git-reference.git (fetch) origin git@github.com:github/git-reference.git (push)
在此你看到了该链接两次,是因为 Git 允许你为每个远端仓库添加不同的推送与获取的链接,以备你读写时希望使用不同的协议。
git clone http://git.oschina.net/yiibai/sample.gitcd sample # git remote 不带参数,列出已经存在的远程分支git remote origin # git remote -v | --verbose 列出详细信息git remote -v origin http://git.oschina.net/yiibai/sample.git (fetch) origin http://git.oschina.net/yiibai/sample.git (push) Administrator@MY-PC /D/worksp/sample (master)git remote --verbose origin http://git.oschina.net/yiibai/sample.git (fetch) origin http://git.oschina.net/yiibai/sample.git (push)
为你的项目添加一个新的远端仓库
如果你希望分享一个本地创建的仓库,或者你想要获取别人的仓库中的贡献——如果你想要以任何方式与一个新仓库沟通,最简单的方式通常就是把它添加为一个远端仓库。
git remote add [alias] [url]
此命令将[url]以[alias]的别名添加为本地的远端仓库。
添加一个新的远程,抓取,并从它检出一个分支
git remote add staging git://git.kernel.org/.../gregkh/staging.gitgit remote origin staginggit fetch staging ... From git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging * [new branch] master -> staging/master * [new branch] staging-linus -> staging/staging-linus * [new branch] staging-next -> staging/staging-nextgit branch -r origin/HEAD -> origin/master origin/master staging/master staging/staging-linus staging/staging-nextgit checkout -b staging staging/master ...
添添加远程仓库
git remote add pb http://git.oschina.net/yiibai/sample.gitgit remote -v origin http://git.oschina.net/yiibai/sample.git
pb http://git.oschina.net/yiibai/sample2.git现在可以用字串pb指代对应的仓库地址了。比如说,要抓取所有 Paul 有的,但本地仓库没有的信息,可以运行git fetch pb
:
git fetch pb remote: Counting objects: 58, done. remote: Compressing objects: 100% (41/41), done. remote: Total 44 (delta 24), reused 1 (delta 0) Unpacking objects: 100% (44/44), done. From http://git.oschina.net/yiibai/sample2.git * [new branch] master -> pb/master * [new branch] ticgit -> pb/ticgit
git remotegit remote add github git@github.com:schacon/hw.gitgit remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push)
像分支的命名一样,远端仓库的别名是强制的——就像“master”,没有特别意义,但它广为使用,因为 git init 默认用它;“origin”经常被用作远端仓库别名,就因为 git clone 默认用它作为克隆自的链接的别名。此例中,我决定给我的远端仓库取名“github”,但我叫它随便什么都可以。
删除现存的某个别名
如果你需要删除一个远端——不再需要它了、项目已经没了,等等:
git remote rm [alias]git remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push)git remote add origin git://github.com/pjhyett/hw.gitgit remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push) origin git://github.com/pjhyett/hw.git (fetch) origin git://github.com/pjhyett/hw.git (push)git remote rm origingit remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push)
简而言之你可以用git remote
列出你的远端仓库和那些仓库的链接。你可以使用git remote add
添加新的远端仓库,用git remote rm
删掉已存在的那些。