git仓库初始化并添加到远程仓库
//初始化
git init
//添加远程仓库
git remote add origin <remote-address>
//提交到远程仓库master
git push -u origin master
其中第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,之后push就可以简化命令了。
远程git地址操作
//添加远程地址
bash-3.2$ git remote add origin https://github.com/hcy0411/testDubbo.git
//修改远程仓库简写 origin 修改成orig
bash-3.2$ git remote rename origin orig
// 查看远程仓库地址
bash-3.2$ git remote -v
origin https://github.com/hcy0411/testDubbo.git (fetch)
origin https://github.com/hcy0411/testDubbo.git (push)
// 删除远程仓库
bash-3.2$ git remote remove origin
git 新建远程分支
// 第一种首先在本地仓库建立分支,然后push到远程仓库
// checkout -b的作用是 branch加checkout操作,即建立并切换到分支
localhost:test-dubbo hanchunyang$ git checkout -b dev
Switched to a new branch 'dev'
// 查看分支
localhost:test-dubbo hanchunyang$ git branch
* dev
master
// 把本地的分支push到远程仓库分支
localhost:test-dubbo hanchunyang$ git push origin dev
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/hcy0411/testDubbo.git
* [new branch] dev -> dev
// 第二种直接在远程仓库建立分支
// 即把本地的master推送到远程的分支,如果远程没有分支就会创建
// git push origin <local_branch_name>:<remote_branch_name>
bash-3.2$ git push origin master:dev2
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/hcy0411/testDubbo.git
* [new branch] master -> dev2
删除分支
// 删除本地分支
bash-3.2$ git branch -d dev
Deleted branch dev (was 44394ff).
// 删除远程分支
// push空白到分支,相当于删除
bash-3.2$ git push origin :dev
To https://github.com/hcy0411/testDubbo.git
- [deleted] dev
git分支合并
// 第一步,分支提交
localhost:test-dubbo hanchunyang$ git commit -a -m "aa"
[dev 82677a3] aa
1 file changed, 2 insertions(+)
// 第二步,切换到master
localhost:test-dubbo hanchunyang$ git checkout master
Switched to branch 'master'
// 第三步,在主干上合并分支
localhost:test-dubbo hanchunyang$ git merge dev
Updating 44394ff..82677a3
Fast-forward
test-customer/src/main/java/test/dubbo/customer/Customer.java | 2 ++
1 file changed, 2 insertions(+)