No.1 Git
特点
- 版本控制:可以解决多人同时开发的代码问题,也可以找回历史代码
- 分布式:Git是一个分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上,首先会有一台计算机充当服务器,这台计算机7*24小时服务,其他计算机都是从这台计算机克隆一份代码到自己的计算机中,并且各自把各自的修改提交到服务器仓库里,也可以从服务器仓库拉取别人的提交
安装
sudo apt-get install git -y
No.2 版本创建与回退
使用
创建一个版本库
git init
创建一个版本
git add test.txt
git commit -m 'v1.0'
查看工作区状态
get status
查看版本记录
git log
git log --pretty=oneline 将每次提交放到一行显示
git log --pretty=oneline --graph 以图形化方式显示每次提交
版本回退
git reset --hard HEAD^ 其中HEAD表示当前最新版本,一个^表示回退一个版本,有几个^表示回退几个版本
git reset --hard HEAD~1 1表示回退一个版本,那么10就是回退10个版本
git reset --hard HEAD
查看操作记录
git reflog
工作区、暂存区、版本库
- 计算机中的目录就是一个工作区
- 工作区中有一个隐藏目录.git,就是版本库,版本库中存在了许多东西,其中最重要的是stage(暂存区),还有一个是git为我们自动创建的第一个分支master,以及指向master的HEAD指针,因此第一个分支是master,所以当我们执行git commit的时候是向master分支上推送,git add 文件是将修改后的文件放到暂存区,git commit是将暂存区中的数据提交到工作区
管理修改
echo 'this is first line' >> test.txt
git add test.txt
echo 'this is first line' >> test.txt
git commit -m 'v1.1'
git status # 发现第二次修改test.txt文件后,并没有真正的加入到工作区,所以并没有提交到版本库
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
撤销修改
撤销对test.txt文件的修改
get checkout -- test.txt
如果要撤销暂存区的修改时
git reset HEAD test.txt
对比文件
对比工作区中的test.txt文件和版本库中的有什么不同
git diff test.txt HEAD -- test.txt
对比两个版本中test.txt文件的不同
git diff HEAD HEAD^ -- test.txt
删除文件
我们将目录中的test.txt删除,git知道我们删除了文件,但是工作区和版本库就不知道了,所以我们执行git status就可以知道哪些文件被删除了
rm -rf test.txt
git status
现在我们有两个选择,一是确定这个文件要被删除,二是确定这个文件误删
# 1.
git rm test.txt
git commit
# 2.
git checkout -- test.txt
No.4 分支管理
创建与合并
git把我们每次提交的版本形成一条时间线,这个时间线就是一个分支,目前,这个分支叫master,每次提交后HEAD指向master,master指向提交,每次提交,master都会向前移动一步,所以这条时间线越来越长,当我们创建分支slave后,HEAD指向slave,指向新提交,从此,再次提交就会将版本提交到slave分支,而master不变,当我们在salve分支上的工作完成了,就可以合并分支
- 查看分支 git branch
- 创建并切换分支 git checkout -b
- 切换分支 git checkout
- 合并分支 git merge
- 删除分支 git branch -d
解决冲突
合并也不是一帆风顺的,比如说,我在工作区中有一个test.txt文件,这个文件中没有任何数据,我在master分支中向第一行增加了一行数据,提交到版本库中,然后切换到slave,在这个文件中新增加了一行数据,然后提交到版本库中,再进行合并就会出现冲突
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git checkout -b slave
Switched to a new branch 'slave'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ cat test.txt
master:the new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ vi test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ cat test.txt
master:the new code
slave:the new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git add test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git commit -m '1.2'
[slave 5035bfb] 1.2
1 file changed, 1 insertion(+)
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git checkout master
Switched to branch 'master'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ cat test.txt
master:the new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ vi test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ cat test.txt
master:the new code
master:the new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git add test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git commit -m '1.3'
[master e46f9b7] 1.3
1 file changed, 2 insertions(+)
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git merge slave
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
# 解决冲突
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master|MERGING)
$ cat test.txt
master:the new code
<<<<<<< HEAD
master:the new code
=======
slave:the new code
>>>>>>> slave
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master|MERGING)
$ vi test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master|MERGING)
$ cat test.txt
master:the new code
slave:the new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master|MERGING)
$ git add test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master|MERGING)
$ git commit -m '1.4'
[master b252477] 1.4
分支管理策略
通常,合并分支时,git会尽可能的使用fast forward模式,但是有时候快速合并没有成功但是也没有冲突,这个时候会合并之后做一次新的提交
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ vi test1.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ cat test1.txt
add new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git add test1.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git commit -m '1.5'
[slave 5fa46e2] 1.5
1 file changed, 1 insertion(+)
create mode 100644 test1.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git checkout master
Switched to branch 'master'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ vi test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ cat test.txt
master:the new code
slave:the new code
master:add new code
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git merge slave
Merge made by the 'recursive' strategy.
test1.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 test1.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git log --pretty=oneline
bee1e463867f605f16c3fbad6a50ea36f4e0b20a (HEAD -> master) 合并slave分支
5fa46e2ec62346526554b04b62a468afbde0f9ba (slave) 1.5
b2524777899baab52783e1b49360fedc1e6a7e50 1.4
e46f9b7b5c379238ddca5db70746bc4f9f8c5edc 1.3
5035bfba43172b02bdbb7447fd9ebb018e6f49f5 1.2
2ae2bcac4258f41cb1a1e3a3a81a572be80c28f9 1.1
8d3dee945a68e5996d9c088a9769f43fc5c75228 1.0
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git log --pretty=oneline --graph
* bee1e463867f605f16c3fbad6a50ea36f4e0b20a (HEAD -> master) 合并slave分支
|\
| * 5fa46e2ec62346526554b04b62a468afbde0f9ba (slave) 1.5
* | b2524777899baab52783e1b49360fedc1e6a7e50 1.4
|\ \
| |/
| * 5035bfba43172b02bdbb7447fd9ebb018e6f49f5 1.2
* | e46f9b7b5c379238ddca5db70746bc4f9f8c5edc 1.3
|/
* 2ae2bcac4258f41cb1a1e3a3a81a572be80c28f9 1.1
* 8d3dee945a68e5996d9c088a9769f43fc5c75228 1.0
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git branch -d slave
Deleted branch slave (was 5fa46e2).
但是快速合并分支后删除分支会丢失掉分支信息,所以我们很多时候需要禁用快速合并
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git checkout -b slave
Switched to a new branch 'slave'
M test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ vi test2.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git add test2.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git commit -m '1.6'
[slave 1e833e6] 1.6
1 file changed, 2 insertions(+)
create mode 100644 test2.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git checkout master
Switched to branch 'master'
M test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git merge --no-ff -m '禁用快速合并' slave
Merge made by the 'recursive' strategy.
test2.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 test2.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git log --graph --pretty=oneline
* cabeb0f4edc6617d27e1a7624be927a0f5f7da59 (HEAD -> master) 禁用快速合并
|\
| * 1e833e62109f3dd266883834bf11014d85c4a3dc (slave) 1.6
|/
* bee1e463867f605f16c3fbad6a50ea36f4e0b20a 合并slave分支
|\
| * 5fa46e2ec62346526554b04b62a468afbde0f9ba 1.5
* | b2524777899baab52783e1b49360fedc1e6a7e50 1.4
|\ \
| |/
| * 5035bfba43172b02bdbb7447fd9ebb018e6f49f5 1.2
* | e46f9b7b5c379238ddca5db70746bc4f9f8c5edc 1.3
|/
* 2ae2bcac4258f41cb1a1e3a3a81a572be80c28f9 1.1
* 8d3dee945a68e5996d9c088a9769f43fc5c75228 1.0
bug分支
软件开发中,出现bug是经常性的事情,在git中,每个bug可以通过一个临时分支来修复,修复后删除分支,假如突然出现一个bug就需要立即修复,但是该分支的工作还没有完成,没有完成就没办法进行提交,所以就用到了git中的保存现场功能(stash)
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git stash
Saved working directory and index state WIP on master: cabeb0f 禁用快速合并
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git status
On branch master
nothing to commit, working tree clean
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git checkout slave
Switched to branch 'slave'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git checkout -b bug001
Switched to a new branch 'bug001'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (bug001)
$ vi test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (bug001)
$ git add test.txt
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (bug001)
$ git commit -m 'bug001修复完成'
[bug001 aab106b] bug001修复完成
1 file changed, 1 deletion(-)
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (bug001)
$ git checkout slave
Switched to branch 'master'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git merge bug001
Merge made by the 'recursive' strategy.
test.txt | 1 -
1 file changed, 1 deletion(-)
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git branch -d bug001
Deleted branch bug001 (was aab106b).
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (slave)
$ git checkout master
Switched to branch 'master'
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git stash list
stash@{0}: WIP on master: cabeb0f 禁用快速合并
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git stash pop
gOn branch slave
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8fe9c203f75afc7929433faabc52e02a6d555ff4)
kernel@DESKTOP-67P59AO MINGW64 /d/Codes/Git/test (master)
$ git status
On branch slave
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
No.5 使用GitHub
添加ssh账户
如果某台机器需要与GitHub进行交互,那么就要把这台机器的ssh公钥添加到GitHub账户上
-
编辑.github文件,使用ssh-keygen -t rsa -C "邮箱地址",生成ssh密钥
- 点击账户头像后的下拉三角,选择settings,选择SSH and GPG keys选项,单击New SSH keys,复制id_rsa.pub文件里的内容,点击Add SSH key
克隆项目
git clone 项目地址
上传分支
git push origin 分支名称
跟踪远程分支
git branch --set-upstream-to=origin/slave master
当本地成功跟踪远程分支后,当修改代码后,只需要git push就可以将修改提交到远程
从远程分支拉取代码
git pull origin 分支名称