一、上堂回顾
1.概念:
集中式【SVN】和分布式【git】
工作区,暂存区和版本库
2.git的常用命令【本地版本库】
git安装:sudo apt-get install git
初始化版本库:git init
将修改添加到暂存区:git add filename
将暂存区中的内容提交到版本库:git commit -m “提交日志”
查看仓库的状态:git status
查看仓库中具体修改的内容:git diff
版本回退:
a.回到上一个版本:git reset --hard HEAD^
b.回到某个指定的版本:git reset --hard commitID【5~8位】
撤销文件:
a.丢弃工作区的修改:git checkout – filename
b.取消暂存区:
git reset HEAD filename
git checkout – filename
c.取消版本库的提交: git reset --hard HEAD^
查看git的日志:
git log和git log --pretty=oneline【确定回到过去的某个版本】
git reflog【确定回到未来的某个版本】
删除文件
a.删除工作区中的文件:rm -rf filename
b.删除版本库中的文件:git rm filename
3.git的常用命令【远程版本库】
建立github和电脑之间的连接:添加ssh key
已知本地仓库,建立远程仓库:
建立本地仓库和远程仓库之间的连接:git remote add origin [email protected]:git的账户名/远程项目名称.git
将本地仓库中的内容推送到远程仓库:git push -u origin master
说明:第一次推送,添加-u选项,为了建立本地仓库的master和远程仓库origin中的master分支
已知远程仓库,建立本地仓库:git clone [email protected]:git的账户名/远程项目名称.git
如果对内容作了修改,要推送到远程仓库:git push origin master
二、git的使用
1.分支管理
默认只有一个master分支,默认HEAD指针指向master分支上的每一次commit
建议:在master分支上尽量只发布版本,不要在master分支上进行开发
实际项目开发中,如果是团队开发,每个人尽量各自有一个单独的分支;每个不同的功能尽量也可以拥有一个单独的分支;如果要单独解决某个bug,也可以单独添加一个分支
1.1创建分支和合并分支
查看所有分支:git branch 创建分支并切换到该分支:git checkout -b 子分支名称 切换到主分支:git checkout master 将子分支合并到master:git merge 子分支名称 删除子分支:git branch -d 子分支名称【子分支已经合并到了master】 git branch -D 子分支名称【子分支未合并到master,强制删除】 演示命令: wyq@Wyq:~$ cd 桌面 wyq@Wyq:~/桌面$ cd python1807/ wyq@Wyq:~/桌面/python1807$ ls clonegit1807 learngit1807 wyq@Wyq:~/桌面/python1807$ cd clonegit1807/ wyq@Wyq:~/桌面/python1807/clonegit1807$ ls a1.txt README.md wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout -b user1 切换到一个新分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch master * user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支与上游分支 'origin/master' 一致。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支与上游分支 'origin/master' 一致。 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout user1 切换到分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch master * user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ vim a1 wyq@Wyq:~/桌面/python1807/clonegit1807$ vim a1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add a1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "modify a1" [user1 f66f280] modify a1 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支与上游分支 'origin/master' 一致。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支与上游分支 'origin/master' 一致。 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ cat a1.txt hello wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge user1 更新 6645051..f66f280 Fast-forward a1.txt | 1 + 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ cat a1.txt hello abc wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout user1 切换到分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支领先 'origin/master' 共 1 个提交。 (使用 "git push" 来发布您的本地提交) wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch -d user1 已删除分支 user1(曾为 f66f280)。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout -b user2 切换到一个新分支 'user2' wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch master * user2 wyq@Wyq:~/桌面/python1807/clonegit1807$ vim a1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add a1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "123" [user2 b88c816] 123 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支领先 'origin/master' 共 1 个提交。 (使用 "git push" 来发布您的本地提交) wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch -d user2 error: 分支 'user2' 没有完全合并。 如果您确认要删除它,执行 'git branch -D user2'。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge user2 更新 f66f280..b88c816 Fast-forward a1.txt | 1 + 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch -d user2 已删除分支 user2(曾为 b88c816)。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支领先 'origin/master' 共 2 个提交。 (使用 "git push" 来发布您的本地提交) 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ git push origin master 对象计数中: 6, 完成. 压缩对象中: 100% (4/4), 完成. 写入对象中: 100% (6/6), 488 bytes | 81.00 KiB/s, 完成. Total 6 (delta 0), reused 0 (delta 0) To github.com:yangyang-git/clonegit1807.git 6645051..b88c816 master -> master
1.2解决冲突
注意:一旦文件中出现冲突,一定要先解决冲突,然后再提交,最后再推送到远程,尽量让冲突出现在本地仓库
在子分支和主分支中同时修改了同一个文件,将子分支合并到master的时候,会出现冲突,解决办法如下:
a.如果在子分支中解决冲突,进行add和commit,回到主分支中,需要再次merge
b.如果在master中解决冲突,直接进行add和commit
git log --graph:查看分支合并图,绿色线条表示正在工作的分支
演示命令: wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout -b feature1 切换到一个新分支 'feature1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * feature1 master wyq@Wyq:~/桌面/python1807/clonegit1807$ touch file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ vim file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "file1" [feature1 d47079e] file1 1 file changed, 1 insertion(+) create mode 100644 file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支与上游分支 'origin/master' 一致。 wyq@Wyq:~/桌面/python1807/clonegit1807$ ls a1.txt README.md wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge feature1 更新 b88c816..d47079e Fast-forward file1.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ ls a1.txt file1.txt README.md wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout feature1 切换到分支 'feature1' wyq@Wyq:~/桌面/python1807/clonegit1807$ vim file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "modify~~~1" [feature1 ce5cb24] modify~~~1 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支领先 'origin/master' 共 1 个提交。 (使用 "git push" 来发布您的本地提交) wyq@Wyq:~/桌面/python1807/clonegit1807$ cat file1.txt new file wyq@Wyq:~/桌面/python1807/clonegit1807$ vim file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "modify~~~~master" [master a62a0ff] modify~~~~master 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge feature1 #合并时出现冲突 自动合并 file1.txt 冲突(内容):合并冲突于 file1.txt 自动合并失败,修正冲突然后提交修正的结果。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支领先 'origin/master' 共 2 个提交。 (使用 "git push" 来发布您的本地提交) 您有尚未合并的路径。 (解决冲突并运行 "git commit") (使用 "git merge --abort" 终止合并) 未合并的路径: (使用 "git add <文件>..." 标记解决方案) 双方修改: file1.txt 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") wyq@Wyq:~/桌面/python1807/clonegit1807$ vim file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支领先 'origin/master' 共 2 个提交。 (使用 "git push" 来发布您的本地提交) 您有尚未合并的路径。 (解决冲突并运行 "git commit") (使用 "git merge --abort" 终止合并) 未合并的路径: (使用 "git add <文件>..." 标记解决方案) 双方修改: file1.txt 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") wyq@Wyq:~/桌面/python1807/clonegit1807$ git add file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "modify conflict" [master ea2ae42] modify conflict wyq@Wyq:~/桌面/python1807/clonegit1807$ cat file1.txt new file master~~~~modify feature1~~~~~modify wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout feature1 切换到分支 'feature1' wyq@Wyq:~/桌面/python1807/clonegit1807$ cat file1.txt new file feature1~~~~~modify wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支领先 'origin/master' 共 4 个提交。 (使用 "git push" 来发布您的本地提交) wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch -d feature1 已删除分支 feature1(曾为 ce5cb24)。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支领先 'origin/master' 共 4 个提交。 (使用 "git push" 来发布您的本地提交) 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ git push origin master 对象计数中: 12, 完成. 压缩对象中: 100% (8/8), 完成. 写入对象中: 100% (12/12), 941 bytes | 941.00 KiB/s, 完成. Total 12 (delta 3), reused 0 (delta 0) remote: Resolving deltas: 100% (3/3), done. To github.com:yangyang-git/clonegit1807.git b88c816..ea2ae42 master -> master wyq@Wyq:~/桌面/python1807/clonegit1807$ git log --graph * commit ea2ae427115bbba2038583c77d08d1561c28e3ee (HEAD -> master, origin/master, origin/HEAD) |\ Merge: a62a0ff ce5cb24 | | Author: lm <lm> | | Date: Fri Mar 1 10:28:08 2019 +0800 | | | | modify conflict | | | * commit ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 | | Author: lm <lm> | | Date: Fri Mar 1 10:20:17 2019 +0800 | | | | modify~~~1 | | * | commit a62a0ffe05be06509c466782625935c5643abe96 |/ Author: lm <lm> | Date: Fri Mar 1 10:21:49 2019 +0800 | | modify~~~~master | * commit d47079ed8c34320e0db22c6f763c99d6289eeaf2 | Author: lm <lm> | Date: Fri Mar 1 10:18:50 2019 +0800 | | file1 | * commit ea2ae427115bbba2038583c77d08d1561c28e3ee (HEAD -> master, origin/mast er, origin/HEAD) |\ Merge: a62a0ff ce5cb24 | | Author: lm <lm> | | Date: Fri Mar 1 10:28:08 2019 +0800 | | | | modify conflict | | | * commit ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 | | Author: lm <lm> | | Date: Fri Mar 1 10:20:17 2019 +0800 | | | | modify~~~1 | | * | commit a62a0ffe05be06509c466782625935c5643abe96 |/ Author: lm <lm> | Date: Fri Mar 1 10:21:49 2019 +0800 | | modify~~~~master | * commit d47079ed8c34320e0db22c6f763c99d6289eeaf2 | Author: lm <lm> | Date: Fri Mar 1 10:18:50 2019 +0800 | | file1
1.3分支管理策略
合并分支的时候,默认情况下git使用Fast-forward模式,如果使用了该模式,则如果删除了分支,在分支合并图上将会丢失分支信息
一般情况下,为了保留分支信息,强制禁用Fast-forward模式,如果删除分支,则会产生一个commit的历史记录
以前:git merge 子分支名称
现在:git merge --no-ff -m “提交日志” 子分支名称
演示命令: wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout -b user1 切换到一个新分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch master * user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ ls a1.txt file1.txt README.md wyq@Wyq:~/桌面/python1807/clonegit1807$ vim a1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add a1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "delete 123" [user1 a74527c] delete 123 1 file changed, 1 deletion(-) wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支与上游分支 'origin/master' 一致。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge user1 更新 ea2ae42..a74527c Fast-forward a1.txt | 1 - 1 file changed, 1 deletion(-) wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支领先 'origin/master' 共 1 个提交。 (使用 "git push" 来发布您的本地提交) 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch -d user1 已删除分支 user1(曾为 a74527c)。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git log --graph --pretty=oneline * a74527cced8fca4e73bc46d64e5cf1568fe267de (HEAD -> master) delete 123 #使用fast-forward模式,在分支合并图中并未出现分支信息 * ea2ae427115bbba2038583c77d08d1561c28e3ee (origin/master, origin/HEAD) modify conflict |\ | * ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 modify~~~1 * | a62a0ffe05be06509c466782625935c5643abe96 modify~~~~master |/ * d47079ed8c34320e0db22c6f763c99d6289eeaf2 file1 * b88c8164af2cedb56023b50366ccf6bfb3b0403a 123 * f66f2803062d6ee658a67d3358b89d2acb402f50 modify a1 * 6645051d912e33ed4b93a225194a01bbefdc0139 a1 * 5581c0aa6f40d17fa29a0d6467a37ebf77584895 Initial commit wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout -b user1 切换到一个新分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ vim file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "file1~~~~" [user1 ed7e8b2] file1~~~~ 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge --no-ff -m "merge user1" user1 Merge made by the 'recursive' strategy. file1.txt | 1 + 1 file changed, 1 insertion(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ cat file1.txt new file master~~~~modify feature1~~~~~modify jarhgajhrg wyq@Wyq:~/桌面/python1807/clonegit1807$ git log --graph --pretty=oneline * cd0f120796ea96cdaea3cafbebe1e12c68ffab94 (HEAD -> master) merge user1 |\ | * ed7e8b28bcb78d77d91c1cc7ed0b3399f5440ab4 (user1) file1~~~~ #禁用了fast-forard模式之后,在分支合并图中出现了分支信息 |/ * a74527cced8fca4e73bc46d64e5cf1568fe267de delete 123 * ea2ae427115bbba2038583c77d08d1561c28e3ee (origin/master, origin/HEAD) modify conflict |\ | * ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 modify~~~1 * | a62a0ffe05be06509c466782625935c5643abe96 modify~~~~master |/ * d47079ed8c34320e0db22c6f763c99d6289eeaf2 file1 * b88c8164af2cedb56023b50366ccf6bfb3b0403a 123 * f66f2803062d6ee658a67d3358b89d2acb402f50 modify a1 * 6645051d912e33ed4b93a225194a01bbefdc0139 a1 * 5581c0aa6f40d17fa29a0d6467a37ebf77584895 Initial commit wyq@Wyq:~/桌面/python1807/clonegit1807$
1.4bug分支
git stash:可以将当前的工作现场封存起来,等其他的工作完成之后恢复现场继续工作
git stash pop:解除封存
git stash list:查看封存的工作现场
演示命令: wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout user1 切换到分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git stash list stash@{0}: WIP on user1: ed7e8b2 file1~~~~ wyq@Wyq:~/桌面/python1807/clonegit1807$ git stash pop 位于分支 user1 尚未暂存以备提交的变更: (使用 "git add <文件>..." 更新要提交的内容) (使用 "git checkout -- <文件>..." 丢弃工作区的改动) 修改: file1.txt 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") 丢弃了 refs/stash@{0} (6edd4209bf476bac8b7250a0e613f5632423b5af) wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 user1 尚未暂存以备提交的变更: (使用 "git add <文件>..." 更新要提交的内容) (使用 "git checkout -- <文件>..." 丢弃工作区的改动) 修改: file1.txt 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") wyq@Wyq:~/桌面/python1807/clonegit1807$ git stash list wyq@Wyq:~/桌面/python1807/clonegit1807$ git add file1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "gahjgrh" [user1 ca637e5] gahjgrh 1 file changed, 1 deletion(-) wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 user1 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout master 切换到分支 'master' 您的分支领先 'origin/master' 共 5 个提交。 (使用 "git push" 来发布您的本地提交) wyq@Wyq:~/桌面/python1807/clonegit1807$ git merge --no-ff -m "user1" user1 Merge made by the 'recursive' strategy. file1.txt | 1 - 1 file changed, 1 deletion(-) wyq@Wyq:~/桌面/python1807/clonegit1807$ git status 位于分支 master 您的分支领先 'origin/master' 共 7 个提交。 (使用 "git push" 来发布您的本地提交) 无文件要提交,干净的工作区 wyq@Wyq:~/桌面/python1807/clonegit1807$ git push origin master 对象计数中: 12, 完成. 压缩对象中: 100% (11/11), 完成. 写入对象中: 100% (12/12), 1.09 KiB | 1.09 MiB/s, 完成. Total 12 (delta 4), reused 0 (delta 0) remote: Resolving deltas: 100% (4/4), completed with 1 local object. To github.com:yangyang-git/clonegit1807.git ea2ae42..2afc83a master -> master
1.5团队开发
在团队开发中,不但要将自己的修改推送到远程仓库,而且需要将队友的修改拉取到 本地
推送:将自己的修改-----》远程仓库
抓取:将别人的修改-----》本地仓库
注意:在实际开发中,一般先抓取,然后再推送【目的:尽量将冲突出现在本地】
1》推送分支
推送主分支:git push origin master
推送子分支:git push origin 子分支名称
2》抓取分支:git pull
演示命令: wyq@Wyq:~/桌面/python1807/clonegit1807$ git remote origin wyq@Wyq:~/桌面/python1807/clonegit1807$ git remote -v origin [email protected]:yangyang-git/clonegit1807.git (fetch) origin [email protected]:yangyang-git/clonegit1807.git (push) wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ git push origin master Everything up-to-date wyq@Wyq:~/桌面/python1807/clonegit1807$ git push origin user1 Total 0 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'user1' on GitHub by visiting: remote: https://github.com/yangyang-git/clonegit1807/pull/new/user1 remote: To github.com:yangyang-git/clonegit1807.git * [new branch] user1 -> user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ cd .. wyq@Wyq:~/桌面/python1807$ cd .. wyq@Wyq:~/桌面$ mkdir other wyq@Wyq:~/桌面$ cd other/ wyq@Wyq:~/桌面/other$ git clone [email protected]:yangyang-git/clonegit1807.git #新建一个目录,模拟团队开发中的另一个人 正克隆到 'clonegit1807'... remote: Enumerating objects: 35, done. remote: Counting objects: 100% (35/35), done. remote: Compressing objects: 100% (19/19), done. remote: Total 35 (delta 9), reused 30 (delta 7), pack-reused 0 接收对象中: 100% (35/35), 完成. 处理 delta 中: 100% (9/9), 完成. wyq@Wyq:~/桌面/other$ ls clonegit1807 wyq@Wyq:~/桌面/other$ cd clonegit1807/ wyq@Wyq:~/桌面/other/clonegit1807$ ls a1.txt file1.txt README.md wyq@Wyq:~/桌面/other/clonegit1807$ git branch * master wyq@Wyq:~/桌面/other/clonegit1807$ git checkout -b user1 origin/user1 分支 'user1' 设置为跟踪来自 'origin' 的远程分支 'user1'。 切换到一个新分支 'user1' wyq@Wyq:~/桌面/other/clonegit1807$ git branch master * user1 wyq@Wyq:~/桌面/other/clonegit1807$ touch b1.txt wyq@Wyq:~/桌面/other/clonegit1807$ git add b1.txt wyq@Wyq:~/桌面/other/clonegit1807$ git commit -m "b1" [user1 02efa42] b1 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 b1.txt wyq@Wyq:~/桌面/other/clonegit1807$ git push origin user1 对象计数中: 3, 完成. 压缩对象中: 100% (2/2), 完成. 写入对象中: 100% (3/3), 301 bytes | 301.00 KiB/s, 完成. Total 3 (delta 0), reused 0 (delta 0) To github.com:yangyang-git/clonegit1807.git ca637e5..02efa42 user1 -> user1 wyq@Wyq:~/桌面/other/clonegit1807$ cd .. wyq@Wyq:~/桌面/other$ cd .. wyq@Wyq:~/桌面$ cd python1807/ wyq@Wyq:~/桌面/python1807$ cd clonegit1807/ wyq@Wyq:~/桌面/python1807/clonegit1807$ ls a1.txt file1.txt README.md wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch * master user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ git chekcout user1 git:'chekcout' 不是一个 git 命令。参见 'git --help'。 最相似的命令是 checkout wyq@Wyq:~/桌面/python1807/clonegit1807$ git checkout user1 切换到分支 'user1' wyq@Wyq:~/桌面/python1807/clonegit1807$ git pull remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 展开对象中: 100% (3/3), 完成. 来自 github.com:yangyang-git/clonegit1807 ca637e5..02efa42 user1 -> origin/user1 当前分支没有跟踪信息。 请指定您要合并哪一个分支。 详见 git-pull(1)。 git pull <远程> <分支> 如果您想要为此分支创建跟踪信息,您可以执行: git branch --set-upstream-to=origin/<分支> user1 #报错:原因是没有指定本地子分支和远程子分支之间的连接 wyq@Wyq:~/桌面/python1807/clonegit1807$ git branch --set-upstream-to=origin/user1 user1 #执行本地子分支和远程子分支之间的连接 分支 'user1' 设置为跟踪来自 'origin' 的远程分支 'user1'。 wyq@Wyq:~/桌面/python1807/clonegit1807$ git pull 更新 ca637e5..02efa42 Fast-forward b1.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 b1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ ls a1.txt b1.txt file1.txt README.md wyq@Wyq:~/桌面/python1807/clonegit1807$ vim b1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git add b1.txt wyq@Wyq:~/桌面/python1807/clonegit1807$ git commit -m "b1" [user1 48e4794] b1 1 file changed, 3 insertions(+) wyq@Wyq:~/桌面/python1807/clonegit1807$ git push origin user1 对象计数中: 3, 完成. 压缩对象中: 100% (2/2), 完成. 写入对象中: 100% (3/3), 251 bytes | 251.00 KiB/s, 完成. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To github.com:yangyang-git/clonegit1807.git 02efa42..48e4794 user1 -> user1 wyq@Wyq:~/桌面/python1807/clonegit1807$ cd ../.. wyq@Wyq:~/桌面$ cd other/ wyq@Wyq:~/桌面/other$ cd clonegit1807/ wyq@Wyq:~/桌面/other/clonegit1807$ ls a1.txt b1.txt file1.txt README.md wyq@Wyq:~/桌面/other/clonegit1807$ cat b1.txt wyq@Wyq:~/桌面/other/clonegit1807$ git branch master * user1 wyq@Wyq:~/桌面/other/clonegit1807$ git pull remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 展开对象中: 100% (3/3), 完成. 来自 github.com:yangyang-git/clonegit1807 02efa42..48e4794 user1 -> origin/user1 更新 02efa42..48e4794 Fast-forward b1.txt | 3 +++ 1 file changed, 3 insertions(+) wyq@Wyq:~/桌面/other/clonegit1807$ cat b1.txt hfajhgja ghajwrhga hgjahgj #注意:团队开发的过程中,很大可能会产生冲突,如果出现冲突,同样需要手动解决冲突,处理办法和【解决冲突】中的方式完全相同。冲突被解决后,再次提交commit,然后再推送到远程【push】
2.标签管理
在实际开发中,标签是可选的
回退到某个版本,通过commitID可以找到指定的版本
标签:tag,是版本库的快照,一般是发布版本的时候标签管理,注意:tag指向某个指定的commit
git tag 标签名称:在当前的commit做标签
git tag:查看当前版本库中所有的标签
git tag 标签名称 commitId:给指定的commit添加标签
git show 标签名称:显示某个标签的具体信息
git push origin --tags :将本地版本库中的标签推送到远程仓库
git tag -d xxx;删除本地仓库中的标签
git push oritin :refs/tags/xxxx:删除远程仓库中的标签
演示命令: wyq@Wyq:~/桌面/other/clonegit1807$ git branch master * user1 wyq@Wyq:~/桌面/other/clonegit1807$ git log --graph --pretty=oneline * 48e4794996ffd33818343e94a0aeaafff99c0e5e (HEAD -> user1, origin/user1) b1 * 02efa42b362ef4b4fefc0b34739b9fc1a839da9d b1 * ca637e57bc4062432d7885c820d733ace8099270 gahjgrh * ed7e8b28bcb78d77d91c1cc7ed0b3399f5440ab4 file1~~~~ * a74527cced8fca4e73bc46d64e5cf1568fe267de delete 123 * ea2ae427115bbba2038583c77d08d1561c28e3ee modify conflict |\ | * ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 modify~~~1 * | a62a0ffe05be06509c466782625935c5643abe96 modify~~~~master |/ * d47079ed8c34320e0db22c6f763c99d6289eeaf2 file1 * b88c8164af2cedb56023b50366ccf6bfb3b0403a 123 * f66f2803062d6ee658a67d3358b89d2acb402f50 modify a1 * 6645051d912e33ed4b93a225194a01bbefdc0139 a1 * 5581c0aa6f40d17fa29a0d6467a37ebf77584895 Initial commit wyq@Wyq:~/桌面/other/clonegit1807$ git tag v2.0 wyq@Wyq:~/桌面/other/clonegit1807$ git tag v2.0 wyq@Wyq:~/桌面/other/clonegit1807$ git log --graph --pretty=oneline * 48e4794996ffd33818343e94a0aeaafff99c0e5e (HEAD -> user1, tag: v2.0, origin/user1) b1 * 02efa42b362ef4b4fefc0b34739b9fc1a839da9d b1 * ca637e57bc4062432d7885c820d733ace8099270 gahjgrh * ed7e8b28bcb78d77d91c1cc7ed0b3399f5440ab4 file1~~~~ * a74527cced8fca4e73bc46d64e5cf1568fe267de delete 123 * ea2ae427115bbba2038583c77d08d1561c28e3ee modify conflict |\ | * ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 modify~~~1 * | a62a0ffe05be06509c466782625935c5643abe96 modify~~~~master |/ * d47079ed8c34320e0db22c6f763c99d6289eeaf2 file1 * b88c8164af2cedb56023b50366ccf6bfb3b0403a 123 * f66f2803062d6ee658a67d3358b89d2acb402f50 modify a1 * 6645051d912e33ed4b93a225194a01bbefdc0139 a1 * 5581c0aa6f40d17fa29a0d6467a37ebf77584895 Initial commit wyq@Wyq:~/桌面/other/clonegit1807$ git tag v1.0 d47079ed8 wyq@Wyq:~/桌面/other/clonegit1807$ git log --graph --pretty=oneline * 48e4794996ffd33818343e94a0aeaafff99c0e5e (HEAD -> user1, tag: v2.0, origin/user1) b1 * 02efa42b362ef4b4fefc0b34739b9fc1a839da9d b1 * ca637e57bc4062432d7885c820d733ace8099270 gahjgrh * ed7e8b28bcb78d77d91c1cc7ed0b3399f5440ab4 file1~~~~ * a74527cced8fca4e73bc46d64e5cf1568fe267de delete 123 * ea2ae427115bbba2038583c77d08d1561c28e3ee modify conflict |\ | * ce5cb2493217cdb9d786d1b6459d4a9778ac3e34 modify~~~1 * | a62a0ffe05be06509c466782625935c5643abe96 modify~~~~master |/ * d47079ed8c34320e0db22c6f763c99d6289eeaf2 (tag: v1.0) file1 * b88c8164af2cedb56023b50366ccf6bfb3b0403a 123 * f66f2803062d6ee658a67d3358b89d2acb402f50 modify a1 * 6645051d912e33ed4b93a225194a01bbefdc0139 a1 * 5581c0aa6f40d17fa29a0d6467a37ebf77584895 Initial commit wyq@Wyq:~/桌面/other/clonegit1807$ git tag v1.0 v2.0 wyq@Wyq:~/桌面/other/clonegit1807$ git show v1.0 commit d47079ed8c34320e0db22c6f763c99d6289eeaf2 (tag: v1.0) Author: lm <lm> Date: Fri Mar 1 10:18:50 2019 +0800 file1 diff --git a/file1.txt b/file1.txt new file mode 100644 index 0000000..fa49b07 --- /dev/null +++ b/file1.txt @@ -0,0 +1 @@ +new file wyq@Wyq:~/桌面/other/clonegit1807$ git push origin --tags Total 0 (delta 0), reused 0 (delta 0) To github.com:yangyang-git/clonegit1807.git * [new tag] v1.0 -> v1.0 * [new tag] v2.0 -> v2.0 wyq@Wyq:~/桌面/other/clonegit1807$ git tag -d v1.0 已删除标签 'v1.0'(曾为 d47079e) wyq@Wyq:~/桌面/other/clonegit1807$ git tag v2.0 wyq@Wyq:~/桌面/other/clonegit1807$ git push origin :refs/tags/v1.0 To github.com:yangyang-git/clonegit1807.git - [deleted] v1.0
三、shell编程
1.什么是shell
alias .bashrc
把在终端下可执行的命令保存到一个本地文件中,该文件就被称为shell程序
shell编程就是对一堆linux命令的逻辑化处理
/bin/bash,bash简单易用和免费,应用比较广泛,bash是大多数Linux发布版本的默认shell
ubuntu下默认的shell解析器是bash wyq@Wyq:~/桌面/other/clonegit1807$ echo "hello" hello wyq@Wyq:~/桌面/other/clonegit1807$ echo $SHELL /bin/bash
2.第一个shell程序
#!/bin/bash echo "hello world"
3.运行shell
wyq@Wyq:~/桌面/shell$ ll 总用量 12 drwxr-xr-x 2 wyq wyq 4096 3月 1 15:43 ./ drwxr-xr-x 8 wyq wyq 4096 3月 1 15:40 ../ -rw-r--r-- 1 wyq wyq 31 3月 1 15:43 test.sh #方式一:修改文件权限 wyq@Wyq:~/桌面/shell$ chmod +x ./test.sh wyq@Wyq:~/桌面/shell$ ll 总用量 12 drwxr-xr-x 2 wyq wyq 4096 3月 1 15:43 ./ drwxr-xr-x 8 wyq wyq 4096 3月 1 15:40 ../ -rwxr-xr-x 1 wyq wyq 31 3月 1 15:43 test.sh* wyq@Wyq:~/桌面/shell$ test.sh test.sh:未找到命令 wyq@Wyq:~/桌面/shell$ ./test.sh hello world #方式二 wyq@Wyq:~/桌面/shell$ /bin/bash test.sh hello world #方式三 wyq@Wyq:~/桌面/shell$ sh test.sh hello world #方式四 chmod +x ./test.sh wyq@Wyq:~/桌面/shell$ ll
4.基本语法
4.1变量
格式:变量名=值
#!/bin/bash name="zhangsan" echo "zhangsan" echo $name num=10 echo ${num} echo "his name is ${name}" #只读变量 url="www.baidu.com" readonly url #url="hello" #删除变量 age=18 unset age echo ${age}
4.2字符串和数组
字符串可以使用单引号,双引号,也可以使用反引号
注意:反引号可以将其中的内容按照命令执行
#!/bin/bash name1="my name is" name2="jack" echo $name1 $name2 #获取字符串的长度 echo ${#name1} #字符串截取 echo ${name1:1:4} #获取子字符串在原字符串中的索引 echo `expr index "$name1" is`
数组:
#!/bin/bash arr1=(10 20 30 40) echo $arr1 arr2=( 10 20 30 40 ) echo $arr2 #读取数组中的元素 echo ${arr1[2]} echo ${arr1[@]} echo ${#arr1} echo ${#arr1[@]} echo ${#arr1[*]}
4.3运算符
#!/bin/bash echo "expr 2 + 2" echo `expr 2 + 2` echo `expr 2 \* 4` file='/home/wyq/桌面/shell/string.sh' if [ -r $file ] then echo "可读的" else echo "不可读" fi
关系运算符:
-eq =
-lt <
-gt >
逻辑运算符
&& || !
文件测试运算符
4.4语句
echo ,printf,test命令
if语句,for语句,while语句,until语句
#!/bin/bash a=10 b=20 if [ $a -eq $b ] then echo "yes" fi if [ $a -eq $b ] then echo "yes" else echo "no" fi if [ $a -eq $b ] then echo "yes" elif [ $a -lt $b ] then echo "111" else echo "222" fi read num case $num in 1) echo "1" ;; 2) echo "2" ;; *) echo "no" ;; esac
#!/bin.bash for num in 1 2 3 4 5 do echo $num done n=1 while [ $n -lt 5 ] do echo $n #let "n++" n=$[$n+1] done m=1 until [ $m -lt 10 ] do echo $m ((m++)) done
4.5函数
#!/bin/bash test() { echo "hello" } test check() { echo "first num:" read num1 echo "second num:" read num2 echo "qiu he" return $(($num1+$num2)) } check echo "和:$?" arg() { echo $1 echo $2 return 100 } arg 3 4 echo "返回:$?"