git分支管理与恢复

1分支管理

创建

创建分支之前需要查看当前的分支情况。Git branch -a命令可以查看所有的本地和远程分支。

$ git branch -a
* a-branch
  b-branch
  c-branch
  d-branch
  master
当前所处的分支是a-branch

Git checkout master是切换到master分支
$ git checkout master
Switched to branch 'master'

-b参数指定创建分支后同时切换到最新分支。新分支名称为X-branch,以master分支创建新分支。
$ git checkout -b X-branch master
Switched to a new branch 'X-branch'

删除

如果使用-d参数删除,分支如何没有和其他分支合并过,就会出现下面的错误。如果确定分支不需要合并直接删除,可以改成使用-D参数。
$ git branch -d a-branch
error: The branch 'a-branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D a-branch'.

$ git branch -D a-branch
Deleted branch a-branch (was 6c821ee).

合并与冲突

使用当前分支合并b-branch分支,出现冲突会提示如下。同时冲突的文件会标有黄色的谈好,需要自己去手动处理冲突。
$ git merge b-branch
Auto-merging b.txt
CONFLICT (content): Merge conflict in b.txt
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.

git mergetool工具解决冲突,冲突解决的时候会生成3个文件(各分支一个,分支合并的一个),选择文件后就是使用vim命令处理文件。合并冲突后会生成.orig文件,是合并前的原文件。
$ git mergetool

This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc codecompare smerge emerge vimdiff
Merging:
a.txt
b.txt

Normal merge conflict for 'a.txt':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (vimdiff): a.txt
还有 4 个文件等待编辑

Normal merge conflict for 'b.txt':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (vimdiff): b.txt
还有 4 个文件等待编辑

2远程提交/合并

本地分支提交信息推送到远程
$ git push origin 本地分支名称

3删除分支的恢复

查看分支情况
$ git reflog
4be25b4 (HEAD -> c-branch) HEAD@{0}: commit: c分支
51bb833 (d-branch) HEAD@{1}: checkout: moving from b-branch to c-branch
0567ec4 (b-branch) HEAD@{2}: commit: b分支
51bb833 (d-branch) HEAD@{3}: checkout: moving from master to b-branch
e5effe9 (master, X-branch) HEAD@{4}: checkout: moving from X-branch to master
e5effe9 (master, X-branch) HEAD@{5}: checkout: moving from master to X-branch
e5effe9 (master, X-branch) HEAD@{6}: checkout: moving from a-branch to master
6c821ee HEAD@{7}: reset: moving to HEAD
6c821ee HEAD@{8}: reset: moving to HEAD
6c821ee HEAD@{9}: commit (amend): 测试撤销操作
af2f51f HEAD@{10}: commit (amend): 测试撤销操作
cc585fa HEAD@{11}: commit: 撤销操作
8649811 HEAD@{12}: commit (amend): 测试一下撤销操作
37d332e HEAD@{13}: commit (amend): 测试一下撤销操作
f3e54c1 HEAD@{14}: commit (amend): 测试一下撤销操作
40023aa HEAD@{15}: commit (amend): 测试撤销操作
7430ce9 HEAD@{16}: commit (merge): 测试撤销操作
f610a36 HEAD@{17}: commit: a-branch分支冲突
6c732d8 HEAD@{18}: checkout: moving from master to a-branch
e5effe9 (master, X-branch) HEAD@{19}: commit: 冲突演示
6c732d8 HEAD@{20}: merge a-branch: Fast-forward
238b8ae (tag: v2019.09.26.10.56.001) HEAD@{21}: checkout: moving from a-branch to master

使用7430ce9该分支,分支名称为c-master
$ git checkout -b c-master 7430ce9
Switched to a new branch 'c-master'
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读