流程图:html
1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端
请求头和请求体中会包含浏览器的动做(action),这个动做一般为get或者post,体如今url之中.git
2. url通过Django中的wsgi,再通过Django的中间件,最后url到过路由映射表,在路由中一条一条进行匹配,
一旦其中一条匹配成功就执行对应的视图函数,后面的路由就再也不继续匹配了.
3. 视图函数根据客户端的请求查询相应的数据.返回给Django,而后Django把客户端想要的数据作为一个字符串返回给客户端.
4. 客户端浏览器接收到返回的数据,通过渲染后显示给用户.程序员
wsgi 是web服务网关接口,它是一套协议。本质经过socket实现的服务端!它不是django特有的,它同时只能处理一个请求github
uwsgi 能同时处理多个请求web
uWSGI是一个Web应用服务器,它具备应用服务器,代理,进程管理及应用监控等功能。它支持WSGI协议,同时它也支持自有的uWSGI协议,该协议听说性能很是高,并且内存占用率低,为mod_wsgi的一半左右,我没有实测过。它还支持多应用的管理及应用的性能监控。虽然uWSGI自己就能够直接用来当Web服务器,但通常建议将其做为应用服务器配合Nginx一块儿使用,这样能够更好的发挥Nginx在Web端的强大功能。面试
它通常和Nginx配合使用,请参考文档sql
http://www.py3study.com/Article/details/id/323.html数据库
FBV(function base views) 就是在视图里使用函数处理请求。django
看代码:vim
views.py
from django.shortcuts import render def index(request): if request.method == 'POST': print('method is :' + request.method) elif request.method == 'GET': print('method is :' + request.method) return render(request, 'index.html')
注意此处定义的是函数【def index(request):】
上面就是FBV的使用。
CBV(class base views) 就是在视图里使用类处理请求。
将上述代码中的views.py 修改成以下:
from django.views import View class Index(View): def get(self, request): print('method is :' + request.method) return render(request, 'index.html') def post(self, request): print('method is :' + request.method) return render(request, 'index.html')
注:类要继承 View ,类中函数名必须小写。
两种方式没有优劣,均可以使用。
ORM单表操做,比较经常使用的,有13个!
create delete update filter/all exclude values values_list get first last order_by only defer
上面列举的,必需要会。面试可能会考到!
13个具体API用法,请参考连接:
http://www.javashuo.com/article/p-ypzeiwrw-g.html
面试题:查询书籍表中,id不等于5的记录
obj = Book.objects.exclude(id=100)
补充2个命令
通常查询整个表,使用下面的语法
user_list = models.User.objects.all()
它返回一个queryset对象,格式为queryset = [obj,obj,obj]
至关于执行SQL: select id,name,pwd from user;
举例:查询全部id和name
user_list = models.User.objects.all().only('id','name')
它也是返回一个queryset对象,至关于执行SQL: select id,name from user
举例:排除pwd之外的字段
user_list = models.User.objects.all().defer('pwd')
它也是返回一个queryset对象,至关于执行SQL: select id,name from user
Git 是一个开源的分布式版本控制软件,用以有效、高速的处理从很小到很是大的项目版本管理。 Git 最初是由Linus Torvalds设计开发的,用于管理Linux内核开发。Git 是根据GNU通用公共许可证版本2的条款分发的自由/免费软件,安装参见:http://git-scm.com/
GitHub是一个基于Git的远程文件托管平台(同GitCafe、BitBucket和GitLab等)。
Git自己彻底能够作到版本控制,但其全部内容以及版本记录只能保存在本机,若是想要将文件内容以及版本记录同时保存在远程,则须要结合GitHub来使用。使用场景:
其余:
集中式:远程服务器保存全部版本,用户客户端有某个版本
分布式:远程服务器保存全部版本,用户客户端有全部版本
说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文必定出现过这番景象!
毕业论文_初稿.doc
毕业论文_修改1.doc
毕业论文_修改2.doc
毕业论文_修改3.doc
毕业论文_完整版1.doc
毕业论文_完整版2.doc
毕业论文_完整版3.doc
毕业论文_最终版1.doc
毕业论文_最终版2.doc
毕业论文_死也不改版.doc
以上就是使用最原始的方式进行版本控制,可是这种方式有显著缺点:
为了解决以上版本控制存在问题,应运而生了一批版本控制工具:VSS、CVS、SVN、Git等,其中Git属于绝对霸主地位。
注意:通常版本控制工具包含两部分
小P是一个年轻有为程序员,从小立志要干出一番大事,某个深夜小P在网上查找**老师主演的学习视频,花了1个小时才找到想要的资源,小P想到和本身同样的有为青年天天花费大量的时间寻找喜欢老师的做品,感受本身干大事的机会来了,毅然决然选择创业,建立一个**平台,提供**老师的全部资源!!!
创业初期,小P独自封闭开发一个月,第一个版本终于上线:
回顾开发过程,其中辛酸只有小P本身知道。上线完成后的某一天,小P猛然看到本身开发目录,卧槽这拓麻也太乱了,加入那天程序出问题回滚到上个版本的时候,本身都找不到肯定版本,而且我老子作的这个系统往后是要成千上万人来维护开发,这种经过原始文件来保存版本的形式简直Low到爆啊。
开始调研:小P发现了版本控制神奇Git,可是都是道听途说,到底牛逼成什么样子也不清楚,因此抱着试试看的态度,小P开始使用Git进行版本控制。
MacBook-Pro-4:pondo wupeiqi$ pwd # 进入程序目录 /Users/wupeiqi/PycharmProjects/pondo MacBook-Pro-4:pondo wupeiqi$ git init # git初始化 Initialized empty Git repository in /Users/wupeiqi/PycharmProjects/pondo/.git/
初始化后,会在当前目录自动建立 .git 文件夹,该文件是Git中最重要的文件夹,由于Git相关文件以及版本都将保存在该文件夹中,有了它,妈妈不再用担忧我好多文件来记录版本了,经过Git命令能够将全部版本保存在 .git 文件中,两条命令建立一个版本:
MacBook-Pro-4:pondo wupeiqi$ git status # 查看当前git状态 On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) .idea/ app01/ db.sqlite3 manage.py pondo/ readme templates/ nothing added to commit but untracked files present (use "git add" to track) MacBook-Pro-4:pondo wupeiqi$ git add . # 添加当前目录下全部文件到版本库 MacBook-Pro-4:pondo wupeiqi$ git commit -m '第一次提交' # 提交到版本库,并填写版本说明,以便之后回滚。 [master (root-commit) df47fe4] 第一次提交 33 files changed, 879 insertions(+) create mode 100644 .idea/dictionaries/wupeiqi.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml ...
注意:执行git commit 命令时,可能会提示进行用户和邮箱的配置,该配置用于记录当前版本由那个用户提交
Git把管理的文件分为了两个区域四个状态。
工做区:当前开发程序所在目录称为工做区,即:工做开发都是在该目录,该区域的文件会有状态的变化且状态由git自动检测,若是程序中文件作任何操做(增、删、改),文件状态均会被检测到,可使用 【git status】命令查看。
MacBook-Pro-4:pondo wupeiqi$ ls # 查看原程序目录 app01 db.sqlite3 manage.py pondo readme static templates MacBook-Pro-4:pondo wupeiqi$ git status # 查看git当前状态 On branch master nothing to commit, working tree clean MacBook-Pro-4:pondo wupeiqi$ touch a.py # 建立新文件 MacBook-Pro-4:pondo wupeiqi$ ls a.py app01 db.sqlite3 manage.py pondo readme static templates MacBook-Pro-4:pondo wupeiqi$ git status # 查看git当前状态,检测到:工做区 a.py 发生变化 On branch master Untracked files: (use "git add <file>..." to include in what will be committed) a.py nothing added to commit but untracked files present (use "git add" to track)
版本库:工做区检测到有文件发生变化,那么意味着较上一个版本以后对程序进行了修改,修改完成以后,能够当作下一版本进行提交,那么就是执行 【git add .】 将全部文件提交到暂存区,而后再执行【git commit -m '又一个版本'】提交到版本库的分支便可,以后可使用【git log】命令查看版本记录。
MacBook-Pro-4:pondo wupeiqi$ ls a.py app01 db.sqlite3 manage.py pondo readme static templates MacBook-Pro-4:pondo wupeiqi$ git status # 文件颜色为红色,表示在工做区的被修改状态 On branch master Untracked files: (use "git add <file>..." to include in what will be committed) a.py nothing added to commit but untracked files present (use "git add" to track) MacBook-Pro-4:pondo wupeiqi$ git add . # 将全部相较上一次版本以后全部的修改添加到暂存状态 MacBook-Pro-4:pondo wupeiqi$ git status # 文件颜色为绿色,表示在版本库的暂存状态 On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: a.py MacBook-Pro-4:pondo wupeiqi$ git commit -m '又一次提交' # 提交到版本库的分支 [master f139d5d] 又一次提交 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 a.py MacBook-Pro-4:pondo wupeiqi$ git log # 查看历史版本提交记录(根据版本commit值能够进行回滚) commit f139d5d0a648af06d8a1ecadd90faf572afc388a Author: 武沛齐 <you@example.com> Date: Fri Aug 11 10:02:14 2017 +0800 又一次提交 commit df47fe49fc1f14d9cdd1534baa96f46ec71a9934 Author: 武沛齐 <you@example.com> Date: Fri Aug 11 08:49:49 2017 +0800 第一次提交
目前已使用Git的四个命令,这四个命令已经能够代替本地多个文件保存版本的方式:
调研完,小P好气本身哟,这么6的东西为何没有早发现,今后小P的版本管理就告别繁杂的文件夹了,赶忙搞起来。
MacBook-Pro-4:pondo wupeiqi$ ls app01 db.sqlite3 manage.py pondo static templates MacBook-Pro-4:pondo wupeiqi$ git init Initialized empty Git repository in /Users/wupeiqi/PycharmProjects/pondo/.git/ MacBook-Pro-4:pondo wupeiqi$ git config --local user.name '武沛齐' MacBook-Pro-4:pondo wupeiqi$ git config --local user.email 'wupeiqi@live.com' MacBook-Pro-4:pondo wupeiqi$ git add . MacBook-Pro-4:pondo wupeiqi$ git commit -m '项目首次移植到Git控制版本' [master (root-commit) 6c439d2] 项目首次移植到Git控制版本 32 files changed, 870 insertions(+) create mode 100644 .idea/dictionaries/wupeiqi.xml create mode 100644 .idea/encodings.xml ...
刚好,此时须要开发一个非洲专区的功能,不再用从新copy一遍文件了,在工做区直接开始搞起来,30分钟开发测试完成,又一个版本完成了咯!!!
MacBook-Pro-4:pondo wupeiqi$ git status # 非洲专区功能的开发,仅对app01/views.py进行了修改 On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo wupeiqi$ git add . MacBook-Pro-4:pondo wupeiqi$ git commit -m '非洲专区上线' [master 0972f4b] 非洲专区上线 1 file changed, 3 insertions(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$
非洲专区上线一个月后,接连收到用户投诉,原来清新脱俗的小P那里去了?怎么变得如此重口味?想回到过去....
小P向来秉承为人民服务的原则,人民不想看那必定要修改。决定:回滚,回到上一个版本。
那么问题来了?
一个月过去了,代码修改的位置早就忘记了,怎么修改,总不能再开发一遍吧。机智的小P猜测Git既然这么牛逼,应该会提供这样的功能,通过一番查找,果不其然Git提供了这个回滚的功能。
回滚到指定版本:
MacBook-Pro-4:pondo wupeiqi$ git log commit 0972f4bb43104baee15aeec2dd62bd0a307ec837 Author: 武沛齐 <wupeiqi@live.com> Date: Fri Aug 11 10:54:42 2017 +0800 非洲专区上线 commit 6c439d2fd0d943f36f3ee84e158ff86b052961d2 Author: 武沛齐 <wupeiqi@live.com> Date: Fri Aug 11 10:42:09 2017 +0800 项目首次移植到Git控制版本 MacBook-Pro-4:pondo wupeiqi$ git reset --hard 6c439d2fd0d943f36f3ee84e158ff86b052961d2 HEAD is now at 6c439d2 项目首次移植到Git控制版本 # 命令执行完,工做区的全部文件就变成未开发非洲专区功能以前了,太爽了有么有....
回滚却是完成了,小P在想若是某一天想要在回有非洲专区功能的版本怎么办呢?来来来,不能像以往经过【git log】来查看记录再回滚了,再回去须要这么搞:
MacBook-Pro-4:pondo wupeiqi$ git reflog 6c439d2 HEAD@{2}: reset: moving to 6c439d2fd0d943f36f3ee84e158ff86b052961d2 0972f4b HEAD@{3}: commit: 非洲专区上线 6c439d2 HEAD@{4}: commit (initial): 项目首次移植到Git控制版本 MacBook-Pro-4:pondo wupeiqi$ git reset --hard 0972f4b HEAD is now at 0972f4b 非洲专区上线 Git使用之小P创业史:成长期
企业想要不被淘汰,就要跟紧时代步伐,近日直播行业日趋火热,小P的也但愿本身的平台加入直播功能,已经评估预计2个月开发完成,小P开始没日没夜的干了起来...
一个月过去了,开发任务和按照预期有条不紊的进行着,直播功能也已完成一半,就是在此时线上运行平台出现Bug须要紧急修复,怎么办?怎么办??怎么办???
小P出了几个解决方案:
方案一:stash
stash用于将工做区发生变化的全部文件获取临时存储在“某个地方”,将工做区还原当前版本未操做前的状态;stash还能够将临时存储在“某个地方”的文件再次拿回到工做区。
acBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 开发直播功能,刚开发到一半 MacBook-Pro-4:pondo wupeiqi$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo wupeiqi$ git stash # 将开发到一半的直播功能,临时存储到“某个地方” Saved working directory and index state WIP on master: 0972f4b 非洲专区上线 HEAD is now at 0972f4b 非洲专区上线 MacBook-Pro-4:pondo wupeiqi$ git status # 工做区回到当前版本未作任何操做前 On branch master nothing to commit, working tree clean MacBook-Pro-4:pondo wupeiqi$ vim pondo/settings.py # 紧急修复bug MacBook-Pro-4:pondo wupeiqi$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: pondo/settings.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo wupeiqi$ git add . # 添加到修改bug的代码到暂存状态 MacBook-Pro-4:pondo wupeiqi$ git commit -m '紧急修复bug' # 提交修复Bug的代码到分支 [master 1300d33] 紧急修复bug 1 file changed, 1 insertion(+) MacBook-Pro-4:pondo wupeiqi$ git stash pop # 将开发到一半的直播功能从“某个地方”再次拿会工做区继续开发 On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (059d78ca8fa204f9559bd3ce0ae76235969b4301)
特别的:执行 git stash pop 命令时,可能会遇到冲突,由于在紧急修复bug的代码和经过stash存储在“某个地方”的代码会有重合部分,因此执行 git stash pop 时候就会出现冲突,有冲突解决冲突便可。
git stash pop 出现冲突
a. 原来内容: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('非洲专区') b. 开发到一半直播功能: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('非洲专区') def live(request): print('开发到一半') return HttpResponse('....') c. 执行git stash,回到当前版本未修改状态: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('非洲专区') d. 修复Bug并提交: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('非洲xxxxx专区') e. 继续开发直播功能 git stash pop,此时会出现冲突: MacBook-Pro-4:pondo wupeiqi$ git stash pop Auto-merging app01/views.py CONFLICT (content): Merge conflict in app01/views.py 表示app01/views.py存在冲突须要解决,此时文件内容为: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): <<<<<<< Updated upstream: # 修复Bug时更改的内容 return HttpResponse('非洲xxxx区') ======= # 修复Bug前正在开发新功能时的内容 return HttpResponse('非洲专区') def live(request): print('刚开发到一半') return HttpResponse('直播功能') >>>>>>> Stashed changes 须要自行解决冲突,而后继续开发,如: from django.shortcuts import render,HttpResponse def index(request): return render(request,'index.html') def africa(request): return HttpResponse('非洲xxxx区') def live(request): print('刚开发到一半') return HttpResponse('直播功能') git stash pop 出现冲突
stash相关经常使用命令:
方案二:branch
分支学习:branch称为分支,默认仅有一个名为master的分支。通常开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支。
通常流程示例(上图)
MacBook-Pro-4:pondo wupeiqi$ git branch dev # 建立新分支,即:拷贝一份当前所在分支代码到新分支 MacBook-Pro-4:pondo wupeiqi$ git checkout dev # 切换到dev分支 MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 开发功能 MacBook-Pro-4:pondo wupeiqi$ git status # 查看状态,即:在dev分支修改了app01/views.py文件 On branch dev Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: app01/views.py no changes added to commit (use "git add" and/or "git commit -a") MacBook-Pro-4:pondo wupeiqi$ git add . # 将修改文件添加到版本库的暂存区 MacBook-Pro-4:pondo wupeiqi$ git commit -m '新功能开发完毕' # 将暂存区的内容提交到当前所在分支,即:dev分支 [dev 32b40cd] 新功能开发完毕 file changed, 2 insertions(+) MacBook-Pro-4:pondo wupeiqi$ git checkout master # 切换回master分支 Switched to branch 'master' MacBook-Pro-4:pondo wupeiqi$ git merge dev # 将dev分支内容合并到master分支 Updating 0972f4b..32b40cd Fast-forward app01/views.py | 2 ++ file changed, 2 insertions(+) 通常流程示例(上图)
学习参考上图,小P也能够按照着这样的流程进行开发,若是遇到上文开发到通常须要临时修复Bug的状况,能够按照下图的流程进行:
MacBook-Pro-4:pondo wupeiqi$ git branch # 当前在master分支 * master MacBook-Pro-4:pondo wupeiqi$ git branch dev # 建立dev分支用于开发新功能 MacBook-Pro-4:pondo wupeiqi$ git checkout dev # 切换到dev分支 Switched to branch 'dev' MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 开发新功能到一半,须要紧急修复Bug MacBook-Pro-4:pondo wupeiqi$ git add . MacBook-Pro-4:pondo wupeiqi$ git commit -m '新功能开发一半' [dev b3ac2cb] 新功能开发一半 1 file changed, 2 insertions(+) MacBook-Pro-4:pondo wupeiqi$ git checkout master # 切换回master分支 Switched to branch 'master' MacBook-Pro-4:pondo wupeiqi$ git branch bug # 建立bug分支 MacBook-Pro-4:pondo wupeiqi$ git checkout bug # 切换到bug分支 Switched to branch 'bug' MacBook-Pro-4:pondo wupeiqi$ vim pondo/settings.py # 修改bug MacBook-Pro-4:pondo wupeiqi$ git add . # 提交bug MacBook-Pro-4:pondo wupeiqi$ git commit -m '紧急修复bug' # 提交bug [bug f42f386] 紧急修复bug 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$ git checkout master # 切换会master Switched to branch 'master' MacBook-Pro-4:pondo wupeiqi$ git merge bug # 将bug分支内容合并到master分支,表示bug修复完毕,能够上线 Updating 0972f4b..f42f386 Fast-forward pondo/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$ git checkout dev # 切换到dev分支,继续开发新功能 Switched to branch 'dev' MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 继续开发其余一半功能 MacBook-Pro-4:pondo wupeiqi$ git add . # 提交新功能 MacBook-Pro-4:pondo wupeiqi$ git commit -m '继续开发完成' # 提交功能 [dev c0bfb27] 继续开发完成 1 file changed, 1 insertion(+) MacBook-Pro-4:pondo wupeiqi$ git checkout master # 切换回master分支 Switched to branch 'master' MacBook-Pro-4:pondo wupeiqi$ git merge dev # 将dev分支合并到master分支 Merge made by the 'recursive' strategy. app01/views.py | 3 +++ 1 file changed, 3 insertions(+)
注意:git merge 时也可能会出现冲突,解决冲突的方式上述stash相同,即:找到冲突文件,手动修改冲突并提交,此处再也不敖述。
branch相关经常使用命令:
小P不忘初心始终如一的为广大有为青年提供资源,使得网站的访问量不断攀升,已经出具规模并赚了一些钱,有钱就要造么,索性国贸租了一间写字楼用于办公,而且也完善运营市场团队。。屌丝终归是屌丝,小P仍是离不开写代码的习惯,因此开发的任务仍是由本身一人承担,小P今后开始了白天在国贸写代码,晚上回天通苑写代码。PS:有钱,公司一台新电脑,家里一台原来老电脑。。。。。 妈的,故事怎么才能变得有趣呢?太拓麻难了。
小P内心开始寻思,我爱写代码,公司写,家里写,若是天天来回带一个U盘拷贝着实麻烦,Git有没有相似于云盘似得东西能够进行数据同步呢?答案确定是有。 必须有,否则老子真的就编不下去了。
GitHub,一个基于Git实现的代码托管的平台,能够将内容以及版本记录在远程也保存一份,这样就不用U盘咯(相似于云盘)。PS: 相似GitHub的产品还有许多,如:GitLab、Bitbucket、码云等。
基于GitHub实现代码托管,须要一下步骤:
小P学会使用Git和GitHub以后,就能够基于GitHub进行代码远程托管。
在家里,小P开发完毕部分功能将代码推送到GitHub。
MacBook-Pro-4:pondo wupeiqi$ git remote add origin https://github.com/WuPeiqi/pondo.git # 为地址起一个别名origin MacBook-Pro-4:pondo wupeiqi$ git push origin master # 将本地master分支内容以及版本信息推送到GitHub Username for 'https://github.com': # 输入GitHub用户名 Password for 'https://wupeiqi@github.com': # 输入GitHub密码 Counting objects: 2, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 270 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/WuPeiqi/pondo.git 634aac4..274f1e4 master -> master MacBook-Pro-4:pondo wupeiqi$ git push origin dev # 将本地dev分支内容以及版本信息推送到GitHub Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 261 bytes | 0 bytes/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/WuPeiqi/pondo.git 274f1e4..50e2169 dev -> dev
在公司,新电脑第一次使用,须要将代码从GitHub中获取并继续开发,开发完事下班就下班回家。
MacBook-Pro-4:github wupeiqi$ git clone https://github.com/WuPeiqi/pondo.git # 将项目从GitHub中获取 Cloning into 'pondo'... remote: Counting objects: 31, done. remote: Compressing objects: 100% (26/26), done. remote: Total 31 (delta 2), reused 30 (delta 1), pack-reused 0 Unpacking objects: 100% (31/31), done. MacBook-Pro-4:github wupeiqi$ cd pondo/ MacBook-Pro-4:pondo wupeiqi$ git Branch # 默认获取到得只有master分支 * master MacBook-Pro-4:pondo wupeiqi$ git branch dev origin/dev # 建立dev分支且和远程dev分支同步 Branch dev set up to track remote branch dev from origin. MacBook-Pro-4:pondo wupeiqi$ git checkout dev # 切换到dev分支 Switched to branch 'dev' MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 继续开发新功能 MacBook-Pro-4:pondo wupeiqi$ git add . # 添加文件到版本库的暂存状态 MacBook-Pro-4:pondo wupeiqi$ git commit -m '公司开发功能1' # 提交新功能到版本库的分支 [dev 9281447] 公司开发功能1 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$ git push origin dev # 提交dev分支内容到远程GitHub托管仓库的dev分支 Username for 'https://github.com': wupeiqi Password for 'https://wupeiqi@github.com': Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 427 bytes | 0 bytes/s, done. Total 4 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 2 local objects. To https://github.com/WuPeiqi/pondo.git 50e2169..9281447 dev -> dev
在家里,因为白天在公司已经开发一部分功能并提交到GitHub,家里电脑的代码仍是昨晚的版本,因此须要从GitHub拉去最新代码,而后继续开发。
MacBook-Pro-4:pondo wupeiqi$ git checkout dev # 切换到dev分支 Already on 'dev' MacBook-Pro-4:pondo wupeiqi$ git pull origin dev # 从远程GitHub仓库获取dev分支最新内容,并合并到本地 remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0 Unpacking objects: 100% (4/4), done. From https://github.com/WuPeiqi/pondo * branch dev -> FETCH_HEAD 50e2169..9281447 dev -> origin/dev Updating 50e2169..9281447 Fast-forward app01/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 继续开发新功能 MacBook-Pro-4:pondo wupeiqi$ git add . # 添加文件到版本库的暂存状态 MacBook-Pro-4:pondo wupeiqi$ git commit -m '家里开发功能1' # 提交新功能到版本库的分支
在公司,因为昨天晚上在家已经开发了一部分功能,在公司须要先把昨晚开发的功能从GitHub中拉取,并继续开发。
MacBook-Pro-4:pondo wupeiqi$ git checkout dev # 切换到dev分支 MacBook-Pro-4:pondo wupeiqi$ git fetch origin dev # 从GitHub仓库获取dev分支最新内容到版本库的分支 remote: Counting objects: 3, done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/WuPeiqi/pondo * branch dev -> FETCH_HEAD 150d891..65b6604 dev -> origin/dev MacBook-Pro-4:pondo wupeiqi$ git merge origin/dev # 将版本库的分支内容合并到工做区 Updating 150d891..65b6604 Fast-forward readme | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) MacBook-Pro-4:pondo wupeiqi$ vim app01/views.py # 继续开发新功能 MacBook-Pro-4:pondo wupeiqi$ git add . # 添加文件到版本库的暂存状态 MacBook-Pro-4:pondo wupeiqi$ git commit -m 'xxxxxxxxxxx' # 提交新功能到版本库的分支
久而久之,将Git和GitHub结合使用作到避免电脑损坏形成数据丢失以及多地开发的问题,上文执行过程当中执行 【git pull origin 分支】命令等同于【git fetch origin 分支】+ 【git merge origin/分支】,而且在执行过程当中可能会出现冲突,缘由是因为本地代码和获取的最新代码有重合部分,那么就须要本身手动解决冲忽然后再继续开发。
小P的公司发展愈来愈好,可是公司产品单一是严重缺点,通过学习考察小P决定再招聘3个Python程序开发另一个产品“约P”平台来丰富公司业务线,为用户提供一整套服务。
小P的Slogan:看了想要,想要就约。 不要问我怎么想要的,我本身也不知道 哈哈哈哈哈哈哈哈
“约P”平台须要三人协同开发,GitHub中多人协同开发和单人开发仍是有点差异,协同开发通常有两种方式:
协同开发命令和以上步骤相似,此处就再也不从新写代码,而是使用文件描述三人协同开发整个过程。
在上面红色标注的解决方法位置能够有三种方式操做,三者均可以完成合并并提交新功能,可是日志记录会有差别,如:前二者版本记录中会出现合并,而第三种能够保证版本记录干净整洁。
用户A: touch 4.py git add . git commit -m '功能4' git push origin master 用户B: touch 5.py git add . git commit -m '功能5' git push origin master # 报错,由于GitHub中已经有人提交新代码 git pull origin master git push origin master
用户A: touch 4.py git add . git commit -m '功能4' git push origin master 用户B: touch 5.py git add . git commit -m '功能5' git push origin master # 报错,由于GitHub中已经有人提交新代码 git fetch origin master git rebase origin/master git push origin master
终于终于小P等到了公司上市实现财务自由,但做为一个技术屌仍是脱离不了屌丝的本质,因此天天都是逛逛github,看看别人有什么好的项目,本身能够给他挑挑bug装装逼,可是别人不可能给小P搞成合做者什么的,那怎么才能给别人贡献代码呢?那就是fork了。。。。
1. 配置文件
Git的配置文件有三个:
2. 用户凭证
因为Git和Github交互操做可能会很频繁,那么必定少了用户受权的操做,为了防止每次操做重复输入用户名和密码,Git提供了两种解决方法:
store:
表示将用户名和密码保存在硬盘上
第一次输入过用户名和密码以后,用户名和密码就会保存在当前用户根目录的 .git-credentials 文件中,内容格式为:https://用户名:密码@github.com
自动添加配置命令:git config credential.helper store
cache:
表示将用户名和密码保存在缓存中
第一次输入过用户名和密码以后,用户名和密码就会保存在缓存中,默认超时时间是 900 秒,缓存相关文件保存在当前用户根目录的 git-credential-cache 中
自动添加配置命令:
git config credential.helper cache
git config credential.helper 'cache --timeout=300'
相关操做:
清除缓存:git credential-cache exit
指定超时:
[credential]
helper = cache --timeout=300
注意:
这种方式须要使用GIt中 https://github.com/WuPeiqi/xxxx.git 格式地址。
指定用户名和密码: https://用户名:密码@github.com/wupeiqi/xxx.git
就酱紫,之后想到再加吧...
终于终于小P等到了公司上市实现财务自由,但做为一个技术屌仍是脱离不了屌丝的本质,因此天天都是逛逛github,看看别人有什么好的项目,本身能够给他挑挑bug装装逼,可是别人不可能给小P搞成合做者什么的,那怎么才能给别人贡献代码呢?那就是fork了。。。。
Git的配置文件有三个:
因为Git和Github交互操做可能会很频繁,那么必定少了用户受权的操做,为了防止每次操做重复输入用户名和密码,Git提供了两种解决方法:
store:
表示将用户名和密码保存在硬盘上
第一次输入过用户名和密码以后,用户名和密码就会保存在当前用户根目录的 .git-credentials 文件中,内容格式为:https://用户名:密码@github.com
自动添加配置命令:git config credential.helper store
cache:
表示将用户名和密码保存在缓存中
第一次输入过用户名和密码以后,用户名和密码就会保存在缓存中,默认超时时间是 900 秒,缓存相关文件保存在当前用户根目录的 git-credential-cache 中
自动添加配置命令:
git config credential.helper cache
git config credential.helper 'cache --timeout=300'
相关操做:
清除缓存:git credential-cache exit
指定超时:
[credential]
helper = cache --timeout=300
注意:
这种方式须要使用GIt中 https://github.com/WuPeiqi/xxxx.git 格式地址。
指定用户名和密码: https://用户名:密码@github.com/wupeiqi/xxx.git
就酱紫,之后想到再加吧...
这个是文件用用来作 Git 忽略提交
说明:
有些时候,你必须把某些文件放到Git工做目录中,但又不能提交它们,好比保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files ...,有强迫症的童鞋内心确定不爽。
好在Git考虑到了你们的感觉,这个问题解决起来也很简单,在Git工做区的根目录下建立一个特殊的.gitignore文件,而后把要忽略的文件名填进去,Git就会自动忽略这些文件。
不须要从头写.gitignore文件,GitHub已经为咱们准备了各类配置文件,只须要组合一下就可使用了。全部配置文件能够直接在线浏览:https://github.com/github/gitignore
忽略文件的原则是:
忽略操做系统自动生成的文件,好比缩略图等;
忽略编译生成的中间文件、可执行文件等,也就是若是一个文件是经过另外一个文件自动生成的,那自动生成的文件就不必放进版本库,好比Java编译产生的.class文件;
忽略你本身的带有敏感信息的配置文件,好比存放口令的配置文件。
语法:
以斜杠“/”开头表示目录; 以星号“*”通配多个字符; 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表; 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录; .gitignore
版本相关
git tag -a v1.0 -m '版本介绍' 本地建立Tag git show v1.0 查看 git tags -n 查看本地Tag git tag -l 'v1.4.2.*' 查看本地Tag,模糊匹配 git tag -d v1.0 删除Tag git push origin :refs/tags/v0.2 更新远程tag git checkout v.10 切换tag git fetch origin tag V1.2 git push origin --tags git pull origin --tags git clone -b v0.1 版本相关
git内容部分,参考博客:
http://www.cnblogs.com/wupeiqi/articles/7295372.html
总结:
git作版本管理:本地 github是代码托管仓库:远程 1. 请书写你了解的git命令? 准备: git init git config --global user.email "you@example.com" git config --global user.name "Your Name" git remote add origin https://github.com/ayuchao/bjhot.git git remote add origin https://用户名:密码@github.com/ayuchao/bjhot.git 提交: git add . git commit -m 'xxxxx' git push origin dev 下载: git clone https://github.com/ayuchao/bjhot.git 等价于: 1. 手动建立文件夹bjhot 2. 进入文件夹 3. git init 4. git remote add origin https://用户名:密码@github.com/ayuchao/bjhot.git 5. git pull origin master git pull origin master 合并: git merge 日志回滚: git log git reflog git reset --hard asdfasdfasdfadsfasdfasdf 暂存: git stash git stash pop
做业:
1. 本地提交+回滚 2. 经过分支来模拟:出现bug以后如何解决? 3. 将代码托管到github上 4. 之后:将我的代码非敏感信息上传到github上。
答案:
1. 本地提交+回滚
新建一个文件1.txt,内容为1,执行如下命令
git init git add . git commit -m "1" 修改文件内容为2,再次提交 git add . git commit -m "2" 修改文件内容为3,再次提交 git add . git commit -m "3"
将内容回滚到第2次提交,查看git提交记录
回滚到第2次说起的commit id
查看1.txt文件内容,发现是2
2. 经过分支来模拟:出现bug以后如何解决?
3. 将代码托管到github上
4. 之后:将我的代码非敏感信息上传到github上。
使用命令行方式,提交代码,参考文章:
http://www.py3study.com/Article/details/id/113.html
使用客户端工具SourceTree,它是Git官方GUI客户端,排名第一的客户端。
windows和MAC都是通用的
安装过程,请参考文章:
http://www.py3study.com/Article/details/id/112.html
SourceTree 提交代码以及合并,参考文章:
http://www.py3study.com/Article/details/id/143.html
ps: 之后去了公司上班,上传代码时,能够选择命令行方式或者使用客户端工具。
我我的推荐使用客户端工具,由于公司95%的人,是用的客户端工具。
大神都是敲命令的,膜拜!