您的位置:首页 > 大数据 > 人工智能

分支管理#180804

2018-08-04 11:44 134 查看

查看当前仓库分支

[root@localhost ~]# cd /data/gitroot/
[root@localhost gitroot]# git branch
* master


*为当前所在的分支的位置

创建新分支

[root@localhost gitroot]# git branch main
[root@localhost gitroot]# git branch
main
* master
[root@localhost gitroot]# ls
1.txt

切换分支

[root@localhost gitroot]# git checkout main
Switched to branch 'main'
[root@localhost gitroot]# git branch
* main
master
[root@localhost gitroot]# ls
1.txt

[root@localhost gitroot]# echo "222" > 2.txt
[root@localhost gitroot]# git add .
[root@localhost gitroot]# git commit -m "add 2.txt"
[main 9b0965c] add 2.txt
Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 1 insertion(+)
create mode 100644 2.txt

[root@localhost gitroot]# ls
1.txt  2.txt
[root@localhost gitroot]# git checkout master
Switched to branch 'master'
[root@localhost gitroot]# ls
1.txt

分支合并

合并之前,先切换到目标分支

[root@localhost gitroot]# git checkout master
Already on 'master'
[root@localhost gitroot]# git branch
main
* master

[root@localhost gitroot]# git merge main
Updating c246d69..9b0965c
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@localhost gitroot]# ls
1.txt  2.txt

合并冲突

[root@localhost gitroot]# echo "222" >> 2.txt
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m "add 2.txt"
[master 8658308] add 2.txt
Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 1 insertion(+)

[root@localhost gitroot]# git checkout main
Switched to branch 'main'
[root@localhost gitroot]# echo "333" >> 2.txt
[root@localhost gitroot]# git add 2.txt
[root@localhost gitroot]# git commit -m "add 2.txt"
[main 7f8ccff] add 2.txt
Committer: root <root@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"
git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

1 file changed, 1 insertion(+)

[root@localhost gitroot]# git checkout master
Switched to branch 'master'
[root@localhost gitroot]# git merge main
Auto-merging 2.txt	#自动合并 2.txt
CONFLICT (content): Merge conflict in 2.txt	#冲突(内容):合并冲突于2.txt
Automatic merge failed; fix conflicts and then commit the result.	#自动合并失败,修正冲突然后提交修正的结果


解决冲突的方法就是修改内容为一致,将当前内容修改为与main的2.txt一致

若main的内容为保留内容,可使用倒着合并,即先切换到main下,在进行合并

[root@localhost gitroot]# cat 2.txt
222
<<<<<<< HEAD
222
=======
333
>>>>>>> main


冲突部分,上面为master,下面main

[root@localhost gitroot]# git checkout main
2.txt: needs merge
error: you need to resolve your current index first	#您需要先解决当前索引的冲突

[root@localhost gitroot]# git checkout main
Switched to branch 'main'


当发生冲突,不能切换分支

查看提示信息

[root@localhost gitroot]# git commit -a
Merge branch 'main'

Conflicts:
2.txt
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: root <root@localhost.localdomain>
#
# On branch master
# All conflicts fixed but you are still merging.
#   (use "git commit" to conclude merge)
#
# Changes to be committed:
#
#       modified:   2.txt
#


解决冲突方法,修改内容为一致,或将冲突文件进行重命名,再次合并

删除分支

[root@localhost gitroot]# git branch -d main
error: Cannot delete the branch 'main' which you are currently on.	#无法删除当前所在的分支

[root@localhost gitroot]# git checkout master
Switched to branch 'master'
[root@localhost gitroot]# git branch -d main
Deleted branch main (was 7f8ccff).	#已删除分支


若分支还没有合并,删除前则会提示

强制删除

[root@localhost gitroot]# git branch -D main


若不合并,则会强制删除
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TryAgain