Python full-stack of the road --- Special Edition (git use)

version control

Speaking version, you certainly will not feel strange, we often see the phone APP upgrade tips, this is another new version of the software available. Let me now a familiar example. Remember when we graduate thesis was tortured days? Tutor always be able to help you find a new one by mistake, kept modifications, each modification will become a version retained, so there's this scene the following scenario:

Preliminary thesis _ .doc 
thesis _ modify 1.doc 
thesis _ 2.doc modify 
thesis _ modify 3.doc 
thesis _ the full version 1.doc 
thesis _ the full version 2.doc 
thesis _ the full version 3. doc 
thesis _ final version 1.doc 
thesis _ 2.doc final version of the 
thesis _ dead nor revision .doc 
...

This is a version control method before we use, but also to avoid the loss of previously modified files, so can only save a file after another. Although this method is feasible, but there are many disadvantages:

1, large number of files, you need to save a file to an alternate for each version retains all versions

2, if the need for co-operation on these operations, can not help but talk about the copy after copy of these files are packaged to go

3, easy to lose, if missed, can not be recovered after deletion

So in order to solve the above version control, a number of version control tools have emerged: VSS, CVS, SVN, Git, etc., and in this one Git in an absolutely dominant position.

Note: Version control tool consists of two parts

  • The client (local): preparation of local content and release records
  • Server (network drive): The contents and the version recorded simultaneously held on the remote (optional)

Git Introduction

Git  is an open source distributed version control software for efficient, high-speed processing from very small to very large project version management. Git was originally developed by Linus Torvalds designed and developed to manage Linux kernel development. Git is a free / free software distributed under the terms of the GNU General Public License version 2.

Before using Git we need to install Git. Git support currently runs on Linux / Unix, Solaris, Mac and Windows platforms.

Git each platform installation package download address is: http://git-scm.com/downloads

Followed by fool-click next all the way to complete the installation

Once installed, you can use the git command line tool (already comes with the ssh client), and a graphical interface in addition to Git project management tool.

Find "Git" in the Start menu -> "Git Bash", Git command window will pop up, you can Git operate in the window.

GitHub is a hosted Git remote file platform (with GitCafe, BitBucket and GitLab, etc.) based.

Git version control itself can do, but all of its contents and imprint can only be saved in this machine, if you want the contents of the file and the version stored in the remote while recording, you need to use in conjunction with GitHub. scenes to be used:

  • No GitHub: maintain last file in the folder within the local file .git
  • There GitHub: maintain last file in the folder local .git file, but also will last for files hosted on a remote repository

other:

    Centralized: The remote server saves all versions, users have a version of the client
    Distributed: The remote server saves all versions, users have all client versions

Git uses the Ning Bao explore history: start-up period

 By understanding, Ning Bao know Git version control tool, try holding the attitude, he started using Git for version control.

 As the need to actually understand its effect, we a simple Django project to observe the effect of:

 In the case of git has been successfully installed, enter the directory has been created under a good project, right mouse Git Bash Here you can see the selection, click to enter to see the figure below:

 

At this point we try a few simple commands:

pwd -------> View program directory

git init ------> git initialization

 After initialization, the current directory is created automatically .git folder, this folder Git is the most important folder because Git version and relevant documents will be saved in this folder, so many versions can be saved in .git file

 We then look at the other commands:

git status -------> to view the current status of git

 

git add. -------> Add all files in the current directory to the repository

git commit -m 'name record (self-created)' --------> committed to the repository, and fill out the version of explanation, in order to roll back later

 Note that: git commit command execution time, you may be prompted to configure user and mailbox, which is configured to record the current version of which was submitted by the user:

git config --local user.name 'Name'

git config --local user.email 'mailbox'

 Now let's look at two simple understanding of regional and four state Git managed file:

Workspace : The current development program directory called workspace, namely: development work is in the directory, the file will change the status of the region and the state by the git automatically detect if the program files do anything (add, delete , change), will file state is detected, you can use [] git status command.

 We try to create a new file to observe the phenomenon:

touch a.py -------> Create a new file a.py

When the command git status again to view the status, git automatically detect changes occur a.py workspace, and marked out in red

 

 版本库:工作区检测到有文件发生变化,那么意味着较上一个版本之后对程序进行了修改,修改完成之后,可以当做下一版本进行提交,那么就是执行 【git add .】 将所有文件提交到暂存区,此时再次查看状态发现a.py变成了绿色。然后再执行【git commit -m '又一个版本'】提交到版本库的分支即可,

git log-------->查看历史版本提交记录(根据版本commit值可以进行回滚)

 

 现在我们对已经出现过的命令进行简单的小结

  • git init,初始化,表示即将对当前文件夹进行版本控制。
  • git status,查看Git当前状态,如:那些文件被修改过、那些文件还未提交到版本库等。
  • git add 文件名,将指定文件添加到版本库的暂存状态。
  • git commit -m '提交信息',将暂存区的文件提交到版本库的分支。
      • git config --local user.name '姓名'   标明提交的用户
      • git config --local user.email '邮箱' 标明提交的邮箱   
  • git log,查看提交记录,即:历史版本记录

 在git的配合下,项目的建设在有条不紊的进行,进过努力,我们已经完成了版块一的开发

 

但是突然某一天发现版块一中有一个很严重的漏洞,严重到该版块接近作废,此时我们又想回到版块一开发前的状态了。于是,我们决定:回滚,回到上一个版本

通过使用git reset --hard XXXXX命令,我们成功回到了曾经,但是突然有一天我们又想回到版块一完成的状态应该怎么办呢?

当然可以通过git reflog命令查看记录再次回滚:

Git使用之凝宝探索史:成长期

 时间突然变得有点紧,凝宝又接到了其他项目需要尽快启动,然而目前的项目也还在开发,更糟糕的是,此时已经开发的程序中出现了Bug需要紧急修复。。。

方案一:stash

stash用于将工作区发生变化的所有文件获取临时存储在“某个地方”,将工作区还原当前版本未操作前的状态;stash还可以将临时存储在“某个地方”的文件再次拿回到工作区。

 

 特别的:执行 git stash pop 命令时,可能会遇到冲突,因为在紧急修复bug的代码和通过stash存储在“某个地方”的代码会有重合部分,所以执行 git stash pop 时候就会出现冲突,有冲突解决冲突即可。

纠正后:

 

现在对stash相关常用命令进行总结

  • git stash             将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态
  • git stash list        查看“某个地方”存储的所有记录
  • git stash clear     清空“某个地方”
  • git stash pop       将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
  • git stash apply     编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突) 
  • git stash drop      编号,删除指定编号的记录

方案二:branch

 分支学习:branch称为分支,默认仅有一个名为master的分支。一般开发新功能流程为:开发新功能时会在分支dev上进行,开发完毕后再合并到master分支。

 

我们继续来认识新命令:

git branch dev--------->创建新分支,即:拷贝一份当前所在分支代码到新分支

git checkout dev------->切换到dev分支

 

 新版块开发完成后即可提交并切回master将其合并

 

 和上图一样,我们也可以按照着这样的流程进行开发,如果遇到上文开发到一般需要临时修复Bug的情况,可以按照下图的流程进行:

 

 

修复完成后切回master并将bug与之合并,然后进入dev继续开发,完成后同样进行提交合并。

注意:git merge 时也可能会出现冲突,解决冲突的方式上述stash相同,即:找到冲突文件,手动修改冲突并提交,此处不再敖述。

branch相关常用命令

  • git branch 分支名称             创建分支
  • git checkout 分支名称          切换分支
  • git branch -m 分支名称        创建并切换到指定分支
  • git branch                          查看所有分支
  • git branch -d 分支名称         删除分支
  • git merge 分支名称              将指定分支合并到当前分支

Git使用之凝宝探索史:快速发展期

 由于实际需要,现在凝宝不得不天天在家里和公司来回,但是如果和以前一样使用U盘来存储已经写好的代码随身携带则显得太不专业了,于是我们有引入了一个新的工具,即GitHub,一个基于Git实现的代码托管的平台,可以将内容以及版本记录在远程也保存一份,这样就不用U盘咯(类似于云盘)。PS: 类似GitHub的产品还有许多,如:GitLab、Bitbucket、码云等。

基于GitHub实现代码托管,需要一下步骤:

  • 注册GitHub
  • 创建仓库,创建完仓库后会有一个URL代指该仓库,如:
  • git可以是用该URL进行向远程推送版本信息或获取版本信息

于是,凝宝开始把已经写好的代码推送到GitHub上进行托管

git remote add origin https://github.com/zhuoning680/demo.git    # 为地址起一个别名origin

git push origin master     #将本地master分支内容以及版本信息推送到GitHub

随后在跳出的登录界面输入邮箱和密码

git push origin dev       # 将本地dev分支内容以及版本信息推送到GitHub

 到了公司,由于是第一次使用新电脑,需要将代码从GitHub中获取并继续开发

 git clone https://github.com/zhuoning680/demo.git      #将项目从GitHub中获取

git Branch                     # 默认获取到得只有master分支

git branch dev origin/dev                               # 创建dev分支且和远程dev分支同步

git checkout dev                      # 切换到dev分支

开发新功能后

git push origin dev                # 提交dev分支内容到远程GitHub托管仓库的dev分支

回到家,由于白天在公司已经开发一部分功能并提交到GitHub,家里电脑的代码还是昨晚的版本,所以需要从GitHub拉去最新代码,然后继续开发。

git checkout dev      # 切换到dev分支
git pull origin dev   # 从远程GitHub仓库获取dev分支最新内容,并合并到本地  
#继续开发之后add  commit

在公司,由于昨天晚上在家已经开发了一部分功能,在公司需要先把昨晚开发的功能从GitHub中拉取,并继续开发。

git checkout dev                                   # 切换到dev分支
git fetch origin dev                               # 从GitHub仓库获取dev分支最新内容到版本库的分支
git merge origin/dev                               # 将版本库的分支内容合并到工作区
#继续开发
git add .                                          # 添加文件到版本库的暂存状态
git commit -m 'xxxxxxxxxxx'                        # 提交新功能到版本库的分支

 

长此以往,将Git和GitHub结合使用做到避免电脑损坏造成数据丢失以及多地开发的问题,上文执行过程中执行 【git pull origin 分支】命令等同于【git fetch origin 分支】+ 【git merge origin/分支】,并且在执行过程中可能会出现冲突,原因是由于本地代码和获取的最新代码有重合部分,那么就需要自己手动解决冲突然后再继续开发

Git使用之凝宝探索史:成熟期

 由于手上项目太多,凝宝想让好朋友柳柳来参与合作一起开发。

在GitHub中提供了两种方式:

合作者,将其他用户添加到仓库合作者中之后,该用户就具有向当前仓库提交代码。

 

组织,创建一个组织,然后再该组织下可以创建多个项目,组内成员可以向组内所有项目提交代码。PS:也可以对某个项目指定合作者

 

 

 

协同开发命令和以上步骤类似,此处就不再重新写代码,而是使用文件描述三人协同开发整个过程。

  • 创建程序
    • 用户A创建程序,提交到GitHub
    • 用户B克隆项目
    • 用户C克隆项目
  • 开发功能
    • 用户A开发功能1
    • 用户B开发功能2
    • 用户C开发功能3
  • 提交
    • 用户A提交功能1,并push(A用户手速快,先提交。)
    • 用户B提交功能2,无法push,因为GitHub上已经有其他人提交的新代码。
      解决方法:从GitHub上获取最新代码并合并到本地,提交自己开发的功能2。
    • 用户C提交功能3,无法push,无法提交,因为GitHub上已经有其他人提交的新代码。
      解决方法:从GitHub上获取最新代码并合并到本地,提交自己开发的功能3。
  • 获取最新代码
    • 用户A获取最新代码
    • 用户B获取最新代码
    • 用户C获取最新代码

在上面红色标注的解决方法位置可以有三种方式操作,三者都可以完成合并并提交新功能,但是日志记录会有差异,如:前两者版本记录中会出现合并,而第三种可以保证版本记录干净整洁。

    • 先 git pull origin master   然后 git push origin master
    • 先 git fetch origin master 然后 git merge 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 pull origin master
 
    git push origin master

 

 

先 git fetch origin master 然后 git rebase 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   

补充内容

1. 配置文件

Git的配置文件有三个:

  • 系统配置: /private/etc/gitconfig
  • 用户配置: ~/.gitconfig
  • 项目配置:.git/config

2. 用户凭证

由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两种解决方法:

    • 秘钥
      首先创建一对秘钥  ssh-keygen -t rsa,然后将 id_rsa.pub (公钥)内容拷贝到github中,日后操作无需再输入用户名和密码。
      注意:这种方式需要使用GIt中 [email protected]:WuPeiqi/xxxxx.git 格式地址。
    • 密码
      Https访问git时,避免每次操作需要输入用户名和密码,可以在配置文件中添加如下配置项:
          [credential]
          helper = store/cache/第三方

      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 

3、配置Git

首先在本地创建ssh key;

$ ssh-keygen -t rsa -C "[email protected]"

后面的[email protected]改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key

回到github上,进入 Account Settings(账户配置),左边选择SSH Keys,Add SSH Key,title随便填,粘贴在你电脑上生成的key。

为了验证是否成功,在git bash下输入:

如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。

接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。

$ git config --global user.name "your name"
$ git config --global user.email "[email protected]"

进入要上传的仓库,右键git bash,添加远程地址:

$ git remote add origin [email protected]:yourName/yourRepo.git

后面的yourName和yourRepo表示你再github的用户名和刚才新建的仓库,加完之后进入.git,打开config,这里会多出一个remote "origin"内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。

<h3创建新仓库< h3="">

创建新文件夹,打开,然后执行 git init 以创建新的 git 仓库。

 

Guess you like

Origin www.cnblogs.com/zhuoning/p/11384040.html