文章目录
一、初识分支
1.分支定义
在开发中,master表示主线。
在开发新功能时,会创建一个分支,等到开发完成后,会合并产生一个新版本。
分支可以给使用者提供多个环境,意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线。
主线上永远都是正式版本,分支上测试没有问题之后,会将其添加到主线,这样才不会影响主线上的正式版本的发行使用。
主线和分支间、各分支之间做了代码隔离。
2.git分支常见命令
- 查看当前所在分支
git branch
- 创建分支
git branch name
- 切换分支
git checkout name
- 合并分支
git merge name
- 删除分支
git branch -d name
二、基于分支修复线上bug
1.紧急修复线上bug的思路
大致思路是另建一个分支,专门用于解决bug,等到bug解决后再与主线合并,图示如下:
2.修复分支bug实现
- 查看目前处在的分支
git branch
- 创建分支
git branch 分支名字
- 切换分支
git checkout 分支名称
- 分支合并(可能产生冲突)
git merge 要合并的分支
- 删除分支
git branch -d 分支名称
示例如下:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch
* master
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch dev
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch
dev
* master
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git checkout dev
Switched to branch 'dev'
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git branch
* dev
master
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ vim index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git commit -m 'shop'
[dev e780977] shop
1 file changed, 4 insertions(+), 2 deletions(-)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git log
commit e780977495aa5bc83c6aef32913743345b290c05 (HEAD -> dev)
Author: Corley <[email protected]>
Date: Sat Mar 28 16:50:08 2020 +0800
shop
commit f12719f22469243dd4e95bc3de7da2f524627938 (master)
Author: Corley <[email protected]>
Date: Fri Mar 27 16:17:09 2020 +0800
-v3
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project</title>
</head>
<body>
<ul>
<li>English</li>
<li>French</li>
<li>Spain</li>
<li>Russia</li>
</ul>
<ul>
<li>Math</li>
<li>Physics</li>
</ul>
<ul>
<li>History</li>
<li>Science</li>
</ul>
<ul>
<li>Art</li>
</ul>
</body>
</html>
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git checkout master
Switched to branch 'master'
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project</title>
</head>
<body>
<ul>
<li>English</li>
<li>French</li>
<li>Spain</li>
<li>Russia</li>
</ul>
<ul>
<li>Math</li>
<li>Physics</li>
</ul>
<ul>
<li>History</li>
<li>Science</li>
</ul>
</body>
</html>
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch bug
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch
bug
dev
* master
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git checkout bug
Switched to branch 'bug'
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (bug)
$ vim index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (bug)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (bug)
$ git commit -m 'nobug'
[bug 2694c72] nobug
1 file changed, 2 insertions(+), 1 deletion(-)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (bug)
$ git log
commit 2694c72bcf04da286b6941fb820ade8f856a694e (HEAD -> bug)
Author: Corley <[email protected]>
Date: Sat Mar 28 16:56:43 2020 +0800
nobug
commit f12719f22469243dd4e95bc3de7da2f524627938 (master)
Author: Corley <[email protected]>
Date: Fri Mar 27 16:17:09 2020 +0800
-v3
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (bug)
$ git checkout master
Switched to branch 'master'
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git merge bug
Updating f12719f..2694c72
Fast-forward
index.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project</title>
</head>
<body>
<ul>
<li>English</li>
<li>French</li>
<li>Spain</li>
<li>Russia</li>
</ul>
<ul>
<li>Math</li>
<li>Physics</li>
</ul>
<ul>
<li>History</li>
<li>Science</li>
<li>Bug Solved</li>
</ul>
</body>
</html>
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch -d bug
Deleted branch bug (was 2694c72).
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git branch
dev
* master
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git checkout dev
Switched to branch 'dev'
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ vim index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git commit -m 'shopok'
[dev 9668e01] shopok
1 file changed, 1 insertion(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git checkout master
Switched to branch 'master'
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git merge dev
Auto-merging index.html
Merge made by the 'recursive' strategy.
index.html | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'noconflict'
On branch master
nothing to commit, working tree clean
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 76fdfa89581308fd3e21e06a6e87a30cd26a11ee (HEAD -> master)
Merge: 2694c72 9668e01
Author: Corley <[email protected]>
Date: Sat Mar 28 17:05:11 2020 +0800
Merge branch 'dev'
commit 9668e01c33ab314cc53eb7de84f6c4cc704102a0 (dev)
Author: Corley <[email protected]>
Date: Sat Mar 28 17:04:32 2020 +0800
shopok
commit 2694c72bcf04da286b6941fb820ade8f856a694e
Author: Corley <[email protected]>
Date: Sat Mar 28 16:56:43 2020 +0800
nobug
commit e780977495aa5bc83c6aef32913743345b290c05
Author: Corley <[email protected]>
Date: Sat Mar 28 16:50:08 2020 +0800
shop
commit f12719f22469243dd4e95bc3de7da2f524627938
Author: Corley <[email protected]>
Date: Fri Mar 27 16:17:09 2020 +0800
-v3
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
合并应该在主线中进行,合并之后可以删除分支。
合并时可能遇到冲突,可以手动解决,打开文档会看到有标注,根据正确的目标修改要解决的冲突。
主线上必须是正式版本;分支上是开发版本。
三、GitHub的使用
情景假设:
一个程序猿在一个公司上班,在公司码代码后,回到家中需要继续码,有下图中的方式实现代码同步:
- 携带电脑或U盘拷贝
- 传到百度网盘等云端存储
- 使用GitHub进行代码托管
显然,程序猿一般都会选择使用Github来同步代码。
使用GitHub实现代码托管的步骤如下:
- 注册账号
- 创建仓库
- 本地代码推送到远程仓库
常见命令如下:
- 给仓库起别名
git remote add origin 远程仓库地址
- 将代码推送到Github
git push -u origin 分支
- 初次下载(克隆)代码
git clone 远程仓库地址
演示如下:
四、代码同步和忘记推送代码的解决
1.Github实现家和公司代码的同步
- 切换分支
git checkout 分支
- 在公司下载完代码后,继续开发
切换到dev分支进行开发
git checkout dev
- 把master分支合并到dev
git merge master
- 提交代码
git add .
git commit -m "xxx"
git push origin dev
- 开发完毕,要上线
将dev分支合并到master,进行上线
git checkout master
git merge dev
git push origin master
- 把dev分支也推送到远程
git checkout dev
git merge master
git push origin dev
测试如下:
在公司开发并提交:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (master)
$ git pull origin dev
From https://github.com/corleytd/projecttest
* branch dev -> FETCH_HEAD
* [new branch] dev -> origin/dev
Already up to date.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (master)
$ git checkout dev
Switched to a new branch 'dev'
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git merge master
Updating 9668e01..76fdfa8
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ vim index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git status
On branch dev
Your branch is ahead of 'origin/dev' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git commit -m 'devincomany'
[dev 3c1ae7e] devincomany
1 file changed, 1 insertion(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git push origin dev
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 326 bytes | 326.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/corleytd/projecttest.git
9668e01..3c1ae7e dev -> dev
回到家中pull代码进行开发并上线提交:
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git push -u origin dev
Everything up-to-date
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git pull origin master
From https://github.com/corleytd/projecttest
* branch master -> FETCH_HEAD
Already up to date.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git merge dev
Already up to date.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git pull origin dev
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 306 bytes | 21.00 KiB/s, done.
From https://github.com/corleytd/projecttest
* branch dev -> FETCH_HEAD
9668e01..3c1ae7e dev -> origin/dev
Updating 76fdfa8..3c1ae7e
Fast-forward
index.html | 1 +
1 file changed, 1 insertion(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git merge dev
Already up to date.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ vim index.html
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git add .
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git commit -m 'inhomefinished'
[master 3796244] inhomefinished
1 file changed, 1 insertion(+)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git log
commit 379624423128adf931c09717f93ada473b48bd90 (HEAD -> master)
Author: Corley <[email protected]>
Date: Sun Mar 29 14:01:57 2020 +0800
inhomefinished
commit 3c1ae7e45ed113a5b8d2ca6191830494ece7961a (origin/dev)
Author: Corley <[email protected]>
Date: Sun Mar 29 13:57:06 2020 +0800
devincomany
commit 76fdfa89581308fd3e21e06a6e87a30cd26a11ee (origin/master)
Merge: 2694c72 9668e01
Author: Corley <[email protected]>
Date: Sat Mar 28 17:05:11 2020 +0800
Merge branch 'dev'
commit 9668e01c33ab314cc53eb7de84f6c4cc704102a0 (dev)
Author: Corley <[email protected]>
Date: Sat Mar 28 17:04:32 2020 +0800
shopok
commit 2694c72bcf04da286b6941fb820ade8f856a694e
Author: Corley <[email protected]>
Date: Sat Mar 28 16:56:43 2020 +0800
nobug
commit e780977495aa5bc83c6aef32913743345b290c05
Author: Corley <[email protected]>
Date: Sat Mar 28 16:50:08 2020 +0800
shop
commit f12719f22469243dd4e95bc3de7da2f524627938
Author: Corley <[email protected]>
Date: Fri Mar 27 16:17:09 2020 +0800
-v3
commit 742d05cf56bd15331d6c762a2f6f86b56c14a45b
Author: Corley <[email protected]>
Date: Fri Mar 27 16:06:18 2020 +0800
v2
commit 2ec36954c41be1836644d2f18d185ca8e1f67c78
Author: Corley <[email protected]>
Date: Fri Mar 27 16:01:43 2020 +0800
v1.15
commit 5f6b74bb67e375a4f9ebc2658893b5eaab2e3bb1
Author: Corley <[email protected]>
Date: Fri Mar 27 15:58:08 2020 +0800
v1.1
commit 1e8ddbf90b34a2c11374813cd6d154d73283428e
Author: Corley <[email protected]>
Date: Fri Mar 27 15:51:36 2020 +0800
v1
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git pull origin dev
From https://github.com/corleytd/projecttest
* branch dev -> FETCH_HEAD
Already up to date.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git pull origin master
From https://github.com/corleytd/projecttest
* branch master -> FETCH_HEAD
Already up to date.
2.忘记推送代码的补救
- 在公司拉代码
git pull origin dev
- 提交代码
git add .
git commit -m "xxx"
但是并没有提交到GitHub托管。
- 回家继续写代码
拉代码,发现并没有公司的代码
git pull origin dev
无奈,继续开发其他功能
- 把dev分支也推送到远程
git add .
git commit -m "xxx"
git push origin dev
- 到公司继续写代码
拉代码,把昨天的代码拉到本地(可能存在冲突)
git pull origin dev
- 手动解决冲突,继续开发
把dev分支也推送到远程
git add .
git commit -m "xxx"
git push origin dev
进行测试如下:
- 在公司开发忘记推送代码
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git branch
* dev
master
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ vim demo.py
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git add .
warning: LF will be replaced by CRLF in demo.py.
The file will have its original line endings in your working directory
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git commit -m 'donotforget'
[dev 666839c] donotforget
1 file changed, 1 insertion(+)
create mode 100644 demo.py
- 回家开发,发现在公司忘记上传代码
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (master)
$ git pull origin dev
From https://github.com/corleytd/projecttest
* branch dev -> FETCH_HEAD
Already up to date.
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ vim demo2.py
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ ls
demo2.py index.html readme.txt
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git add .
warning: LF will be replaced by CRLF in demo2.py.
The file will have its original line endings in your working directory
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git commit -m 'homenewfunc'
[dev 17139e4] homenewfunc
1 file changed, 1 insertion(+)
create mode 100644 demo2.py
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/projecttest (dev)
$ git push origin dev
fatal: HttpRequestException encountered.
▒▒▒▒▒▒▒▒ʱ▒▒▒▒
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 334 bytes | 334.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/corleytd/projecttest.git
3c1ae7e..17139e4 dev -> dev
- 到公司后合并继续开发
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ cat demo2.py
print('hello,newfunc')
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ vim demo.py
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git add .
warning: LF will be replaced by CRLF in demo.py.
The file will have its original line endings in your working directory
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git commit -m 'fantastic'
[dev c4e8ff1] fantastic
1 file changed, 3 insertions(+), 1 deletion(-)
Lenovo@LAPTOP-61GNF3CH MINGW64 /e/testincompany/projecttest (dev)
$ git push origin dev
Logon failed, use ctrl+c to cancel basic credential prompt.
error: unable to read askpass response from 'E:/Git/mingw64/libexec/git-core/git-gui--askpass'
Username for 'https://github.com': corleytd
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 945 bytes | 472.00 KiB/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/corleytd/projecttest.git
17139e4..c4e8ff1 dev -> dev