Git远程仓库

远程仓库的基本操作

  • git clone <url> 克隆仓库 image-20191120195935418
  • 克隆后默认把远程仓库的名字改为origin,且本地分支默认跟踪对应的远程分支
  • git remote 查看远程仓库名称列表
  • git remote -v 查看带上URL的远程仓库信息
  • git remote show <remote> 查看remote远程仓库的详细信息
  • git remote add <remote> <url> 增加远程仓库
  • git remote rename <old-remote-name> <new-remote-name> 重命名远程仓库
  • git remote rm <remote> 删除远程仓库

fetch与pull

  • git fetch <remote> 获取remote远程仓库的更新信息
  • git fetch --all 获取所有远程仓库的更新信息 fetch前 image-20191120200023526 fetch后 image-20191125111458726 fetch多个远程仓库 image-20191120200538659
  • git pull 拉取跟踪分支的更新信息,然后自动合并,相当于fetch + merge
  • git pull <remote> <branch> 拉取remote远程仓库的branch分支
  • git pull <url> <branch> 只拉取一次,不把url加入远程仓库列表
  • git push git pull命令,远程仓库默认名称是origin,远程仓库的名字为origin,可以省略

远程分支

  • 本地的远程分支以(remote)/(branch)命名
  • 本地的远程分支不可编辑
  • git checkout -b <branch> <remote>/<branch> 以远程分支为起点新建本地分支
  • git checkout --track <remote-name>/<remote-branch> 以远程分支为起点新建本地分支并设置好跟踪分支
  • git checkout --track <local-branch> <remote-name>/<remote-branch> 本地分支设置不同的分支名
  • git branch -u <remote>/<branch>git branch --set-upstream-to <remote>/<branch> 设置当前分支的跟踪分支
  • @{upstream}@{u} 跟踪分支的快捷方式
  • git branch -vv 查看包含跟踪分支的分支信息

push

  • git push <remote> <branch> 当前分支推送更新到remote远程仓库的branch分支
  • git push <remote> <local-branch> <remote-branch> 本地local-branch分支推送到remote远程仓库的remote-branch分支
  • git push <remote> --delete <remote-branch> 删除远程分支
  • 执行git push 假如远程仓库有新的提交,则push操作会被拒绝,必须先pull远程仓库的更新内容
  • 假如pull拉取下来的更新内容与本地仓库修改的内容有冲突,则需要手动修改合并冲突,处理完再push