1.分支操作
[root@fengxi ~]# cd /data/gitroot //进入gitroot 的仓库下
[root@fengxi gitroot]# git branch //查看分支
* master
[root@fengxi gitroot]# git branch lsk //创建分支
[root@fengxi gitroot]# git branch
lsk
* master
[root@fengxi gitroot]# git checkout lsk //切换到lsk分支下
Switched to branch 'lsk'
[root@fengxi gitroot]# git branch
* lsk
master
//再用git branch查看,会看到有两个分支master和lsk,当前使用的分支前面会有一个*在lsk分支下 ,编辑2.txt,并提交到新分支
[root@fengxi gitroot]# ls
1.txt
[root@fengxi gitroot]# echo "guojian" > 2.txt
[root@fengxi gitroot]# git add 2.txt //将2.txt添加至仓库
[root@fengxi gitroot]# git commit -m"new file 2.txt" //使用git commit -m "注释"把它提交到版本库:
[lsk e7265d4] new file 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@fengxi gitroot]# ls //查看
1.txt 2.txt
[root@fengxi gitroot]# git checkout master //切换回master分支
Switched to branch 'master'
[root@fengxi gitroot]# git branch //查看分支
lsk
* master
[root@fengxi gitroot]# ls
1.txt
(2)分支的合并
[root@fengxi gitroot]# git checkout master //合并分支之前,切换回master分支
Switched to branch 'master'
[root@fengxi gitroot]# git merge lsk //把lsk分支合并到master
Updating 45af9a3..e7265d4
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@fengxi gitroot]# ls
1.txt 2.txt
冲突举例以及解决办法(在master分支下,编辑2.txt,改为lsk分支里面2.txt的内容。然后提交2.txt,再合并lsk分支):
[root@fengxi gitroot]# echo "2" >> 2.txt
[root@fengxi gitroot]# git checkout lsk
M 2.txt
Switched to branch 'lsk'
[root@fengxi gitroot]# echo "1" >> 2.txt
[root@fengxi gitroot]# git add 2.txt //添加至仓库
[root@fengxi gitroot]# git commit -m "2.txt" //提交到版本库
[lsk c37502b] 2.txt
1 file changed, 2 insertions(+)
[root@fengxi gitroot]# cat 2.txt //查看2.txt的内容
guojian
2
1
[root@fengxi gitroot]# git checkout master //切换回master分支
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
[root@fengxi gitroot]# git branch //查看分支
lsk
* master
[root@fengxi gitroot]# cat 2.txt
guojian
[root@fengxi gitroot]# echo add 2 >> 2.txt
[root@fengxi gitroot]# cat 2.txt
guojian
add 2
[root@fengxi gitroot]# git add 2.txt
[root@fengxi gitroot]# git commit -m"2.txt"
[master 28ef615] 2.txt
1 file changed, 1 insertion(+)
[root@fengxi gitroot]# cat 2.txt
guojian
add 2
[root@fengxi gitroot]# git merge lsk //冲突
Auto-merging 2.txt
CONFLICT (content): Merge conflict in 2.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@fengxi gitroot]# vi 2.txt //编写相同的文件内容
guojian
2
1
~
"2.txt" 3L, 12C written
[root@fengxi gitroot]# git add 2.txt
[root@fengxi gitroot]# git commit -m"2.txt"
[master 5844aa0] 2.txt
[root@fengxi gitroot]# git merge lsk
Already up-to-date.
[root@fengxi gitroot]# cat 2.txt
guojian
2
1
(3)分支删除
[root@fengxi gitroot]# git branch -d lsk //删除分支
Deleted branch lsk (was c37502b).
假如分支没有合并,删除之前会提示,那就不合并,强制删除
[root@fengxi gitroot]# git branch -D lsk
Deleted branch lsk (was 89b88e8).
(4)使用分支的原则
原则:
① master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
② 创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master。
③ 开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己PC上)里面开发代码,然后合并到dev分支。
dev分支合并bob分支的命令是:
[root@fengxi gitroot]# git checkout dev //先切换到dev分支,然后
Switched to branch 'dev'
[root@fengxi gitroot]# git merge bob
Updating 89b88e8..ea53299
Fast-forward
ll | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 ll
(5)远程分支
对于git push分支分两种情况:
① 当本地分支和远程分支一致时
git push会把所有本地分支的变更一同推送到远程,如果想只推送一个分支,使用git push origin branch-name命令。
② 当本地分支比远程分支多
默认git push只推送本地和远程一致的分支,想要把多出来的本地分支推送到远程时,使用git push origin branch-name命令如果推送失败,先用git pull抓取远程的新提交。
[root@fengxi gitroot]# git ls-remote origin //查看远程分支命令(所有分支)
0ad9ccef7a34542dfa8a56d4392cc9881e134ae3 HEAD
0ad9ccef7a34542dfa8a56d4392cc9881e134ae3 refs/heads/master
添加一个demo分支
[root@fengxi gitroot]# git branch dev
[root@fengxi gitroot]# git branch demo
[root@fengxi gitroot]# git branch
demo
dev
* master
[root@fengxi gitroot]# git push origin dev
Counting objects: 23, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (23/23), 1.83 KiB | 0 bytes/s, done.
Total 23 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote: https://github.com/guo-xia-bub/demo-1/pull/new/dev
remote:
To git@github.com:guo-xia-bub/demo-1.git
* [new branch] dev -> dev
git clone的时候默认只把master分支克隆下来。如果想把所有分支都克隆下来,需要手动创建,在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name命令,需要注意本地和远程分支的名称要一致。
[root@fengxi gitroot]# cd /mnt
[root@fengxi mnt]# git clone git@github.com:guo-xia-bub/demo-1.git
Cloning into 'demo-1'...
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 26 (delta 0), reused 26 (delta 0), pack-reused 0
Receiving objects: 100% (26/26), done.
[root@fengxi mnt]# ls
demo-1
[root@fengxi mnt]# cd demo-1/
[root@fengxi demo-1]# ll -a
total 12
drwxr-xr-x. 3 root root 46 Nov 10 06:03 .
drwxr-xr-x. 3 root root 19 Nov 10 06:03 ..
-rw-r--r--. 1 root root 1 Nov 10 06:03 1.txt
drwxr-xr-x. 8 root root 4096 Nov 10 06:03 .git
-rw-r--r--. 1 root root 8 Nov 10 06:03 gitroot.sh
[root@fengxi demo-1]# git branch
* master
[root@fengxi demo-1]# git checkout -b demo origin/demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'
[root@fengxi demo-1]# git branch
* demo
master