git 分支操作 新建分支 切换分支(本地分支) 合并分支 删除分支(本地) 并行操作 解决合并冲突 分支上拉取最新的代码的方法 git pull和git fetch的区别 使用git fetch,如果想要合并到本地分支的操作 使用git rebase进行合并注意事项 继续上次未评审的代码提交

如果想要创建一个远程都没有的本地分支

git branch <branchname>

切换分支(本地分支)

git checkout <branch>

如果想要创建一个基于远程分支的本地分支(基于分支的分支)并且想要切换到这个分支

# 先使用git branch -a看好自己要基于哪个分支创建
git branch -b 分支名字 remotes/origin/分支名字

合并分支

# 可以合并远程,也可以合并本地的。会将branchname的最后一次记录放入到当前分支
git merge <branchname>

删除分支(本地)

git branch -d <branchname>

并行操作

意思是:基于一个分支,多个人修改,并且提交。或者一个人提交了,但是还没有合并,又在之前的版本修改提交。

示例

git branch issue2
git branch issue3
git checkout issue2
vim myfile.txt
git add .
git commit -m "XXX"
git checkout issue3
vim myfile.txt
git add .
git commit -m "XXX"

造成的后果:并行操作后果

解决合并冲突

主要思路:将并行化的操作,串行化解决
以上面造成的问题进行解决

方法1

git checkout master
git merge issue2
git merge issue3
# 会出现错误:
# Auto-merging myfile.txt
# CONFLICT (content): Merge conflict in myfile.txt
# Automatic merge failed; fix conflicts and then commit the result.
# 进入myfile.txt文件将出现冲突的内容做个修改就可以了
vim myfile.txt

方法2

使用git rebase。可以使提交的历史记录显得更简洁

git checkout master
git merge issue2
git checkout issue3
git rebase master # 将master上的代码合并到issue3
# 依旧会报错
# Auto-merging myfile.txt
# CONFLICT (content): Merge conflict in myfile.txt
# error: could not apply 4e38767... add kkk
# Resolve all conflicts manually, mark them as resolved with
# "git add/rm <conflicted_files>", then run "git rebase --continue".
# You can instead skip this commit: run "git rebase --skip".
# To abort and get back to the state before "git rebase", run "git rebase --abort".
# Could not apply 4e38767... add kkk
# 解决办法和方法1一样
vim myfile.txt # 修改有冲突的部分
# rebase的时候,修改冲突后的提交不是使用commit命令,而是执行rebase命令指定 --continue选项。若要取消rebase,指定 --abort选项。
git add myfile.txt
git rebase --continue # 在issue3的分支中提交 这个是合并后的提交操作,--commit描述不变
# 解决冲突之后,将issue3合并到master中
git checkout master
git merge master

分支上拉取最新的代码的方法

# 首先使用git remote查看远程库的名字
git remote
# 如果没有修改会输出origin
git pull remote <远程分支的名字> # 比如:git pull remote master
# 但是如果后面不跟一些参数的话就会有一大堆警告
# hint: Pulling without specifying how to reconcile divergent branches is
# hint: discouraged. You can squelch this message by running one of the following
# hint: commands sometime before your next pull:
# hint: 
# hint:   git config pull.rebase false  # merge (the default strategy)
# hint:   git config pull.rebase true   # rebase
# hint:   git config pull.ff only       # fast-forward only
# hint: 
# hint: You can replace "git config" with "git config --global" to set a default
# hint: preference for all repositories. You can also pass --rebase, --no-rebase,
# hint: or --ff-only on the command line to override the configured default per

# 解决办法他已经提出来了。主要是我们要按照需求来处理
# git config pull.rebase false这个代表着git pull remote master这里不带参数的时候,拉取下来之后,合并之后就会生成一个commit记录
# git config pull.rebase false这个代表着git pull remote master这里不带参数的时候,拉取下来之后,合并之后就不会生成一个commit记录
# git config pull.rebase only这个代表着git pull remote master这里不带参数的时候,如果有合并冲突会直接终止操作。如果想要继续拉取可以使用git pull remote master --no-ff或者git pull remote master --rebase来解决。git pull remote master --no-ff这个表示拉取下来之后,进行合并之后会生成一个新的提交

# 新的提交的意思是指在git log的记录中会存在一个merge的提交描述。还有上面的这些配置说是否想要有个merge的提交描述而已。
# 详细可以查看:https://blog.csdn.net/wq6ylg08/article/details/114106272 这个博客

git pull和git fetch的区别

git pull 和 git fetch都是从远程拉取最新的代码,但是git pull = git fetch + git merge

使用git fetch,如果想要合并到本地分支的操作

# 示例
# 在本地新建一个temp分支,并将远程origin仓库的master分支代码下载到本地temp分支
git fetch origin master:tmp 

# 来比较本地代码与刚刚从远程下载下来的代码的区别
git diff master tmp 

# 合并temp分支到本地的master分支
git merge tmp

# 如果不想保留temp分支 可以用这步删除 
git branch -d temp

使用git rebase进行合并注意事项

可以参考:https://www.jianshu.com/p/6960811ac89c
注:git rebase --continue这个命令是用来解决冲突的,如果在git rebase <远程库>时候,没有任何冲突,就会报:no process rebase?

继续上次未评审的代码提交

# 修改了代码之后
git add .
# 提交代码。如果不小心使用git commit -m 'XXX'操作,可以使用git reset --soft HEAD^来撤销你的commit
git commit --am
# 推送到评审
git push origin HEAD:refs/for/分支名