git

https://www.cnblogs.com/maple-shaw/articles/7552210.html

1. 版本控制

1. 初始化

  1. 下载安装

    https://git-scm.com/downloads
    
  2. 创建文件夹,右键打开 git bash

  3. 初始化在文件夹中初始化

    git init #创建git版本管理的本地仓库
    # 或者不创建文件夹,在Git init时创建文件夹  
    git init 文件夹名称
    
  4. 产生的.git文件夹用来存放你管理的文件的所有版本以及git配置相关的内容,不要轻易动它

2. 简单指令使用

1. 基本操作命令

git init      # 文件  创建文件并初始化仓库
git  status   # 查看状态
git  add 文件名 /.    # 将变化的内容添加到暂存区,用文件名或者点
git commit -m '描述信息'  # 提交到版本库 
git log          # 查看版本记录
git reflog       # 查看版本变更记录
git reset --hard    # 版本号    版本回退
git checkout -b dev #创建并切换到dev分支上

2. 分支 branch

git branch 分支名称            # 创建分支
git checkout 分支名称          # 切换分支
git branch -m 分支名称         # 创建并切换到指定分支
git branch                    # 查看所有分支
git branch -d 分支名称         # 删除分支
git merge 分支名称             # 将指定分支合并到当前分支,两个同时修改后合并,有冲突时手动修改

3. stash相关常用命令

git stash             将当前工作区所有修改过的内容存储到“某个地方”,将工作区还原到当前版本未修改过的状态,还没有开发完成,要去进行bug修复,先暂停一下,不上传
git stash list        查看“某个地方”存储的所有记录
git stash clear     清空“某个地方”
git stash pop       将第一个记录从“某个地方”重新拿到工作区(可能有冲突)
git stash apply     编号, 将指定编号记录从“某个地方”重新拿到工作区(可能有冲突) 
git stash drop      编号,删除指定编号的记录

4. 练习

# 打开e盘,执行右键git bash git功能
$ git init day68  # 在e盘创建了day68的文件夹

$ cd day68   # 切换路径到day68的文件夹,默认分支master

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ touch a.txt   # 在day68中添加a.txt文件 

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ django-admin startproject day68  # 创建day68的django项目

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git status   # 查看状态
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a.txt    # 红色字体表示修改未保存
        day68/   #

nothing added to commit but untracked files present (use "git add" to track)

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git add .    # 将变化的内容添加到暂存区,点是所有的改变

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git commit -m "新建项目"   # 提交到版本库 
[master (root-commit) 806c55b] 新建项目
 6 files changed, 179 insertions(+)
 create mode 100644 a.txt
 create mode 100644 day68/day68/__init__.py
 create mode 100644 day68/day68/settings.py
 create mode 100644 day68/day68/urls.py
 create mode 100644 day68/day68/wsgi.py
 create mode 100644 day68/manage.py

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ cd day68   # 切换到django的文件夹目录

Lenovo@SinoSoft MINGW64 /e/day68/day68 (master)
$ python manage.py startapp app01   # 在此路径下添加应用

Lenovo@SinoSoft MINGW64 /e/day68/day68 (master)
$ cd ..   # 切换到上级目录

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git status   # 查看状态,app01还未提交保存
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:   day68/day68/urls.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        day68/app01/
        day68/day68/__pycache__/

no changes added to commit (use "git add" and/or "git commit -a")

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git commit -m "新建首页功能"  # url.py文件中添加index路由
[master fe0a627] 新建首页功能
 10 files changed, 19 insertions(+)
 create mode 100644 day68/app01/__init__.py
 create mode 100644 day68/app01/admin.py
 create mode 100644 day68/app01/apps.py
 create mode 100644 day68/app01/migrations/__init__.py
 create mode 100644 day68/app01/models.py
 create mode 100644 day68/app01/tests.py
 create mode 100644 day68/app01/views.py
 create mode 100644 day68/day68/__pycache__/__init__.cpython-36.pyc
 create mode 100644 day68/day68/__pycache__/settings.cpython-36.pyc

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git branch new  # 创建新的分值,名称为new

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git branch    # 查看分支
  *master       # *代表当前分支
   new      

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout new     # 切换分支到new
Switched to branch 'new'

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git status
On branch new
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:   day68/day68/urls.py

no changes added to commit (use "git add" and/or "git commit -a")

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git commit -m "创建登陆功能"
[new 29a6aef] 创建登陆功能
 1 file changed, 1 insertion(+)

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git log   # 查看版本记录
commit 29a6aef4f3615eb29b55b702e1aca4c75f6170fc (HEAD -> new)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:02:32 2019 +0800

    创建登陆功能

commit fe0a627eaa30d3e3df38acce85b95351812993c3 (master)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:59:35 2019 +0800

    新建首页功能

commit 806c55bbf065e72c1e9f747ee812f68715ba8216
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:54:22 2019 +0800

    新建项目

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git checkout master
Switched to branch 'master'

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git log
commit fe0a627eaa30d3e3df38acce85b95351812993c3 (HEAD -> master)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:59:35 2019 +0800

    新建首页功能

commit 806c55bbf065e72c1e9f747ee812f68715ba8216
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:54:22 2019 +0800

    新建项目

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git log
commit fe0a627eaa30d3e3df38acce85b95351812993c3 (HEAD -> master)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:59:35 2019 +0800

    新建首页功能

commit 806c55bbf065e72c1e9f747ee812f68715ba8216
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:54:22 2019 +0800

    新建项目

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ 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:   day68/day68/urls.py

no changes added to commit (use "git add" and/or "git commit -a")

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git commit -m "新建注册功能"
[master e8b82e6] 新建注册功能
 1 file changed, 1 insertion(+)

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git merge new    # 将new合并到master中,只有两个都做了修改时才会冲突,手动修改
Auto-merging day68/day68/urls.py
CONFLICT (content): Merge conflict in day68/day68/urls.py
Automatic merge failed; fix conflicts and then commit the result.

Lenovo@SinoSoft MINGW64 /e/day68 (master|MERGING)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (master|MERGING)
$ git commit -m "合并new的reg功能"
[master 5350502] 合并new的reg功能

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git log
commit 5350502667ce013168a0c30c14b62e984e36693b (HEAD -> master)
Merge: e8b82e6 29a6aef
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:07:27 2019 +0800

    合并new的reg功能

commit e8b82e6a64389f05a95241928c87ebca7ad14686
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:05:24 2019 +0800

    新建注册功能

commit 29a6aef4f3615eb29b55b702e1aca4c75f6170fc (new)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:02:32 2019 +0800

    创建登陆功能

commit fe0a627eaa30d3e3df38acce85b95351812993c3
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:59:35 2019 +0800


Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout new
Switched to branch 'new'

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git log
commit 29a6aef4f3615eb29b55b702e1aca4c75f6170fc (HEAD -> new)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:02:32 2019 +0800

    创建登陆功能

commit fe0a627eaa30d3e3df38acce85b95351812993c3
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:59:35 2019 +0800

    新建首页功能

commit 806c55bbf065e72c1e9f747ee812f68715ba8216
Author: ko <541121@qq.com>
Date:   Thu Nov 14 17:54:22 2019 +0800

    新建项目

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git checkout master
Switched to branch 'master'

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ 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:   day68/day68/urls.py

no changes added to commit (use "git add" and/or "git commit -a")

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git commit -m "新增出版社"
[master 9380ce3] 新增出版社
 1 file changed, 1 insertion(+)

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout new
Switched to branch 'new'

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git merge master   # master作了修改,new未作修改,合并时不会冲突
Updating 29a6aef..9380ce3
Fast-forward
 day68/day68/urls.py | 2 ++
 1 file changed, 2 insertions(+)

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git log
commit 9380ce36f1117d56bb7c41e5be3c42a79c3ad099 (HEAD -> new, master)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:14:01 2019 +0800

    新增出版社

commit 5350502667ce013168a0c30c14b62e984e36693b
Merge: e8b82e6 29a6aef
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:07:27 2019 +0800

    合并new的reg功能

commit e8b82e6a64389f05a95241928c87ebca7ad14686
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:05:24 2019 +0800

    新建注册功能

commit 29a6aef4f3615eb29b55b702e1aca4c75f6170fc
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:02:32 2019 +0800


Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git reset --hard
HEAD is now at 9380ce3 新增出版社

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git reset --hard
HEAD is now at 9380ce3 新增出版社

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git status
On branch new
nothing to commit, working tree clean

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git log
commit 9380ce36f1117d56bb7c41e5be3c42a79c3ad099 (HEAD -> new, master)
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:14:01 2019 +0800

    新增出版社

commit 5350502667ce013168a0c30c14b62e984e36693b
Merge: e8b82e6 29a6aef
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:07:27 2019 +0800

    合并new的reg功能

commit e8b82e6a64389f05a95241928c87ebca7ad14686
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:05:24 2019 +0800

    新建注册功能

commit 29a6aef4f3615eb29b55b702e1aca4c75f6170fc
Author: ko <541121@qq.com>
Date:   Thu Nov 14 18:02:32 2019 +0800

Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git reset --hard 29a6aef4f3615e    # 回滚到指定版本
HEAD is now at 29a6aef 创建登陆功能

3. 个人开发

1. 有bug需要修复时

在开发过程中,master存上线的功能代码,dev存放开发行的功能,debug执行临时修复bug,这时我们可能正在开发,但还是还没有开发完,不上传版本,可以使其先隐藏,等修复完成合并后再显示继续开发
Lenovo@SinoSoft MINGW64 /e/day68 (new)
$ git checkout master
Switched to branch 'master'

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git branch -d new
Deleted branch new (was 29a6aef).

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git branch
* master

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git branch dev

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout dev
Switched to branch 'dev'

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ 打开views,写函数
bash: 打开views,写函数: command not found

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ def index(request):
bash: syntax error near unexpected token `('

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git status
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:   day68/app01/views.py

no changes added to commit (use "git add" and/or "git commit -a")

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git checkout master
Switched to branch 'master'
M       day68/app01/views.py

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ 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:   day68/app01/views.py

no changes added to commit (use "git add" and/or "git commit -a")

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout dev
Switched to branch 'dev'
M       day68/app01/views.py

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   day68/app01/views.py


Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git stash      # 将未开发完成的dev分支先隐藏,在切换回master,构建debug分支解决bug
Saved working directory and index state WIP on dev: 9380ce3 新增出版社

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git checkout master
Switched to branch 'master'

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git status
On branch master
nothing to commit, working tree clean

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git branch debug

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout debug
Switched to branch 'debug'

Lenovo@SinoSoft MINGW64 /e/day68 (debug)
$ 在debug文件修改bug,然后提交
bash: 在debug文件修改bug,然后提交: command not found

Lenovo@SinoSoft MINGW64 /e/day68 (debug)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (debug)
$ git commit -m "修改 xx bug"
[debug 0c61fdf] 修改 xx bug
 1 file changed, 1 insertion(+), 1 deletion(-)

Lenovo@SinoSoft MINGW64 /e/day68 (debug)
$ git checkout master
Switched to branch 'master'

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git merge debug
Updating 9380ce3..0c61fdf
Fast-forward
 day68/app01/views.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Lenovo@SinoSoft MINGW64 /e/day68 (master)
$ git checkout dev
Switched to branch 'dev'

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git merge debug
Updating 9380ce3..0c61fdf
Fast-forward
 day68/app01/views.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ 分别用master和dev合并debug,修复bug
bash: 分别用master和dev合并debug,修复bug: command not found

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ 删除掉debug分支
bash: 删除掉debug分支: command not found

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git branch -d debug
Deleted branch debug (was 0c61fdf).

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git branch
* dev
  master

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git stash pop   # 修复完成后,显示dev分支继续开发
Auto-merging day68/app01/views.py
CONFLICT (content): Merge conflict in day68/app01/views.py
The stash entry is kept in case you need it again.

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ 位置有冲突了,手动修复,
bash: 位置有冲突了,手动修复,: command not found

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ 再次开发,写完后上传保存
bash: 再次开发,写完后上传保存: command not found

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git add .

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git commit -m "views新增index函数"
[dev f9899af] views新增index函数
 1 file changed, 5 insertions(+), 1 deletion(-)

Lenovo@SinoSoft MINGW64 /e/day68 (dev)
$ git log
commit f9899af1258d6ef9e01fc59f05acff35ed5d7ae0 (HEAD -> dev)
Author: 吕卫贺 <541198121@qq.com>
Date:   Thu Nov 14 20:22:29 2019 +0800

    views新增index函数

commit 0c61fdf9be6914eb5cc0eb532328962af5478653 (master)
Author: 吕卫贺 <541198121@qq.com>
Date:   Thu Nov 14 20:13:13 2019 +0800

    修改 xx bug

commit 9380ce36f1117d56bb7c41e5be3c42a79c3ad099
Author: 吕卫贺 <541198121@qq.com>
Date:   Thu Nov 14 18:14:01 2019 +0800

    新增出版社

commit 5350502667ce013168a0c30c14b62e984e36693b
Merge: e8b82e6 29a6aef
Author: 吕卫贺 <541198121@qq.com>
Date:   Thu Nov 14 18:07:27 2019 +0800

4. 本地仓库与网络仓库连接

  1. 先在本地创建仓库,并初始化

    git init #创建git版本管理的本地仓库
    # 或者不创建文件夹,在Git init时创建文件夹  
    git init 文件夹名称
    
  2. 码云中创建仓库

    个人主页 -- +新建仓库 -- 写仓库名称 -- 公开 -- 都不选 -- 点创建
    
  3. 本地仓库与网络仓库建立连接

由于已有本地仓库,只需要构件连接,所以不需要新建仓库文件夹了,只需要把
git remote add origin https://gitee.com/lvweihe/s26.git
粘贴到本地打开的git软件中,就建立好链接了,然后上传即可
git add .
git commmit -m "上传"
git push -u origin master // 上传文件成功
# 只需输入已有仓库的代码即可
  1. 刷新即可,显示上传的内容

  2. 在网络仓库新建分支

点master 管理 -- 新建分支 --用时下拉框切换即可
或者在本地git中切换到dev,然后git push -u origin dev // 上传文件成功,新建了dev分支

5. 个人在家与公司开发

5.1 需求

在公司开发,下班了,提交数据到码云的网络仓库dev中,但是还没有完成,想回家接着开发,需要在家构建仓库,从马云上下载下来,等开发完成后再提交

5.2 流程

  1. 下班提交代码

    git  add . 
    git  commit -m  '未完成' 
    git push origin dev 
    
  2. 回家接着开发

    首先下载安装git,并做好相关配置
    克隆代码 clone,并添加网络路径
    本地新建文件夹home,打开git bash here,输入如下代码,
    git clone https://gitee.com/maple-shaw/day68.git   // 将网络文件下载到本地
    $ git branch
    * master       #// 只有master分支
    
    $ git branch dev  #// 新建dev分支
    
    Lenovo@SinoSoft MINGW64 /e/home/s25 (master)
    $ git checkout dev    #// 切换到dev分支
    Switched to branch 'dev'
    
    Lenovo@SinoSoft MINGW64 /e/home/s25 (dev)
    $ git pull origin dev     #// 从网络拉取dev分支文件
    From https://gitee.com/lvweihe/s25
     * branch            dev        -> FETCH_HEAD
    Already up to date.
    
    Lenovo@SinoSoft MINGW64 /e/home/s25 (dev)
    $ git branch
    * dev
      master
    
  3. 开发完成提交

  4. 第二天公司

    git pull origin dev
    有冲突解决冲突,继续开发
    

6. 多人协同开发

  1. 网络搭建自己的分支 lv
  2. 本地创建分支 lv
  3. 对文件进行开发,比如添加了c.txt文件
  4. 添加并将内容推到网络 $ git push origin lv
  5. 领导看完并进行线上合并 pull request ,并新建,将lv合并到dev分支,标题和简单说明,点创建

7. 自己项目文件

  1. 每个django项目中都有一个.idea文件夹,是pycharm自带的,要忽略

  2. 找到一个项目文件夹,初始化git文件

    git init
    
    
  3. 网上访问 https://github.com/

  4. 搜索gitignore,点进去

  5. 找到Python.gitignore的文件,创建.gitignore的文件

    touch .gitignore
    
    
  6. 打开网络文件,将内容拷到本地的.gitignore文件中

  7. 并添加.idea内容,表示忽略,保存并关闭文件

  8. $ git add . $ git status 没有.idea

开发环境和线上环境配置问题

  1. 复制一份settings.py改名为local_settings.py

  2. local_settings.py ,在配置时此文件不会上传到git,是忽略文件

    DEBUG = True
    
    ALLOWED_HOSTS = []
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': "crm01",    # cmd创建的数据库名称
            "USER": "root",      # 连接数据库的用户名
            "PASSWORD": "123",   # 连接数据库的密码
            "HOST": "127.0.0.1", # 连接主机,默认本级
            "PORT": 3306,        #  端口 默认3306
        }
    }
    
    
  3. settings.py

    DEBUG = False
    
    ALLOWED_HOSTS = ["*"]
    
    try:
        from .local_settings import *  # 重写条件
    except ModuleNotFoundError:
        pass
    
    
原文地址:https://www.cnblogs.com/lvweihe/p/11885351.html