git-remote

发布时间 2023-07-13 15:24:21作者: lxd670

1.查看远程仓库

-v会显示需要读写远程仓库使用的 Git 保存的简写与其对应的URL

关联单个

显示关联别名

git remote
origin

显示远程url

git remote -v
origin	https://github.com/schacon/ticgit (fetch)
origin	https://github.com/schacon/ticgit (push)

关联多个

git remote -v
bakkdoor  https://github.com/bakkdoor/grit (fetch)
bakkdoor  https://github.com/bakkdoor/grit (push)
cho45     https://github.com/cho45/grit (fetch)
cho45     https://github.com/cho45/grit (push)
defunkt   https://github.com/defunkt/grit (fetch)
defunkt   https://github.com/defunkt/grit (push)
koke      git://github.com/koke/grit.git (fetch)
koke      git://github.com/koke/grit.git (push)
origin    git@github.com:mojombo/grit.git (fetch)
origin    git@github.com:mojombo/grit.git (push)

查看某个远程仓库

git remote show <remote_name>

  • Remote branches:表示远程分支
  • Local branch configured for 'git pull':表示本地关联远程的分支
  • Local ref configured for 'git push'
git remote show origin 
* remote origin
  Fetch URL: git@gitlab.xxxx.com:bbb/test1.git
  Push  URL: git@gitlab.xxxx.com:bbb/test1.git
  HEAD branch: master
  Remote branches:
    dev2   tracked
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

2.添加远程仓库

git remote add <remote_name> <url>添加一个新的远程 Git 仓库

查看关联的仓库

git remote
origin

添加一个远程仓库

# test1是remote_name
git remote add test1 git@gitlab.xxxx.com:bbb/test1.git

查看关联的仓库及url

git remote -v
origin  git@gitlab.xxxx.com:aaa/hello.git (fetch)
origin  git@gitlab.xxxx.com:aaa/hello.git (push)
test1   git@gitlab.xxxx.com:bbb/test1.git (fetch)
test1   git@gitlab.xxxx.com:bbb/test1.git (push)

3.重命名远程仓库

git remote rename <ole_remote_name> <new_remote_name>

4.更换URL

git remote set-url <remote_name> <new_url>

4.删除远程仓库

git remote remove <remote_name>

5.用法

5-1.获取对应url

git remote get-url <remote_name>

5-1-1.查询已有的remote

git remote get-url test1
git@gitlab.xxxx.com:bbb/test1.git

5-1-2.查询没有的remote

git remote get-url test1
error: No such remote 'test11'

5-3.从远程仓库中抓取与拉取

git fetch <remote_name>拉取指定remote_name

mkdir test1
cd test1
git init
git branch -m master
git remote add test1 git@gitlab.xxxx.com:bbb/test1.git
git fetch test1
remote: Enumerating objects: 4725, done.
remote: Counting objects: 100% (4725/4725), done.
remote: Total 4725 (delta 823), reused 4725 (delta 823), pack-reused 0
Receiving objects: 100% (4725/4725), 3.78 MiB | 11.10 MiB/s, done.
Resolving deltas: 100% (823/823), done.
From git@gitlab.xxxx.com:bbb/test1.git
 * [new branch]      dev2       -> test1/dev2
 * [new branch]      master     -> test1/master
git merge test1/master
git br -vv
* t1 e755491 [xxx] update xxx

5-3.推送到远程仓库

# 把当前分支推送到远程同名分支上
git push <remote_name> 

# 指定本地分支推送到远程同名分支上
git push <remote_name> <branch>

# 指定本地分支推送到远程指定分支上(远程没有回创建新分支)
git push <remote_name> <branch>[:<remote_branch>]

推送到test1仓库

把本地master分支推送远程origin的master分支

等于git push test1 master:master

git push test1 master
Enumerating objects: 4725, done.
Counting objects: 100% (4725/4725), done.
Delta compression using up to 96 threads
Compressing objects: 100% (4129/4129), done.
Writing objects: 100% (4725/4725), 3.78 MiB | 16.14 MiB/s, done.
Total 4725 (delta 823), reused 4375 (delta 527), pack-reused 0
remote: Resolving deltas: 100% (823/823), done.
remote: Create a Code Review for  master by visiting:
remote: 
remote:        https://code.xxxx.com:bbb/test1/xxx/new?from=master&to=master
remote: 
To gitlab.xxxx.com:bbb/test1.git
 * [new branch]      master -> master