1. github创建repo,clone到本地。
项目越来越复杂,感觉放在一个folder里面不方便,于是另分个区出来给它。同时,再从master拉个新branch。
git init
git clone https://github.com/aiXpertLab/gitrebase.git
2. git log查看,有两个commit。
git log
commit e84129af810b292502acb02c6ac816e543a70bd0
Author: aiXpert <45121432+aiXpertLab@users.noreply.github.com>
Date: Tue Jan 9 18:01:15 2024 -0500
Create README.md
commit fde1fb3f7cb9106b53c0822d69b5dbbbc105472c
Author: aiXpert <45121432+aiXpertLab@users.noreply.github.com>
Date: Tue Jan 9 18:01:00 2024 -0500
Initial commit
3. 查看可以checkout的branch
git branch -a
git branch
---------------
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
4. 创建新分支,切换checkout分支,分支push到服务器
git branch develop
git checkout develop
git push origin develop:develop
git checkout -b v1 develop
git push origin v1:v1
然后可以在新的分支上愉快的写代码开发新功能了.
4.1 checkout已有分支,
git checkout develop
git fetch origin
git merge master //本地同步master
git push //推到remote,确保远端develop和master同步
5. 用add命令来添加新写的代码,commit命令用来提交新写的代码
git add .
git commit -m "add v1"
--------------
[v1 ef2779a] add v1
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 branch.txt
create mode 100644 log.txt
create mode 100644 ls.txt
6. git status查看一下
git status
On branch v1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: status.v1
Untracked files:
(use "git add <file>..." to include in what will be committed)
status.v2
status.v2.txt
7. add命令执行后,修改被保存到暂存区。
接下来,不可以直接用到merge命令,合并v1到develop去,如果这样做,很可能出现冲突。因为可能出现有很多人在develop分支上更新。所以你这个时候用pull命令,把远程仓库的更新取回并更新。
git checkout develop
git pull origin develop
------------------
From https://github.com/aiXpertLab/gitrebase
* branch develop -> FETCH_HEAD
Already up to date.
8. 然后再切换回自己的分支,用rebase命令合并新更新到自己目前工作的分支。
git checkout v1
git rebase develop
一般情况下rebase都是会有冲突的,详细查看冲突可以用命令git status然后就会显示哪个文件有冲突,然后打开有冲突的哪个文件,会发现有一些“«««<”, “=======”, “»»»>” 这样的符号。
"<<<<<<<" 表示冲突代码开始
"=======" 表示test与分支冲突代码分隔符
">>>>>>>" 表示冲突代码的结束
<<<<<<<
所以这一块区域test分支的代码
=======
这一块区域develop分支的代码
>>>>>>>
rebase 和 merge的另一个区别是rebase 的冲突是一个一个解决,如果有十个冲突,先解决第一个,然后用命令
git add -u
git rebase --continue
继续后才会出现第二个冲突,直到所有冲突解决完,而merge 是所有的冲突都会显示出来。
所以rebase的工作流就是
git rebase
while(存在冲突) {
git status
找到当前冲突文件,编辑解决冲突
git add -u
git rebase --continue
if( git rebase --abort )
break;
}
最后冲突全部解决,rebase成功!!
9. 合并分支git merge –no-ff dev 到develop
git checkout develop
git pull
git merge --no-ff develop
10. 更新到github
git push origin develop
11. remote增加branch
//To fetch a branch, you simply need to:
git fetch origin
//This will fetch all of the remote branches for you. With the remote branches
//in hand, you now need to check out the branch you are interested in, giving
//you a local working copy: