Pre
常用命令
#查看远程配置 git remote [-v] #添加远程地址 git remote add origin http:xxx.xxx #删除远程地址 git remote remove origin #上传新分支至远程 git push --set-upstream origin master #将本地分支与远程建立关联 git branch --track --set-upstream-to=origin/test test
演示
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote -v #查看远程配置 origin https://gitee.com/yangshangwei/gitProject.git (fetch) origin https://gitee.com/yangshangwei/gitProject.git (push) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote add test https://gitee.com/yangshagnwei/test.git #添加远程地址 artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote -v #添加远程地址 origin https://gitee.com/yangshangwei/gitProject.git (fetch) origin https://gitee.com/yangshangwei/gitProject.git (push) test https://gitee.com/yangshagnwei/test.git (fetch) test https://gitee.com/yangshagnwei/test.git (push) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote remove test #删除远程地址 artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote -v #查看远程配置 origin https://gitee.com/yangshangwei/gitProject.git (fetch) origin https://gitee.com/yangshangwei/gitProject.git (push) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $
多个远程仓库演示
第一个仓库
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote -vv # 查看远程配置 origin https://gitee.com/yangshangwei/gitProject.git (fetch) origin https://gitee.com/yangshangwei/gitProject.git (push) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ mkdir -p src/main/java/com/artisan # 新建目录 artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ echo 'package com.artisan' > src/main/java/com/artisan/Hello.java # 创建文件 artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git status # 查看本地状态 On branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) src/ nothing added to commit but untracked files present (use "git add" to track) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git add -A ;git commit -am '7 commit' ; git push # local cache --> local repositroy ----> remote repository warning: LF will be replaced by CRLF in src/main/java/com/artisan/Hello.java. The file will have its original line endings in your working directory [master b685db4] 7 commit 1 file changed, 1 insertion(+) create mode 100644 src/main/java/com/artisan/Hello.java Enumerating objects: 9, done. Counting objects: 100% (9/9), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (8/8), 502 bytes | 502.00 KiB/s, done. Total 8 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/yangshangwei/gitProject.git 9f302ff..b685db4 master -> master artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git branch -avv # 查看分支映射 master -----------> origin/master * master b685db4 [origin/master] 7 commit remotes/origin/master b685db4 7 commit
好了,提交到 https://gitee.com/yangshangwei/gitProject.git 分支了,我们去gitee上查看一把
第二个仓库
新建 仓库 gitProject2
首先我们先新建个仓库 gitProject2
上传至远程新仓库
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote add origin2 https://gitee.com/yangshangwei/gitProject2.git # 添加远程仓库 gitProject2 artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote # 查看 origin origin2 artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git remote -vv # 查看详情 origin https://gitee.com/yangshangwei/gitProject.git (fetch) origin https://gitee.com/yangshangwei/gitProject.git (push) origin2 https://gitee.com/yangshangwei/gitProject2.git (fetch) origin2 https://gitee.com/yangshangwei/gitProject2.git (push) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git push --set-upstream origin2 master # 上传新分支至远程 Enumerating objects: 20, done. Counting objects: 100% (20/20), done. Delta compression using up to 12 threads Compressing objects: 100% (8/8), done. Writing objects: 100% (20/20), 1.46 KiB | 749.00 KiB/s, done. Total 20 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/yangshangwei/gitProject2.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin2'. artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $
执行完 git push --set-upstream origin2 master
后 ,后面再 git push 其实是上传到了 origin2 分支,可以看下
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git branch -avv * master b685db4 [origin2/master] 7 commit remotes/origin/master b685db4 7 commit remotes/origin2/master b685db4 7 commit
可以发下 master 对应的分支 为 origin2/master 。
orgin2 对应了我们的 git remote add origin2 https://gitee.com/yangshangwei/gitProject2.git gitProject2 项目 。
我们来验证下
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ pwd /d/opt/gitdemo/gitProject artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ echo 'artisan git git git ' > README.MF artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git push Everything up-to-date artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git status On branch master Your branch is up to date with 'origin2/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: README.MF no changes added to commit (use "git add" and/or "git commit -a") artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git add -A ; warning: LF will be replaced by CRLF in README.MF. The file will have its original line endings in your working directory artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git commit -am '8 commit'; [master 36a0f02] 8 commit 1 file changed, 1 insertion(+), 4 deletions(-) artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git push Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/yangshangwei/gitProject2.git b685db4..36a0f02 master -> master artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $
上传至远程原仓库
那如果要 push 到 原来的 origin 这个远程地址呢 ,执行 git push origin
指定分支名即可。
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $ git push origin Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 12 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 284 bytes | 284.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] To https://gitee.com/yangshangwei/gitProject.git b685db4..36a0f02 master -> master artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master) $
当然了在IDEA中的操作就简单了
选择就可以了。