版权声明:学习smartli的git文档后整理得到本文
特点
- 版本控制
- 分布式
安装与配置
sudo apt-get install git # 安装
git # 查看常见命令
创建一个版本库
# 在当前目录下创建一个版本库
# 在当前目录下创建了一个.git隐藏目录,这就是版本库目录
git init
(dj_py3) python@ubuntu:~/Desktop/demo_dj$ cd gitdemo/
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git init
初始化空的 Git 仓库于 /home/python/Desktop/demo_dj/gitdemo/.git/
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ ls -a
. .. .git
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$
版本库创建与回退
使用
创建文件
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
创建一个版本
git add code.txt
git commit –m '版本1'
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '版本1'
[master (根提交) 0949bfc] 版本1
1 file changed, 1 insertion(+)
create mode 100644 code.txt
查看版本记录
git log # 查看版本记录
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git log
commit 0949bfc72577ee628bff2bf5c9c802f747b240ca
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 02:16:38 2018 +0800
版本1
增加一行
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
创建第二个版本并查看版本记录
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '版本2'
[master b8b2eb0] 版本2
1 file changed, 2 insertions(+)
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git log
commit b8b2eb0e5819704adf088ad22ac60178b218f99c
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 02:19:34 2018 +0800
版本2
commit 0949bfc72577ee628bff2bf5c9c802f747b240ca
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 02:16:38 2018 +0800
版本1
版本回退和误删处理
git reset --hard HEAD^ # 版本回退一个版本
git reset --hard HEAD^^ # 版本回退两个版本
# 版本回退后文件内容回退,git log中也看不到当前版本之后的记录
# 版本回退后可按版本编号(git reflog查看操作记录可得到历史版本编号)切换回回退前的版本
git reset --hard 版本编号 # 版本切换到指定版本
工作区与缓存区
工作区(Working Directory)
文件夹,即目录
版本库(Repository)
工作区中的一个隐藏目录.git
.git目录下的文件 | 作用 |
---|---|
stage(或者叫index) | 暂存区 |
master | git自动创建的第一个分支 |
HEAD | 指向master的一个指针 |
创建git版本库时,git自动创建了唯一一个master分支,所以,git commit就是往master分支上提交更改。
需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。
git add 文件 # 将文件修改提交到暂存区
git commit -m 版本名 # 将暂存区的所有内容提交到当前分支
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code2.txt
the code2 first line
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: code.txt
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: code.txt
新文件: code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '版本3'
[master a30372c] 版本3
2 files changed, 2 insertions(+)
create mode 100644 code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git log
commit a30372cf15115fbe4807412f0fe385814bd027c9
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 02:42:01 2018 +0800
版本3
commit b8b2eb0e5819704adf088ad22ac60178b218f99c
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 02:19:34 2018 +0800
版本2
commit 0949bfc72577ee628bff2bf5c9c802f747b240ca
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 02:16:38 2018 +0800
版本1
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
无文件要提交,干净的工作区
管理修改
git管理的文件的修改只会提交暂存区的修改来创建版本。
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
the new line
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '版本4'
[master cefc25d] 版本4
1 file changed, 1 insertion(+)
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: code.txt
撤销修改
git reset HEAD 文件 # 取消暂存
git checkout -- 文件 # 丢弃工作区的改动
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -- code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
无文件要提交,干净的工作区
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
the new line
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git reset HEAD code.txt
重置后取消暂存的变更:
M code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: code.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -- code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
无文件要提交,干净的工作区
小结:
- 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout – file。
- 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改:
- 第一步用命令git reset HEAD file,就回到了场景1;
- 第二步按场景1操作。
- 场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节。
对比文件的不同
对比工作区和某个版本中文件的不同
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
the new line
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git diff HEAD -- code.txt
diff --git a/code.txt b/code.txt
index d173be1..d94059d 100644
--- a/code.txt
+++ b/code.txt
@@ -2,4 +2,4 @@ this is the first line
this is the second line
this is the third line
this is the forth tline
-
+the new line
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -- code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
无文件要提交,干净的工作区
对比两个版本文件的不同
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git diff HEAD HEAD^ -- code.txt
diff --git a/code.txt b/code.txt
index d173be1..3571907 100644
--- a/code.txt
+++ b/code.txt
@@ -1,5 +1,4 @@
this is the first line
this is the second line
this is the third line
-this is the forth tline
删除文件
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ rm code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add/rm <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
删除: code2.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
恢复
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -- code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ ls
code2.txt code.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
无文件要提交,干净的工作区
删除
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git rm code2.txt
rm 'code2.txt'
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
删除: code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '删除code2.txt'
[master c366b38] 删除code2.txt
1 file changed, 1 deletion(-)
delete mode 100644 code2.txt
(dj_py3) python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 master
无文件要提交,干净的工作区
分支管理
概念
可以选择合并的平行宇宙
创建与合并分支
HEAD指向当前分支、master分支,master分支指向提交。
- 一开始的时候,master分支是一条线,git用master指向最新的提交,再用HEAD指向master,就能确定当前分支,以及当前分支的提交点;每次提交,master分支都会向前移动一步,这样,随着不断提交,master分支的线也越来越长。
- 当我们创建新的分支,例如dev时,git新建了一个指针叫dev,指向master相同的提交,再把HEAD指向dev,就表示当前分支在dev上;git创建一个分支很快,因为除了增加一个dev指针,改变HEAD的指向,工作区的文件都没有任何变化。
- 不过,从现在开始,对工作区的修改和提交就是针对dev分支了,比如新提交一次后,dev指针往前移动一步,而master指针不变。
- 假如dev上的工作完成了,就可以把dev合并到master上。git怎么合并呢?最简单的方法,就是直接把master指向dev的当前提交,就完成了合并,git合并分支也很快,就改改指针,工作区内容也不变。
- 合并完分支后,甚至可以删除dev分支。删除dev分支就是把dev指针给删掉,删掉后,我们就剩下了一条master分支。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch # 查看当前有几个分支、在哪个分支下工作
* master
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -b dev # 创建分支dev并切换到dev分支上进行工作
切换到一个新分支 'dev'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch
* dev
master
# 在dev分支修改code.txt并提交
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m 'dev分支提交'
[dev f303ec5] dev分支提交
1 file changed, 1 insertion(+), 1 deletion(-)
# 切换回master分支,查看code.txt,发现添加的内容没有了
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout master
切换到分支 'master'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch
dev
* master
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
# git merge 合并指定分支到当前分支
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git merge dev
# Fast-forword 是git告诉我们这次合并是快进模式,即直接把master指向dev的当前提交
更新 c366b38..f303ec5
Fast-forward
code.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
# 合并成功,删除dev分支
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch -d dev
已删除分支 dev(曾为 f303ec5)。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch
* master
小结
小结:
git branch # 查看分支
git branch <name> # 创建分支
git checkout <name> # 切换分支
git checkout -b <name> # 创建+切换分支
git merge <name> # 合并某分支到当前分支
git branch -d <name> # 删除分支
解决冲突
# 在dev2中添加新的一行
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -b dev2
切换到一个新分支 'dev2'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch
* dev2
master
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
add one more line in dev2
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m 'dev2提交'
[dev2 6f2e1ef] dev2提交
1 file changed, 1 insertion(+)
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout master
切换到分支 'master'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
add a new line in master
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m 'master提交'
[master 7c94ed0] master提交
1 file changed, 1 insertion(+)
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git merge dev2
自动合并 code.txt
冲突(内容):合并冲突于 code.txt
自动合并失败,修正冲突然后提交修正的结果。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
<<<<<<< HEAD
add a new line in master
=======
add one more line in dev2
>>>>>>> dev2
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt # 修改后提交
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '解决冲突'
[master 0ff41ec] 解决冲突
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
add a new line in master
add one more line in dev2
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git log
commit 0ff41ec0df7a0abfb9a7fb5f77b7c8390d0bc507
Merge: 7c94ed0 6f2e1ef
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 17:53:29 2018 +0800
解决冲突
commit 7c94ed0520a5323f2c99734cacd2f72ae16bdbd1
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 17:46:21 2018 +0800
master提交
commit 6f2e1ef92124f82cb3406c7717cb08e5f71b15fa
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 17:44:30 2018 +0800
dev2提交
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch -d dev2
已删除分支 dev2(曾为 6f2e1ef)。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch
* master
分支管理策略
合并分支时,git通常会用fast forward模式,但是有些快速合并不能成而且合并时没有冲突,这个时候会合并之后并做一次新的提交。但这种模式下,删除分支后,会丢掉分支信息。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -b dev
切换到一个新分支 'dev'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code3.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code3.txt
code3 line
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code3.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '创建code3.txt'
[dev f9ac44b] 创建code3.txt
1 file changed, 1 insertion(+)
create mode 100644 code3.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout master
切换到分支 'master'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
add a new line in master
add one more line in dev2
创建code3.txt后添加的新行
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '添加新行'
[master 8b8acc0] 添加新行
1 file changed, 1 insertion(+)
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git merge dev
# 这次不能进行快速合并,git提示输入合并说明信息,输入之后合并之后git会自动创建一次新的提交
GNU nano 2.5.3 文件: /home/python/Desktop/demo_dj/gitdemo/.git/MERGE_MSG
Merge branch 'dev' # 自动生成的版本名,可以修改
# 请输入一个提交信息以解释此合并的必要性,尤其是将一个更新后的上游分支
# 合并到主题分支。
#
# 以 '#' 开头的行将被忽略,而且空提交说明将会终止提交。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git log
commit 91634fcd7a850a33a9c0bee82b11c956fd9a3274
Merge: 8b8acc0 f9ac44b
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 18:04:40 2018 +0800
Merge branch 'dev'
commit 8b8acc08a0b31bb336c907fafeb6e46f0bc85736
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 18:04:01 2018 +0800
添加新行
commit f9ac44b28b1d0b2b8ab1c695c4a30e26cdc5964f
Author: xwp <xwp_fullstack@163.com>
Date: Fri Jan 5 18:01:59 2018 +0800
创建code3.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch -d dev
已删除分支 dev(曾为 f9ac44b)。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch
* master
Bug分支
每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除。
1. 当你接到一个修复一个代号001的bug的任务时,很自然地,你想创建一个分支bug-001来修复它,但是,等等,当前正在dev上进行的工作还没有提交:
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line
add a new line in master
add one more line in dev2
创建code3.txt后添加的新行
修改bug前在dev分支上正在进行的工作
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 dev
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: code.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
并不是你不想提交,而是工作只进行到一半,还没法提交,预计完成还需1天时间。但是,必须在两个小时内修复该bug,怎么办?
2. git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作:
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git stash
Saved working directory and index state WIP on dev: 91634fc Merge branch 'dev'
HEAD 现在位于 91634fc Merge branch 'dev'
3.首先确定要在哪个分支上修复bug,假定需要在master分支上修复,就从master创建临时分支:
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout master
切换到分支 'master'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout -b bug001
切换到一个新分支 'bug001'
4.现在修复bug,把 add a new line删掉,然后提交。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ vi code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ cat code.txt
this is the first line
this is the second line
this is the third line
this is the forth tline
add a new line in master
add one more line in dev2
创建code3.txt后添加的新行
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '修复bug001'
[bug001 a48be43] 修复bug001
1 file changed, 1 deletion(-)
5.修复完成后,切换到master分支,并完成合并,最后删除bug-001分支。
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout master
切换到分支 'master'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git merge --no-ff -m '修复bug001' bug001
Merge made by the 'recursive' strategy.
code.txt | 1 -
1 file changed, 1 deletion(-)
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git branch -d bug001
已删除分支 bug001(曾为 a48be43)。
6.现在bug-001修复完成,接着回到dev分支干活
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git checkout dev
切换到分支 'dev'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 dev
无文件要提交,干净的工作区
7.查看与恢复stash内容
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git stash list
stash@{0}: WIP on dev: 91634fc Merge branch 'dev'
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git stash pop
位于分支 dev
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: code.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
丢弃了 refs/stash@{0} (ab20846d819a4fcce1986681f84affd33b473ec5)
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git status
位于分支 dev
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: code.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git add code.txt
python@ubuntu:~/Desktop/demo_dj/gitdemo$ git commit -m '修改bug001后回dev上进行的工作'
[dev 752e2ac] 修改bug001后回dev上进行的工作
1 file changed, 1 insertion(+)
小结:
修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;
当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,恢复工作现场。
使用github
创建仓库
(1)注册github账户,登录后,点击”New respository ”
(2)在新页面中,输入项目的名称,勾选’readme.md’,点击’create repository’
(3)添加成功后,转到文件列表页面.
添加ssh账户
(1)点击账户头像后的下拉三角,选择’settings’
如果某台机器需要与github上的仓库交互,那么就要把这台机器的ssh公钥添加到这个github账户上
点击’SSH and GPG keys’,添加ssh公钥。
(2)在ubuntu的命令行中,回到用户的主目录下,编辑文件.gitconfig,修改某台机器的git配置。
(3)修改为注册github时的邮箱,填写用户名。
vi .gitconfig
(4)使用如下命令生成ssh密钥。
ssh-keygen -t rsa -C "邮箱地址"
(5)进入主目录下的.ssh文件件,下面有两个文件。
公钥为id_rsa.pub
私钥为id_rsa
查看公钥内容,复制此内容
cd ~/.ssh
cat id_rsa.pub
(6)回到浏览器中,填写标题,粘贴公钥
克隆项目
(1)在浏览器中点击进入github首页,再进入项目仓库的页面
(2)复制git地址
(3)克隆出错
git clone git@github.com:<name>/<project.git>
# 出错后执行如下命令
eval "$(ssh-agent -s)"
ssh-add
git clone git@github.com:<name>/<project.git>
(4)在命令行中复制仓库中的内容
git clone git@github.com:<name>/<project.git>
上传分支
(1)项目克隆到本地之后,执行如下命令创建分支smart.
(2)创建一个code.txt并提交一个版本。
(3)推送分支,就是把该分支上的所有本地提交推送到远程库,推送时要指定本地分支,这样,git就会把该分支推送到远程库对应的远程分支上
git push origin <branch_name>
将本地分支跟踪服务器分支
git branch --set-upstream-to=origin/远程分支名称 本地分支名称
从远程分支上拉取代码
把远程分支smart上的代码下载并合并到本地所在分支。
git pull orgin 分支名称
生产使用git
项目经理:
(1) 项目经理搭建项目的框架。
(2) 搭建完项目框架之后,项目经理把项目框架代码放到服务器。
普通员工:
(1) 在自己的电脑上,生成ssh公钥,然后把公钥给项目经理,项目经理把它添加的服务器上面。
(2) 项目经理会给每个组员的项目代码的地址,组员把代码下载到自己的电脑上。
(3) 创建本地的分支dev,在dev分支中进行每天的开发。
(4) 每一个员工开发完自己的代码之后,都需要将代码发布远程的dev分支上。
Master:用户保存发布的项目代码。V1.0,V2.0
Dev:保存开发过程中的代码。