IP | Hostname | 备注 |
---|---|---|
192.168.88.31 | server | git服务端 |
192.168.88.33 | client | git客户端 |
环境说明:firewalld关闭,selinux关闭。
上一篇博客:git安装及使用
需求:
1.查看远程分支、查看本地分支、查看全部分支;
2.创建本地分支devserver1-6,创建dev分支并立即切换;
3.切换回主分支,并查看当前所在分支;
4.在分支devserver1里创建一个test.txt,并切换回主分支查看是否存在这个test.txt文件;
5.合并devserver1和主分支,查看主分支里是否有test.txt文件;
6.删除本地分支devserver1,查看剩下所有分支;
7.推送本地分支devserver2-6到远端服务器,并查看是否成功;
8.运用三种办法删除远端分支devserver2-4;
9.在远程创建分支linuxgit,并拉取分支到本地客户端服务器上;
10.如何快速有效的拉取远程服务端的全部分支;
Git常用分支管理命令:
查看分支:git branch
创建分支:git branch <name>
切换分支:git checkout <name>
创建+切换分支:git checkout -b <name>
合并某分支到当前分支:git merge <name>
删除分支:git branch -d <name>
添加到暂存区;git add 文件名
提交文件代码;git commit 文件
-m
推送代码到远程仓库;git push origin <name>
删除远程分支;git branch -r -d origin/<name>
git push origin --delete <name>
git push origin :<name>
拉取分支到本地客户端:git fetch origin 远程分支名:本地分支名
在执行git分支管理里面前,我们需将git仓库克隆下来。
git clone git@192.168.88.31:/home/git/project.git //远程克隆
git clone file:///home/git/project.git //本地克隆
1.查看远程分支、查看本地分支、查看全部分支;
[root@client ~]# git clone git@192.168.88.31:/home/git/project.git
正克隆到 'project'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[root@client ~]# cd project/
[root@client project]# git branch -r //查看远程分支
origin/HEAD -> origin/master
origin/master
[root@client project]# git branch //查看本地分支
* master
[root@client project]# git branch -a //查看全部分支
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
2.创建本地分支devserver1-6,创建dev分支并立即切换;
[root@client project]# git branch devserver1
[root@client project]# git branch devserver2
[root@client project]# git branch devserver3
[root@client project]# git branch devserver4
[root@client project]# git branch devserver5
[root@client project]# git branch devserver6
[root@client project]# git checkout -b dev
切换到一个新分支 'dev'
[root@client project]# git branch
* dev
devserver1
devserver2
devserver3
devserver4
devserver5
devserver6
master
3.切换回主分支,并查看当前所在分支;
[root@client project]# git checkout master
切换到分支 'master'
[root@client project]# git branch
dev
devserver1
devserver2
devserver3
devserver4
devserver5
devserver6
* master
[root@client project]#
4.在分支devserver1里创建一个test.txt,并切换回主分支查看是否存在这个test.txt文件;
[root@client project]# git checkout devserver1
切换到分支 'devserver1'
[root@client project]# echo "test" >> test.txt
[root@client project]# ll
总用量 8
-rw-r--r-- 1 root root 8 3月 18 03:18 1.html
-rw-r--r-- 1 root root 5 3月 18 03:40 test.txt
[root@client project]# git add test.txt
[root@client project]# git commit -m "create devserver1 branch"
[devserver1 0c2cd1e] create devserver1 branch
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@client project]# git checkout master
切换到分支 'master'
[root@client project]# ll
总用量 4
-rw-r--r-- 1 root root 8 3月 18 03:18 1.html
[root@client project]#
5.合并devserver1和主分支,查看主分支里是否有test.txt文件;
[root@client project]# git merge devserver1
更新 9e4aa26..0c2cd1e
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@client project]# ll
总用量 8
-rw-r--r-- 1 root root 8 3月 18 03:18 1.html
-rw-r--r-- 1 root root 5 3月 18 03:42 test.txt
6.删除本地分支devserver1,查看剩下所有分支;
[root@client project]# git branch -d devserver1
已删除分支 devserver1(曾为 0c2cd1e)。
[root@client project]# git branch
dev
devserver2
devserver3
devserver4
devserver5
devserver6
* master
7.推送本地分支devserver2-6到远端服务器,并查看是否成功;
[root@client project]# git remote
origin
[root@client project]# git remote -v
origin git@192.168.88.31:/home/git/project.git (fetch)
origin git@192.168.88.31:/home/git/project.git (push)
[root@client project]# git push origin devserver2 devserver3 devserver4 devserver5 devserver6
Total 0 (delta 0), reused 0 (delta 0)
To git@192.168.88.31:/home/git/project.git
* [new branch] devserver2 -> devserver2
* [new branch] devserver3 -> devserver3
* [new branch] devserver4 -> devserver4
* [new branch] devserver5 -> devserver5
* [new branch] devserver6 -> devserver6
[root@client project]# git branch -r
origin/HEAD -> origin/master
origin/devserver2
origin/devserver3
origin/devserver4
origin/devserver5
origin/devserver6
origin/master
8.运用三种办法删除远端分支devserver2-4;
[root@client project]# git branch -r -d origin/devserver2
已删除远程分支 origin/devserver2(曾为 9e4aa26)。
[root@client project]# git push origin :devserver3
To git@192.168.88.31:/home/git/project.git
- [deleted] devserver3
[root@client project]# git push origin --delete devserver4
To git@192.168.88.31:/home/git/project.git
- [deleted] devserver4
[root@client project]# git branch -r
origin/HEAD -> origin/master
origin/devserver5
origin/devserver6
origin/master
9.在远程创建分支linuxgit,并拉取分支到本地客户端服务器上;
【远程客户端】
[root@client project]# git checkout -b linuxgit
切换到一个新分支 'linuxgit'
[root@client project]# echo "linxgit" >> linuxgit.txt
[root@client project]# git add .
[root@client project]# git commit -m "linuxgit"
[linuxgit aa44e82] linuxgit
1 file changed, 1 insertion(+)
create mode 100644 linuxgit.txt
[root@client project]# git push origin linuxgit
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.88.31:/home/git/project.git
0c2cd1e..aa44e82 linuxgit -> linuxgit
【本地客户端】
[root@server project]# git branch
* master
[root@server project]# git fetch origin linuxgit:linuxgit1
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 file:///home/git/project
* [新分支] linuxgit -> linuxgit1
[root@server project]# git checkout linuxgit1
切换到分支 'linuxgit1'
[root@server project]# ll
总用量 12
-rw-r--r-- 1 root root 8 3月 17 20:04 1.html
-rw-r--r-- 1 root root 8 3月 17 20:14 linuxgit.txt
-rw-r--r-- 1 root root 5 3月 17 20:06 test.txt
10.如何快速有效的拉取远程服务端的全部分支;
[root@server project]# git checkout -b devtest
切换到一个新分支 'devtest'
[root@server project]# git branch -r | grep -v '\->' | cut -d'/' -f2 | xargs -n1 -i git fetch origin {}:{}
来自 file:///home/git/project
* [新分支] devserver5 -> devserver5
来自 file:///home/git/project
* [新分支] devserver6 -> devserver6
来自 file:///home/git/project
* [新分支] linuxgit -> linuxgit
来自 file:///home/git/project
! [已拒绝] master -> master (非快进式)
[root@server project]# git branch
devserver5
devserver6
* devtest
linuxgit
linuxgit1
master
参考博客: