跳转至

10. git分支

10. git分支

1.查看分支

git branch
[root@chupeng git04]# git branch
* master

2.创建分支

#1.创建分支
git branch cp1_branch

#2.查看当前所在的分支
git branch
[root@chupeng git04]# git branch cp1_branch
[root@chupeng git04]# git branch
  cp1_branch
* master

3.切换到分支

#1.切换分支
git checkout cp1_branch

#2.查看当前所在的分支
git branch
[root@chupeng git04]# git checkout cp1_branch
A       stash.txt
Switched to branch 'cp1_branch'
[root@chupeng git04]# git branch
* cp1_branch
  master

4.在分支中创建文件,提交

#1.在分支中创建文件
echo "branch" >branch1.txt

#2. 添加跟踪
git add .

#3. 提交
git commit -m "分支cp1第一次提交"

#4. 查看状态
git status

#5. 查看版本日志
git log --oneline
[root@chupeng git04]# ls
chupeng.txt  cp_v2  stash.txt

[root@chupeng git04]# echo "branch~~~~~">branch1.txt
[root@chupeng git04]# ls
branch1.txt  chupeng.txt  cp_v2  stash.txt

[root@chupeng git04]# git add .
[root@chupeng git04]# git commit -m "分支cp1第一次提交"
[cp1_branch fab9b2a] 分支cp1第一次提交
 2 files changed, 2 insertions(+)
 create mode 100644 branch1.txt
 create mode 100644 stash.txt
[root@chupeng git04]# git status
# On branch cp1_branch
nothing to commit, working directory clean
[root@chupeng git04]# git log --oneline
fab9b2a 分支cp1第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交

5.切换到主分支,查看状态

#1. 切换到master分支
git checkout master

#2. 查看分支
git branch

#3. 查看本地文件,发现看不到在分支中创建的文件!
ls

#4. 查看版本日志,看不到分支的版本日志
git log --oneline
[root@chupeng git04]# git checkout master
Switched to branch 'master'
[root@chupeng git04]# git branch
  cp1_branch
* master
[root@chupeng git04]# ls
chupeng.txt  cp_v2
[root@chupeng git04]# git log --oneline
e4c9c8a 第二次提交
db2fda5 首次提交

6.此时可以删除分支,也可以合并分支

6.1 删除分支
#1. 删除分支
git branch -D cp1_branch

#2. 查看分支
git branch
[root@chupeng git04]# git branch -D cp1_branch
Deleted branch cp1_branch (was fab9b2a).
[root@chupeng git04]# git branch
* master
6.2 合并分支

创建新分支

#1.创建新分支,并切换到分支。
# -b 切换到分支
git checkout -b cp2_branch

#2.模拟添加代码到分支
touch branch2.txt
ls

#3. 提交
git add .
git commit -am "cp2分支第一次提交"

#4. 查看分支的版本
git log --oneline
[root@chupeng git04]# git checkout -b cp2_branch
Switched to a new branch 'cp2_branch'
[root@chupeng git04]# ls
chupeng.txt  cp_v2
[root@chupeng git04]# touch branch2.txt
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2
[root@chupeng git04]# git add .
[root@chupeng git04]# git commit -m "cp2分支第一次提交"
[cp2_branch a0c040a] cp2分支第一次提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 branch2.txt
[root@chupeng git04]# git log --oneline
a0c040a cp2分支第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交

切换到主分支,合并分支

#1.切换到主分支
git checkout master

#2.查看当前所在的分支
git branch

#3. 查看本地文件
ls

#4. 合并cp2分支
git merge cp2_branch

#5. 再次查看本地文件,发现在分支中创建的文件已经可以在主分支看到了
ls

#6. 查看主分支版本日志
git log --oneline
[root@chupeng git04]# git checkout master
Switched to branch 'master'
[root@chupeng git04]# git branch
  cp2_branch
* master
[root@chupeng git04]# ls
chupeng.txt  cp_v2
[root@chupeng git04]# git merge cp2_branch
Updating e4c9c8a..a0c040a
Fast-forward
 branch2.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 branch2.txt
[root@chupeng git04]# ls
branch2.txt  chupeng.txt  cp_v2
[root@chupeng git04]# git log --oneline
a0c040a cp2分支第一次提交
e4c9c8a 第二次提交
db2fda5 首次提交

此时可以删除cp2分支

# 由于已经将cp2分支合并了,所以可以将cp2分支删除!
#1.删除cp2分支
git branch -d cp2_branch

#2. 查看分支
git branch
[root@chupeng git04]# git branch -d cp2_branch
Deleted branch cp2_branch (was a0c040a).
[root@chupeng git04]# git branch
* master

最后更新: 2022-02-18 11:26:47