├── postBuild ├── Git常用命令.bmp ├── git仓库原理.png ├── git系统原理.jpg ├── 人工版本管理.png ├── github是什么.jpg ├── git分支最佳实践.png ├── git版本管理效果.png ├── Git分支最佳实践说明.png ├── git的diff原理.png ├── git的merge原理.png ├── git的rebase原理.png ├── git merge 快进式.png ├── git 的 reset 原理.png ├── git merge --no-ff.png ├── git的cherry-pick原理.png ├── git的commit原理(1).png ├── git的commit原理(2).png ├── git的commit原理(3).png ├── git reset --files原理.png ├── README.md ├── environment.yml ├── Git常用命令.txt ├── Git常用命令清单 └── GitHub简易教程-Live.ipynb /postBuild: -------------------------------------------------------------------------------- 1 | python -m ipykernel install --user --name=Python_Ai_Road 2 | -------------------------------------------------------------------------------- /Git常用命令.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/Git常用命令.bmp -------------------------------------------------------------------------------- /git仓库原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git仓库原理.png -------------------------------------------------------------------------------- /git系统原理.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git系统原理.jpg -------------------------------------------------------------------------------- /人工版本管理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/人工版本管理.png -------------------------------------------------------------------------------- /github是什么.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/github是什么.jpg -------------------------------------------------------------------------------- /git分支最佳实践.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git分支最佳实践.png -------------------------------------------------------------------------------- /git版本管理效果.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git版本管理效果.png -------------------------------------------------------------------------------- /Git分支最佳实践说明.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/Git分支最佳实践说明.png -------------------------------------------------------------------------------- /git的diff原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的diff原理.png -------------------------------------------------------------------------------- /git的merge原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的merge原理.png -------------------------------------------------------------------------------- /git的rebase原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的rebase原理.png -------------------------------------------------------------------------------- /git merge 快进式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git merge 快进式.png -------------------------------------------------------------------------------- /git 的 reset 原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git 的 reset 原理.png -------------------------------------------------------------------------------- /git merge --no-ff.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git merge --no-ff.png -------------------------------------------------------------------------------- /git的cherry-pick原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的cherry-pick原理.png -------------------------------------------------------------------------------- /git的commit原理(1).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的commit原理(1).png -------------------------------------------------------------------------------- /git的commit原理(2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的commit原理(2).png -------------------------------------------------------------------------------- /git的commit原理(3).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git的commit原理(3).png -------------------------------------------------------------------------------- /git reset --files原理.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lyhue1991/github_tutorial/HEAD/git reset --files原理.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # github_tutorial 2 | 3 | ### GitHub教程正文: GitHub简易教程-Live.ipnb 4 | ### Git常用命令一览(文本): Git常用命令.txt 5 | ### Git常用命令一览(思维导图):Git常用命令.bmp 6 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | dependencies: 2 | - python=3 3 | - pip: 4 | - spacy 5 | - ipykernel 6 | - scipy 7 | - numpy 8 | - scikit-learn 9 | - matplotlib 10 | - pandas 11 | - thinc 12 | -------------------------------------------------------------------------------- /Git常用命令.txt: -------------------------------------------------------------------------------- 1 | Git常用命令 2 | 3 | 〇,10分钟入门 4 | 5 | 配置用户信息: 6 | git config --global user.name "XX" 7 | git config --global user.email "XX@XX" 8 | 9 | 新建文件夹并切入 10 | mkdir git-learn 11 | cd git-learn 12 | 13 | 创建仓库: 14 | git init 15 | 16 | 新建readme.txt 17 | echo "hello world" >>readme.txt 18 | 19 | 查看当前状态: 20 | git status 21 | 22 | 添加全部修改到暂存区并提交 23 | git add -A 24 | git commit -m"comment" 25 | gitk 26 | 27 | 添加github远程库并推送文件 28 | git remote add origin https://github.com/XX/XX 29 | git push -u origin master 30 | 31 | 在github上的项目clone到本地文件 32 | git clone https://github.com/XX/XX ../XX 33 | 34 | 35 | 一,安装配置 36 | 37 | 命令列表: 38 | config(配置信息) 39 | init(创建仓库) 40 | help(帮助信息) 41 | status(当前状态) 42 | gitk(图形界面) 43 | 44 | 45 | 示范1: 46 | 设置用户名: 47 | git config --global user.name "XXX" 48 | 设置用户邮箱: 49 | git config --global user.email "XXX@XX" 50 | 51 | 52 | 示范2: 53 | 获取常用git命令列表: 54 | git help 55 | 查看当前状态,获取提示: 56 | git status 57 | 58 | 59 | 示范3: 60 | 产生SSH密钥对: 61 | ssh-keygen -t rsa -C "your_emial@xxx.com" 62 | 将公钥绑定github后尝试建立SSH连接: 63 | ssh -T git@github.com 64 | 65 | 66 | 二,版本管理 67 | 68 | 命令列表: 69 | add(修改暂存) 70 | commit(提交修改) 71 | reset(版本回退) 72 | checkout(撤销修改) 73 | rm(删除文件) 74 | diff(比较文件) 75 | log(版本列表) 76 | reflog(版本历史) 77 | 78 | 79 | 示范1: 80 | git add readme.txt 81 | git add -A 82 | git commit -m"add readme.txt" 83 | 修改覆盖上一次commit: 84 | git commit --amend 85 | 先暂存再提交: 86 | git commit -a -m"modify readme.txt" 87 | 88 | 示范2: 89 | git reset --hard head^ 90 | git reset head a23b5 91 | 撤销修改: 92 | git checkout -- readme.txt 93 | 图形显示仓库版本及分支状态: 94 | git log --oneline --graph --all 95 | 查看head指向过的版本历史: 96 | git reflog 97 | 98 | 示范3: 99 | 查看工作区文件与暂存区文件区别: 100 | git diff readme.txt 101 | 查看工作区文件和head文件区别: 102 | git diff --head readme.txt 103 | 查看暂存区文件与head文件区别: 104 | git diff --cached readme.txt 105 | 查看两个版本某一文件的区别: 106 | git diff a458b d23e5 -- readme.txt 107 | 108 | 109 | 三,远程仓库 110 | 111 | 命令列表: 112 | remote(设置远程) 113 | push(推送远程) 114 | clone(克隆远程) 115 | pull(合并远程) 116 | fetch(更新远程) 117 | 118 | 119 | 示范1: 120 | 使用ssh地址添加github远程库连接并命名为github: 121 | git remote add github git@github.com:lyhue1991/ai.git: 122 | 使用url地址添加远程库: 123 | git remote add origin https://github.com/lyhue1991/GitHub.git 124 | 125 | 示范2: 126 | 推送本地至远程库origin的master分支: 127 | git push -u origin master 128 | 查看远程库信息: 129 | git remote -v 130 | 移除和远程库的连接: 131 | git remote remove orgin 132 | 133 | 示范3: 134 | 建立当前master分支与远程库develop分支的追踪关系: 135 | git branch --set-upstream master origin/develop 136 | 将远程仓库克隆到本地父目录的ML文件夹: 137 | git clone https://github.com/lyhue1991/machine-learning.git ../ML 138 | 取回origin的develop分支与当前master分支合并(或会冲突): 139 | git pull orgin/develop:master 140 | 获取origin的develop分支到本地: 141 | git fetch origin develop 142 | 143 | 144 | 四,分支协作 145 | 146 | 命令列表: 147 | branch(设置分支) 148 | checkout(切换分支) 149 | merge(合并分支) 150 | cherry-pick(采集提交) 151 | rebase(重演分支) 152 | stash(储藏管理) 153 | tag(标签管理) 154 | 155 | 156 | 示范1: 157 | 查看分支信息: 158 | git branch 159 | 当前head位置新建develop分支: 160 | git branch develop 161 | 创建并切换到名称为feature的新分支: 162 | git checkout -b feature 163 | 切换至master分支: 164 | git checkout master 165 | 166 | 167 | 示范2: 168 | head处于develop分支,合并feature分支(或会冲突): 169 | git merge --no-ff -m"merge feature" feature 170 | 采集其它分支中版本号为a458b的commit提交至当前分支(或会冲突): 171 | git cherry-pick a458b 172 | 使用当前所在分支作为base重演develop分支(或会冲突): 173 | git rebase develop 174 | 175 | 示范3: 176 | git stash; 177 | git stash pop; 178 | git stash list; 179 | git stash save "message" 180 | git stash apply @2 181 | git tag; 182 | git tag v1.0; 183 | git tag v0.9 a2543d; 184 | 推送标签到远程(标签不会自动推送) 185 | git push origin v1.0 186 | 删除远程标签(先删本地,再push): 187 | git tag -d v0.9 188 | git push origin :refs/tags/v0.9 189 | 190 | -------------------------------------------------------------------------------- /Git常用命令清单: -------------------------------------------------------------------------------- 1 | Git 常用命令 2 | 3 | 一,仓库 4 | 5 | # 在当前目录新建一个Git代码库 6 | $ git init 7 | 8 | # 新建一个目录,将其初始化为Git代码库 9 | $ git init [project-name] 10 | 11 | # 下载一个项目和它的整个代码历史 12 | $ git clone [url] 13 | 14 | 二,配置 15 | 16 | # 显示当前的Git配置 17 | $ git config --list 18 | 19 | # 编辑Git配置文件 20 | $ git config -e [--global] 21 | 22 | # 设置提交代码时的用户信息 23 | $ git config [--global] user.name "[name]" 24 | $ git config [--global] user.email "[email address]" 25 | 26 | 三,增加/删除文件 27 | 28 | # 添加指定文件到暂存区 29 | $ git add [file1] [file2] ... 30 | 31 | # 添加指定目录到暂存区,包括子目录 32 | $ git add [dir] 33 | 34 | # 添加当前目录的所有文件到暂存区 35 | $ git add . 36 | 37 | # 添加每个变化前,都会要求确认 38 | # 对于同一个文件的多处变化,可以实现分次提交 39 | $ git add -p 40 | 41 | # 删除工作区文件,并且将这次删除放入暂存区 42 | $ git rm [file1] [file2] ... 43 | 44 | # 停止追踪指定文件,但该文件会保留在工作区 45 | $ git rm --cached [file] 46 | 47 | # 改名文件,并且将这个改名放入暂存区 48 | $ git mv [file-original] [file-renamed] 49 | 50 | 四,代码提交 51 | 52 | # 提交暂存区到仓库区 53 | $ git commit -m [message] 54 | 55 | # 提交暂存区的指定文件到仓库区 56 | $ git commit [file1] [file2] ... -m [message] 57 | 58 | # 提交工作区自上次commit之后的变化,直接到仓库区 59 | $ git commit -a 60 | 61 | # 提交时显示所有diff信息 62 | $ git commit -v 63 | 64 | # 使用一次新的commit,替代上一次提交 65 | # 如果代码没有任何新变化,则用来改写上一次commit的提交信息 66 | $ git commit --amend -m [message] 67 | 68 | # 重做上一次commit,并包括指定文件的新变化 69 | $ git commit --amend [file1] [file2] ... 70 | 71 | 五,分支 72 | 73 | # 列出所有本地分支 74 | $ git branch 75 | 76 | # 列出所有远程分支 77 | $ git branch -r 78 | 79 | # 列出所有本地分支和远程分支 80 | $ git branch -a 81 | 82 | # 新建一个分支,但依然停留在当前分支 83 | $ git branch [branch-name] 84 | 85 | # 新建一个分支,并切换到该分支 86 | $ git checkout -b [branch] 87 | 88 | # 新建一个分支,指向指定commit 89 | $ git branch [branch] [commit] 90 | 91 | # 新建一个分支,与指定的远程分支建立追踪关系 92 | $ git branch --track [branch] [remote-branch] 93 | 94 | # 切换到指定分支,并更新工作区 95 | $ git checkout [branch-name] 96 | 97 | # 切换到上一个分支 98 | $ git checkout - 99 | 100 | # 建立追踪关系,在现有分支与指定的远程分支之间 101 | $ git branch --set-upstream [branch] [remote-branch] 102 | 103 | # 合并指定分支到当前分支 104 | $ git merge [branch] 105 | 106 | # 选择一个commit,合并进当前分支 107 | $ git cherry-pick [commit] 108 | 109 | # 删除分支 110 | $ git branch -d [branch-name] 111 | 112 | # 删除远程分支 113 | $ git push origin --delete [branch-name] 114 | $ git branch -dr [remote/branch] 115 | 116 | 六,标签 117 | 118 | # 列出所有tag 119 | $ git tag 120 | 121 | # 新建一个tag在当前commit 122 | $ git tag [tag] 123 | 124 | # 新建一个tag在指定commit 125 | $ git tag [tag] [commit] 126 | 127 | # 删除本地tag 128 | $ git tag -d [tag] 129 | 130 | # 删除远程tag 131 | $ git push origin :refs/tags/[tagName] 132 | 133 | # 查看tag信息 134 | $ git show [tag] 135 | 136 | # 提交指定tag 137 | $ git push [remote] [tag] 138 | 139 | # 提交所有tag 140 | $ git push [remote] --tags 141 | 142 | # 新建一个分支,指向某个tag 143 | $ git checkout -b [branch] [tag] 144 | 145 | 七,查看信息 146 | 147 | # 显示有变更的文件 148 | $ git status 149 | 150 | # 显示当前分支的版本历史 151 | $ git log 152 | 153 | # 显示commit历史,以及每次commit发生变更的文件 154 | $ git log --stat 155 | 156 | # 搜索提交历史,根据关键词 157 | $ git log -S [keyword] 158 | 159 | # 显示某个commit之后的所有变动,每个commit占据一行 160 | $ git log [tag] HEAD --pretty=format:%s 161 | 162 | # 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件 163 | $ git log [tag] HEAD --grep feature 164 | 165 | # 显示某个文件的版本历史,包括文件改名 166 | $ git log --follow [file] 167 | $ git whatchanged [file] 168 | 169 | # 显示指定文件相关的每一次diff 170 | $ git log -p [file] 171 | 172 | # 显示过去5次提交 173 | $ git log -5 --pretty --oneline 174 | 175 | # 显示所有提交过的用户,按提交次数排序 176 | $ git shortlog -sn 177 | 178 | # 显示指定文件是什么人在什么时间修改过 179 | $ git blame [file] 180 | 181 | # 显示暂存区和工作区的差异 182 | $ git diff 183 | 184 | # 显示暂存区和上一个commit的差异 185 | $ git diff --cached [file] 186 | 187 | # 显示工作区与当前分支最新commit之间的差异 188 | $ git diff HEAD 189 | 190 | # 显示两次提交之间的差异 191 | $ git diff [first-branch]...[second-branch] 192 | 193 | # 显示今天你写了多少行代码 194 | $ git diff --shortstat "@{0 day ago}" 195 | 196 | # 显示某次提交的元数据和内容变化 197 | $ git show [commit] 198 | 199 | # 显示某次提交发生变化的文件 200 | $ git show --name-only [commit] 201 | 202 | # 显示某次提交时,某个文件的内容 203 | $ git show [commit]:[filename] 204 | 205 | # 显示当前分支的最近几次提交 206 | $ git reflog 207 | 208 | 八,远程同步 209 | 210 | # 下载远程仓库的所有变动 211 | $ git fetch [remote] 212 | 213 | # 显示所有远程仓库 214 | $ git remote -v 215 | 216 | # 显示某个远程仓库的信息 217 | $ git remote show [remote] 218 | 219 | # 增加一个新的远程仓库,并命名 220 | $ git remote add [shortname] [url] 221 | 222 | # 取回远程仓库的变化,并与本地分支合并 223 | $ git pull [remote] [branch] 224 | 225 | # 上传本地指定分支到远程仓库 226 | $ git push [remote] [branch] 227 | 228 | # 强行推送当前分支到远程仓库,即使有冲突 229 | $ git push [remote] --force 230 | 231 | # 推送所有分支到远程仓库 232 | $ git push [remote] --all 233 | 234 | 九,撤销 235 | 236 | # 恢复暂存区的指定文件到工作区 237 | $ git checkout [file] 238 | 239 | # 恢复某个commit的指定文件到暂存区和工作区 240 | $ git checkout [commit] [file] 241 | 242 | # 恢复暂存区的所有文件到工作区 243 | $ git checkout . 244 | 245 | # 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变 246 | $ git reset [file] 247 | 248 | # 重置暂存区与工作区,与上一次commit保持一致 249 | $ git reset --hard 250 | 251 | # 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变 252 | $ git reset [commit] 253 | 254 | # 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致 255 | $ git reset --hard [commit] 256 | 257 | # 重置当前HEAD为指定commit,但保持暂存区和工作区不变 258 | $ git reset --keep [commit] 259 | 260 | # 新建一个commit,用来撤销指定commit 261 | # 后者的所有变化都将被前者抵消,并且应用到当前分支 262 | $ git revert [commit] 263 | 264 | 暂时将未提交的变化移除,稍后再移入 265 | $ git stash 266 | $ git stash pop 267 | 268 | 十,其他 269 | 270 | # 生成一个可供发布的压缩包 271 | $ git archive 272 | -------------------------------------------------------------------------------- /GitHub简易教程-Live.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# GitHub极简教程" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "GitHub是一个非常流行的全球代码托管平台,基于Git版本控制技术实现,同时GitHub也是一个活跃的开发者交流社区。许多的开源项目都在GitHub上发布。例如著名的Linux操作系统内核的源代码。" 15 | ] 16 | }, 17 | { 18 | "cell_type": "markdown", 19 | "metadata": {}, 20 | "source": [ 21 | "[Linux内核源码](https://github.com/torvalds/linux)" 22 | ] 23 | }, 24 | { 25 | "cell_type": "markdown", 26 | "metadata": {}, 27 | "source": [ 28 | "GitHub是什么?" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "![](GitHub是什么.jpg)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [ 46 | "\n", 47 | "\n", 48 | "\n", 49 | "\n", 50 | "\n", 51 | "\n", 52 | "\n", 53 | "\n", 54 | "\n", 55 | "\n", 56 | "\n", 57 | "\n", 58 | "\n" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### 一,Git简介\n", 66 | "\n", 67 | "Git是目前世界上最先进的分布式版本控制系统(没有之一)。\n", 68 | "\n", 69 | "它由Linux系统之父Linus Torvalds在2005年用C语言开发。\n", 70 | "\n", 71 | "Git是一个分布式版本控制系统,没有中央服务器,不同于svn等需要中央服务器的集中式版本控制系统。\n", 72 | "\n", 73 | "Git的功能:版本控制(版本管理,远程仓库,分支协作)" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "**人工版本管理:**" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": {}, 86 | "source": [ 87 | "![](人工版本管理.png)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "**Git版本管理:**" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "![](git版本管理效果.png)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "collapsed": true 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "\n", 113 | "\n", 114 | "\n", 115 | "\n", 116 | "\n", 117 | "\n", 118 | "\n", 119 | "\n", 120 | "\n", 121 | "\n", 122 | "\n", 123 | "\n", 124 | "\n" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "### 二,功能原理" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": {}, 137 | "source": [ 138 | "* git版本控制系统由工作区,缓存区,版本库组成。\n", 139 | "* git跟踪的是文件的修改而不是全部文件。\n", 140 | "* git擅长管理代码等文本文件,不擅长管理图片等二进制文件。" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "![](./git系统原理.jpg)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": null, 153 | "metadata": { 154 | "collapsed": true 155 | }, 156 | "outputs": [], 157 | "source": [ 158 | "\n", 159 | "\n", 160 | "\n", 161 | "\n", 162 | "\n", 163 | "\n", 164 | "\n", 165 | "\n", 166 | "\n", 167 | "\n", 168 | "\n", 169 | "\n", 170 | "\n", 171 | "\n", 172 | "\n" 173 | ] 174 | }, 175 | { 176 | "cell_type": "markdown", 177 | "metadata": {}, 178 | "source": [ 179 | "### 三,快速上手" 180 | ] 181 | }, 182 | { 183 | "cell_type": "markdown", 184 | "metadata": {}, 185 | "source": [ 186 | "1,安装配置Git.\n", 187 | "\n", 188 | "下载位置: [Git下载链接](https://git-scm.com/downloads)—— https://git-scm.com/downloads\n", 189 | "\n", 190 | "windows下载安装后可以在Gitbash中执行git命令,也可以在Jupyter Notebook中执行." 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 197, 196 | "metadata": { 197 | "collapsed": false 198 | }, 199 | "outputs": [], 200 | "source": [ 201 | "!git config --global user.name \"lyhue1991\"\n", 202 | "!git config --global user.email \"lyhue1991@163.com\"\n", 203 | "\n", 204 | "# 出现一些类似 warning: LF will be replaced by CRLF in . 可启用如下设置。\n", 205 | "!git config --global core.autocrlf false\n", 206 | "\n", 207 | "# 配置打印历史commit的快捷命令\n", 208 | "!git config --global alias.lg \"log --oneline --graph --all\"" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "2,创建仓库" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 12, 221 | "metadata": { 222 | "collapsed": false 223 | }, 224 | "outputs": [ 225 | { 226 | "name": "stdout", 227 | "output_type": "stream", 228 | "text": [ 229 | "F:\\shidayunpan\\allCCTVResearch\\PythonFiles\\GitHub\\git-learn\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "%mkdir git-learn\n", 235 | "%cd git-learn" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 13, 241 | "metadata": { 242 | "collapsed": false 243 | }, 244 | "outputs": [ 245 | { 246 | "name": "stdout", 247 | "output_type": "stream", 248 | "text": [ 249 | "Initialized empty Git repository in F:/shidayunpan/allCCTVResearch/PythonFiles/GitHub/git-learn/.git/\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "!git init" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | "3,提交文件" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 14, 267 | "metadata": { 268 | "collapsed": true 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "!echo \"hello world\" >>readme.txt" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 15, 278 | "metadata": { 279 | "collapsed": true 280 | }, 281 | "outputs": [], 282 | "source": [ 283 | "!git add -A" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": 16, 289 | "metadata": { 290 | "collapsed": false 291 | }, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "On branch master\n", 298 | "\n", 299 | "No commits yet\n", 300 | "\n", 301 | "Changes to be committed:\n", 302 | " (use \"git rm --cached ...\" to unstage)\n", 303 | "\n", 304 | "\tnew file: readme.txt\n", 305 | "\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "!git status" 311 | ] 312 | }, 313 | { 314 | "cell_type": "code", 315 | "execution_count": 18, 316 | "metadata": { 317 | "collapsed": false 318 | }, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "On branch master\n", 325 | "nothing to commit, working tree clean\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "!git commit -m\"add readme.txt\"" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "4,推送远程" 338 | ] 339 | }, 340 | { 341 | "cell_type": "code", 342 | "execution_count": 33, 343 | "metadata": { 344 | "collapsed": false 345 | }, 346 | "outputs": [], 347 | "source": [ 348 | "!git remote add origin https://github.com/lyhue1991/git-learn" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 35, 354 | "metadata": { 355 | "collapsed": false 356 | }, 357 | "outputs": [ 358 | { 359 | "name": "stdout", 360 | "output_type": "stream", 361 | "text": [ 362 | "Branch 'master' set up to track remote branch 'master' from 'origin'.\n" 363 | ] 364 | }, 365 | { 366 | "name": "stderr", 367 | "output_type": "stream", 368 | "text": [ 369 | "To https://github.com/lyhue1991/git-learn\n", 370 | " * [new branch] master -> master\n" 371 | ] 372 | } 373 | ], 374 | "source": [ 375 | "!git push -u origin master" 376 | ] 377 | }, 378 | { 379 | "cell_type": "markdown", 380 | "metadata": {}, 381 | "source": [ 382 | "5,克隆远程" 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": 37, 388 | "metadata": { 389 | "collapsed": false 390 | }, 391 | "outputs": [ 392 | { 393 | "name": "stderr", 394 | "output_type": "stream", 395 | "text": [ 396 | "Cloning into '../liaoxuefeng'...\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "!git clone https://github.com/lyhue1991/Git-Tutorial-By-liaoxuefeng ../liaoxuefeng" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": {}, 407 | "source": [ 408 | "### 四,常用命令列表" 409 | ] 410 | }, 411 | { 412 | "cell_type": "markdown", 413 | "metadata": {}, 414 | "source": [ 415 | "**列了22个常用命令名**。\n", 416 | "\n", 417 | "**举了44个示范命令**。" 418 | ] 419 | }, 420 | { 421 | "cell_type": "markdown", 422 | "metadata": {}, 423 | "source": [ 424 | "![](Git常用命令.bmp)" 425 | ] 426 | }, 427 | { 428 | "cell_type": "code", 429 | "execution_count": null, 430 | "metadata": { 431 | "collapsed": true 432 | }, 433 | "outputs": [], 434 | "source": [ 435 | "\n", 436 | "\n", 437 | "\n", 438 | "\n", 439 | "\n", 440 | "\n", 441 | "\n", 442 | "\n", 443 | "\n", 444 | "\n", 445 | "\n", 446 | "\n", 447 | "\n", 448 | "\n", 449 | "\n", 450 | "\n" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": {}, 456 | "source": [ 457 | "### 五,版本回退" 458 | ] 459 | }, 460 | { 461 | "cell_type": "markdown", 462 | "metadata": {}, 463 | "source": [ 464 | "通过 git reset HEAD^ 可以回退到上一个版本。\n", 465 | "\n", 466 | "通过git reset HEAD^^ 可以回退到上上个版本。\n", 467 | "\n", 468 | "通过 git reset a234b3 可以回退到版本号为 a234b3的版本。\n", 469 | "\n", 470 | "git 命令的选项如下:\n", 471 | "\n", 472 | "git reset --hard head^ 修改版本库,保留暂存区,保留工作区\n", 473 | "\n", 474 | "git reset --mixed head^ 修改版本库,修改暂存区,保留工作区\n", 475 | "\n", 476 | "git reset --soft head^ 修改版本库,保留暂存区,保留工作区" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 2, 482 | "metadata": { 483 | "collapsed": false 484 | }, 485 | "outputs": [ 486 | { 487 | "name": "stdout", 488 | "output_type": "stream", 489 | "text": [ 490 | "F:\\shidayunpan\\allCCTVResearch\\PythonFiles\\GitHub\\git-learn\n" 491 | ] 492 | } 493 | ], 494 | "source": [ 495 | "%cd git-learn" 496 | ] 497 | }, 498 | { 499 | "cell_type": "code", 500 | "execution_count": 3, 501 | "metadata": { 502 | "collapsed": true 503 | }, 504 | "outputs": [], 505 | "source": [ 506 | "!gitk" 507 | ] 508 | }, 509 | { 510 | "cell_type": "code", 511 | "execution_count": null, 512 | "metadata": { 513 | "collapsed": true 514 | }, 515 | "outputs": [], 516 | "source": [ 517 | "# %load readme.txt\n", 518 | "\"hello world\" \n" 519 | ] 520 | }, 521 | { 522 | "cell_type": "code", 523 | "execution_count": 5, 524 | "metadata": { 525 | "collapsed": true 526 | }, 527 | "outputs": [], 528 | "source": [ 529 | "!echo \"hello China\" >> readme.txt" 530 | ] 531 | }, 532 | { 533 | "cell_type": "code", 534 | "execution_count": 6, 535 | "metadata": { 536 | "collapsed": true 537 | }, 538 | "outputs": [], 539 | "source": [ 540 | "%pycat readme.txt #查看readme.txt文件" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 7, 546 | "metadata": { 547 | "collapsed": false 548 | }, 549 | "outputs": [ 550 | { 551 | "name": "stdout", 552 | "output_type": "stream", 553 | "text": [ 554 | "[master 872640d] C2\n", 555 | " 1 file changed, 1 insertion(+)\n" 556 | ] 557 | } 558 | ], 559 | "source": [ 560 | "!git commit -a -m\"C2\"" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 9, 566 | "metadata": { 567 | "collapsed": true 568 | }, 569 | "outputs": [], 570 | "source": [ 571 | "!echo \"hello BeiJing\" >>readme.txt" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 10, 577 | "metadata": { 578 | "collapsed": true 579 | }, 580 | "outputs": [], 581 | "source": [ 582 | "!git add -A" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 11, 588 | "metadata": { 589 | "collapsed": false 590 | }, 591 | "outputs": [ 592 | { 593 | "name": "stdout", 594 | "output_type": "stream", 595 | "text": [ 596 | "[master 1c73eda] C3\n", 597 | " 1 file changed, 1 insertion(+)\n" 598 | ] 599 | } 600 | ], 601 | "source": [ 602 | "!git commit -m\"C3\"" 603 | ] 604 | }, 605 | { 606 | "cell_type": "code", 607 | "execution_count": 13, 608 | "metadata": { 609 | "collapsed": true 610 | }, 611 | "outputs": [], 612 | "source": [ 613 | "!gitk" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": 14, 619 | "metadata": { 620 | "collapsed": false 621 | }, 622 | "outputs": [ 623 | { 624 | "name": "stdout", 625 | "output_type": "stream", 626 | "text": [ 627 | "* 1c73eda C3\n", 628 | "* 872640d C2\n", 629 | "* f7c18e9 C1\n" 630 | ] 631 | } 632 | ], 633 | "source": [ 634 | "!git log --oneline --graph --all" 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "execution_count": 18, 640 | "metadata": { 641 | "collapsed": true 642 | }, 643 | "outputs": [], 644 | "source": [ 645 | "!git reset HEAD^" 646 | ] 647 | }, 648 | { 649 | "cell_type": "code", 650 | "execution_count": 23, 651 | "metadata": { 652 | "collapsed": false 653 | }, 654 | "outputs": [ 655 | { 656 | "name": "stdout", 657 | "output_type": "stream", 658 | "text": [ 659 | "HEAD is now at f7c18e9 C1\n" 660 | ] 661 | } 662 | ], 663 | "source": [ 664 | "!git reset --hard HEAD^" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 25, 670 | "metadata": { 671 | "collapsed": false 672 | }, 673 | "outputs": [], 674 | "source": [ 675 | "%pycat readme.txt" 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": 33, 681 | "metadata": { 682 | "collapsed": false 683 | }, 684 | "outputs": [ 685 | { 686 | "name": "stdout", 687 | "output_type": "stream", 688 | "text": [ 689 | "* f7c18e9 C1\n" 690 | ] 691 | } 692 | ], 693 | "source": [ 694 | "!git log --oneline --graph" 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": null, 700 | "metadata": { 701 | "collapsed": true 702 | }, 703 | "outputs": [], 704 | "source": [] 705 | }, 706 | { 707 | "cell_type": "markdown", 708 | "metadata": {}, 709 | "source": [ 710 | "### 六,版本前进" 711 | ] 712 | }, 713 | { 714 | "cell_type": "markdown", 715 | "metadata": {}, 716 | "source": [ 717 | "如果 版本回退后想回到会退前的版本,可以用git reflog查看该版本的 commit id 进而根据版本号进行恢复。" 718 | ] 719 | }, 720 | { 721 | "cell_type": "code", 722 | "execution_count": 34, 723 | "metadata": { 724 | "collapsed": false 725 | }, 726 | "outputs": [ 727 | { 728 | "name": "stdout", 729 | "output_type": "stream", 730 | "text": [ 731 | "f7c18e9 HEAD@{0}: reset: moving to head\n", 732 | "f7c18e9 HEAD@{1}: reset: moving to head^\n", 733 | "872640d HEAD@{2}: reset: moving to head^\n", 734 | "1c73eda HEAD@{3}: reset: moving to head\n", 735 | "1c73eda HEAD@{4}: reset: moving to HEAD\n", 736 | "1c73eda HEAD@{5}: reset: moving to head\n", 737 | "1c73eda HEAD@{6}: commit: C3\n", 738 | "872640d HEAD@{7}: commit: C2\n", 739 | "f7c18e9 HEAD@{8}: commit (initial): C1\n" 740 | ] 741 | } 742 | ], 743 | "source": [ 744 | "!git reflog" 745 | ] 746 | }, 747 | { 748 | "cell_type": "code", 749 | "execution_count": 42, 750 | "metadata": { 751 | "collapsed": false 752 | }, 753 | "outputs": [ 754 | { 755 | "name": "stdout", 756 | "output_type": "stream", 757 | "text": [ 758 | "HEAD is now at 1c73eda C3\n" 759 | ] 760 | } 761 | ], 762 | "source": [ 763 | "!git reset --hard 1c73eda" 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": 43, 769 | "metadata": { 770 | "collapsed": false 771 | }, 772 | "outputs": [ 773 | { 774 | "name": "stdout", 775 | "output_type": "stream", 776 | "text": [ 777 | "On branch master\n", 778 | "Your branch is ahead of 'origin/master' by 2 commits.\n", 779 | " (use \"git push\" to publish your local commits)\n", 780 | "\n", 781 | "nothing to commit, working tree clean\n" 782 | ] 783 | } 784 | ], 785 | "source": [ 786 | "!git status" 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": 45, 792 | "metadata": { 793 | "collapsed": false 794 | }, 795 | "outputs": [], 796 | "source": [ 797 | "%pycat readme.txt" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": null, 803 | "metadata": { 804 | "collapsed": true 805 | }, 806 | "outputs": [], 807 | "source": [ 808 | "\n", 809 | "\n", 810 | "\n", 811 | "\n", 812 | "\n", 813 | "\n", 814 | "\n", 815 | "\n", 816 | "\n", 817 | "\n", 818 | "\n", 819 | "\n", 820 | "\n", 821 | "\n", 822 | "\n", 823 | "\n" 824 | ] 825 | }, 826 | { 827 | "cell_type": "markdown", 828 | "metadata": {}, 829 | "source": [ 830 | "### 七,撤销修改" 831 | ] 832 | }, 833 | { 834 | "cell_type": "markdown", 835 | "metadata": {}, 836 | "source": [ 837 | "需要撤销工作区的修改,使用git checkout -- filename。如果文件之前已经添加到暂存区,则会用暂存区内容覆盖,如果暂存区已经被提交过了,则会用版本库中最新的版本覆盖。\n", 838 | "\n", 839 | "git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令。\n", 840 | "\n", 841 | "如果文件已经提交过了,但想用版本库中的最新版本文件覆盖工作区文件,可以用git reset HEAD" 842 | ] 843 | }, 844 | { 845 | "cell_type": "code", 846 | "execution_count": 53, 847 | "metadata": { 848 | "collapsed": false 849 | }, 850 | "outputs": [ 851 | { 852 | "name": "stdout", 853 | "output_type": "stream", 854 | "text": [ 855 | "On branch master\n", 856 | "Your branch is up to date with 'origin/master'.\n", 857 | "\n", 858 | "nothing to commit, working tree clean\n" 859 | ] 860 | } 861 | ], 862 | "source": [ 863 | "!git status" 864 | ] 865 | }, 866 | { 867 | "cell_type": "code", 868 | "execution_count": 54, 869 | "metadata": { 870 | "collapsed": true 871 | }, 872 | "outputs": [], 873 | "source": [ 874 | "%pycat readme.txt" 875 | ] 876 | }, 877 | { 878 | "cell_type": "code", 879 | "execution_count": 58, 880 | "metadata": { 881 | "collapsed": true 882 | }, 883 | "outputs": [], 884 | "source": [ 885 | "%echo \"hello Haidian\">>readme.txt" 886 | ] 887 | }, 888 | { 889 | "cell_type": "code", 890 | "execution_count": 59, 891 | "metadata": { 892 | "collapsed": true 893 | }, 894 | "outputs": [], 895 | "source": [ 896 | "%pycat readme.txt" 897 | ] 898 | }, 899 | { 900 | "cell_type": "code", 901 | "execution_count": 57, 902 | "metadata": { 903 | "collapsed": true 904 | }, 905 | "outputs": [], 906 | "source": [ 907 | "!git add -A" 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "execution_count": 60, 913 | "metadata": { 914 | "collapsed": true 915 | }, 916 | "outputs": [], 917 | "source": [ 918 | "!git checkout -- readme.txt" 919 | ] 920 | }, 921 | { 922 | "cell_type": "code", 923 | "execution_count": 61, 924 | "metadata": { 925 | "collapsed": true 926 | }, 927 | "outputs": [], 928 | "source": [ 929 | "%pycat readme.txt" 930 | ] 931 | }, 932 | { 933 | "cell_type": "code", 934 | "execution_count": 65, 935 | "metadata": { 936 | "collapsed": false 937 | }, 938 | "outputs": [], 939 | "source": [ 940 | "!git checkout -- readme.txt" 941 | ] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": 67, 946 | "metadata": { 947 | "collapsed": false 948 | }, 949 | "outputs": [], 950 | "source": [ 951 | "%pycat readme.txt" 952 | ] 953 | }, 954 | { 955 | "cell_type": "markdown", 956 | "metadata": {}, 957 | "source": [ 958 | "### 八,删除文件" 959 | ] 960 | }, 961 | { 962 | "cell_type": "markdown", 963 | "metadata": {}, 964 | "source": [ 965 | "删除文件也是一种修改,git跟踪的是修改而非文件。因此删除文件可以直接在工作区手动删除文件,然后add -A 再 commit即可。对于已经提交过的文件,也可以用 git rm newfile.txt 直接删除工作区和暂存区文件,然后再 commit。" 966 | ] 967 | }, 968 | { 969 | "cell_type": "code", 970 | "execution_count": 78, 971 | "metadata": { 972 | "collapsed": true 973 | }, 974 | "outputs": [], 975 | "source": [ 976 | "!echo 'delete me'>> newfile.txt" 977 | ] 978 | }, 979 | { 980 | "cell_type": "code", 981 | "execution_count": 79, 982 | "metadata": { 983 | "collapsed": true 984 | }, 985 | "outputs": [], 986 | "source": [ 987 | "%pycat newfile.txt" 988 | ] 989 | }, 990 | { 991 | "cell_type": "code", 992 | "execution_count": 82, 993 | "metadata": { 994 | "collapsed": false 995 | }, 996 | "outputs": [ 997 | { 998 | "name": "stdout", 999 | "output_type": "stream", 1000 | "text": [ 1001 | "[master 6ed6005] add a newfile\n", 1002 | " 1 file changed, 1 insertion(+)\n", 1003 | " create mode 100644 newfile.txt\n" 1004 | ] 1005 | } 1006 | ], 1007 | "source": [ 1008 | "!git add -A\n", 1009 | "!git commit -m\"add a newfile\"" 1010 | ] 1011 | }, 1012 | { 1013 | "cell_type": "code", 1014 | "execution_count": 84, 1015 | "metadata": { 1016 | "collapsed": false 1017 | }, 1018 | "outputs": [ 1019 | { 1020 | "name": "stdout", 1021 | "output_type": "stream", 1022 | "text": [ 1023 | "rm 'newfile.txt'\n" 1024 | ] 1025 | } 1026 | ], 1027 | "source": [ 1028 | "!git rm newfile.txt" 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "code", 1033 | "execution_count": 87, 1034 | "metadata": { 1035 | "collapsed": false 1036 | }, 1037 | "outputs": [ 1038 | { 1039 | "name": "stdout", 1040 | "output_type": "stream", 1041 | "text": [ 1042 | "[master c213efd] delete new file\n", 1043 | " 1 file changed, 1 deletion(-)\n", 1044 | " delete mode 100644 newfile.txt\n" 1045 | ] 1046 | } 1047 | ], 1048 | "source": [ 1049 | "!git commit -m\"delete new file\"" 1050 | ] 1051 | }, 1052 | { 1053 | "cell_type": "code", 1054 | "execution_count": 88, 1055 | "metadata": { 1056 | "collapsed": true 1057 | }, 1058 | "outputs": [], 1059 | "source": [ 1060 | "!gitk" 1061 | ] 1062 | }, 1063 | { 1064 | "cell_type": "code", 1065 | "execution_count": null, 1066 | "metadata": { 1067 | "collapsed": true 1068 | }, 1069 | "outputs": [], 1070 | "source": [ 1071 | "\n", 1072 | "\n", 1073 | "\n", 1074 | "\n", 1075 | "\n", 1076 | "\n", 1077 | "\n", 1078 | "\n", 1079 | "\n" 1080 | ] 1081 | }, 1082 | { 1083 | "cell_type": "markdown", 1084 | "metadata": {}, 1085 | "source": [ 1086 | "### 九,比较文件" 1087 | ] 1088 | }, 1089 | { 1090 | "cell_type": "markdown", 1091 | "metadata": {}, 1092 | "source": [ 1093 | "git diff 可以比较不同版本库的同名文件。" 1094 | ] 1095 | }, 1096 | { 1097 | "cell_type": "markdown", 1098 | "metadata": {}, 1099 | "source": [ 1100 | "![git](./git的diff原理.png)" 1101 | ] 1102 | }, 1103 | { 1104 | "cell_type": "code", 1105 | "execution_count": 91, 1106 | "metadata": { 1107 | "collapsed": true 1108 | }, 1109 | "outputs": [], 1110 | "source": [ 1111 | "%echo \"hello HaiDian\" >>readme.txt" 1112 | ] 1113 | }, 1114 | { 1115 | "cell_type": "code", 1116 | "execution_count": 96, 1117 | "metadata": { 1118 | "collapsed": false 1119 | }, 1120 | "outputs": [], 1121 | "source": [ 1122 | "!git diff readme.txt" 1123 | ] 1124 | }, 1125 | { 1126 | "cell_type": "code", 1127 | "execution_count": 95, 1128 | "metadata": { 1129 | "collapsed": true 1130 | }, 1131 | "outputs": [], 1132 | "source": [ 1133 | "!git add readme.txt" 1134 | ] 1135 | }, 1136 | { 1137 | "cell_type": "code", 1138 | "execution_count": 97, 1139 | "metadata": { 1140 | "collapsed": false 1141 | }, 1142 | "outputs": [ 1143 | { 1144 | "name": "stdout", 1145 | "output_type": "stream", 1146 | "text": [ 1147 | "diff --git a/readme.txt b/readme.txt\n", 1148 | "index ffbdc42..9ad0098 100644\n", 1149 | "--- a/readme.txt\n", 1150 | "+++ b/readme.txt\n", 1151 | "@@ -1,3 +1,4 @@\n", 1152 | " \"hello world\" \n", 1153 | " \"hello China\" \n", 1154 | " \"hello BeiJing\" \n", 1155 | "+\"hello HaiDian\" \n" 1156 | ] 1157 | } 1158 | ], 1159 | "source": [ 1160 | "!git diff --cached readme.txt" 1161 | ] 1162 | }, 1163 | { 1164 | "cell_type": "markdown", 1165 | "metadata": {}, 1166 | "source": [ 1167 | "### 十,添加远程" 1168 | ] 1169 | }, 1170 | { 1171 | "cell_type": "markdown", 1172 | "metadata": {}, 1173 | "source": [ 1174 | "通常可以用SSH协议和远程库通信或使用http协议和远程库通信。\n", 1175 | "\n", 1176 | "http协议较为方便,但SSH协议方式速度较快。" 1177 | ] 1178 | }, 1179 | { 1180 | "cell_type": "markdown", 1181 | "metadata": {}, 1182 | "source": [ 1183 | "ssh-keygen -t rsa -C \"your_emial@xxx.com\"" 1184 | ] 1185 | }, 1186 | { 1187 | "cell_type": "markdown", 1188 | "metadata": {}, 1189 | "source": [ 1190 | "在 git bash中运行上述命令,并一路回车,在用户主目录里找到.ssh目录。\n", 1191 | "\n", 1192 | "目录下的id_rsa有私钥,不能泄露出去。里面的id_rsa.pub是公钥,可分享给别人。\n", 1193 | "\n", 1194 | "在GitHub——> settings ——> SSH Keys 页面添加SSH公钥。" 1195 | ] 1196 | }, 1197 | { 1198 | "cell_type": "markdown", 1199 | "metadata": {}, 1200 | "source": [ 1201 | "ssh -T git@github.com" 1202 | ] 1203 | }, 1204 | { 1205 | "cell_type": "markdown", 1206 | "metadata": {}, 1207 | "source": [ 1208 | "再在 git bash中运行上述命令,使得本机和github建立SSH连接" 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "code", 1213 | "execution_count": 98, 1214 | "metadata": { 1215 | "collapsed": true 1216 | }, 1217 | "outputs": [], 1218 | "source": [ 1219 | "# 可以使用SSH地址添加远程库\n", 1220 | "!git remote add github git@github.com:lyhue1991/git-learn" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "code", 1225 | "execution_count": 102, 1226 | "metadata": { 1227 | "collapsed": false 1228 | }, 1229 | "outputs": [], 1230 | "source": [ 1231 | "# 也可以使用http地址添加远程库 #添加码云\n", 1232 | "!git remote add gitee https://gitee.com/Python_Ai_Road/git-learn " 1233 | ] 1234 | }, 1235 | { 1236 | "cell_type": "code", 1237 | "execution_count": 103, 1238 | "metadata": { 1239 | "collapsed": false 1240 | }, 1241 | "outputs": [ 1242 | { 1243 | "name": "stdout", 1244 | "output_type": "stream", 1245 | "text": [ 1246 | "gitee\thttps://gitee.com/Python_Ai_Road/git-learn (fetch)\n", 1247 | "gitee\thttps://gitee.com/Python_Ai_Road/git-learn (push)\n", 1248 | "github\tgit@github.com:lyhue1991/git-learn (fetch)\n", 1249 | "github\tgit@github.com:lyhue1991/git-learn (push)\n", 1250 | "origin\thttps://github.com/lyhue1991/git-learn (fetch)\n", 1251 | "origin\thttps://github.com/lyhue1991/git-learn (push)\n" 1252 | ] 1253 | } 1254 | ], 1255 | "source": [ 1256 | "!git remote -v" 1257 | ] 1258 | }, 1259 | { 1260 | "cell_type": "code", 1261 | "execution_count": 104, 1262 | "metadata": { 1263 | "collapsed": true 1264 | }, 1265 | "outputs": [], 1266 | "source": [ 1267 | "!git remote remove origin" 1268 | ] 1269 | }, 1270 | { 1271 | "cell_type": "code", 1272 | "execution_count": 105, 1273 | "metadata": { 1274 | "collapsed": false 1275 | }, 1276 | "outputs": [ 1277 | { 1278 | "name": "stdout", 1279 | "output_type": "stream", 1280 | "text": [ 1281 | "gitee\n", 1282 | "github\n" 1283 | ] 1284 | } 1285 | ], 1286 | "source": [ 1287 | "!git remote" 1288 | ] 1289 | }, 1290 | { 1291 | "cell_type": "code", 1292 | "execution_count": null, 1293 | "metadata": { 1294 | "collapsed": true 1295 | }, 1296 | "outputs": [], 1297 | "source": [ 1298 | "\n", 1299 | "\n", 1300 | "\n", 1301 | "\n", 1302 | "\n", 1303 | "\n", 1304 | "\n", 1305 | "\n", 1306 | "\n", 1307 | "\n", 1308 | "\n", 1309 | "\n", 1310 | "\n", 1311 | "\n", 1312 | "\n", 1313 | "\n" 1314 | ] 1315 | }, 1316 | { 1317 | "cell_type": "markdown", 1318 | "metadata": {}, 1319 | "source": [ 1320 | "### 十一,推送远程" 1321 | ] 1322 | }, 1323 | { 1324 | "cell_type": "markdown", 1325 | "metadata": {}, 1326 | "source": [ 1327 | "使用 git push 命令将当前仓库内容推送至远程仓库。" 1328 | ] 1329 | }, 1330 | { 1331 | "cell_type": "code", 1332 | "execution_count": 8, 1333 | "metadata": { 1334 | "collapsed": false 1335 | }, 1336 | "outputs": [ 1337 | { 1338 | "name": "stdout", 1339 | "output_type": "stream", 1340 | "text": [ 1341 | "gitee\thttps://gitee.com/Python_Ai_Road/git-learn (fetch)\n", 1342 | "gitee\thttps://gitee.com/Python_Ai_Road/git-learn (push)\n", 1343 | "github\tgit@github.com:lyhue1991/git-learn (fetch)\n", 1344 | "github\tgit@github.com:lyhue1991/git-learn (push)\n" 1345 | ] 1346 | } 1347 | ], 1348 | "source": [ 1349 | "!git remote -v" 1350 | ] 1351 | }, 1352 | { 1353 | "cell_type": "code", 1354 | "execution_count": 18, 1355 | "metadata": { 1356 | "collapsed": false 1357 | }, 1358 | "outputs": [ 1359 | { 1360 | "name": "stdout", 1361 | "output_type": "stream", 1362 | "text": [ 1363 | "Branch 'master' set up to track remote branch 'master' from 'github'.\n" 1364 | ] 1365 | }, 1366 | { 1367 | "name": "stderr", 1368 | "output_type": "stream", 1369 | "text": [ 1370 | "Everything up-to-date\n" 1371 | ] 1372 | } 1373 | ], 1374 | "source": [ 1375 | "!git push -u github master\n", 1376 | "# -u表示指定github为默认远程仓库,后面就可以不加任何参数使用git push了" 1377 | ] 1378 | }, 1379 | { 1380 | "cell_type": "code", 1381 | "execution_count": 9, 1382 | "metadata": { 1383 | "collapsed": true 1384 | }, 1385 | "outputs": [], 1386 | "source": [ 1387 | "!echo \"Hello TianAnMen!\">>readme.txt" 1388 | ] 1389 | }, 1390 | { 1391 | "cell_type": "code", 1392 | "execution_count": 10, 1393 | "metadata": { 1394 | "collapsed": true 1395 | }, 1396 | "outputs": [], 1397 | "source": [ 1398 | "%pycat readme.txt" 1399 | ] 1400 | }, 1401 | { 1402 | "cell_type": "code", 1403 | "execution_count": 11, 1404 | "metadata": { 1405 | "collapsed": true 1406 | }, 1407 | "outputs": [], 1408 | "source": [ 1409 | "!git add -A" 1410 | ] 1411 | }, 1412 | { 1413 | "cell_type": "code", 1414 | "execution_count": 13, 1415 | "metadata": { 1416 | "collapsed": false 1417 | }, 1418 | "outputs": [ 1419 | { 1420 | "name": "stdout", 1421 | "output_type": "stream", 1422 | "text": [ 1423 | "[master b3f4690] C6\n", 1424 | " 1 file changed, 2 insertions(+)\n" 1425 | ] 1426 | } 1427 | ], 1428 | "source": [ 1429 | "!git commit -m\"C6\"" 1430 | ] 1431 | }, 1432 | { 1433 | "cell_type": "code", 1434 | "execution_count": 14, 1435 | "metadata": { 1436 | "collapsed": false 1437 | }, 1438 | "outputs": [ 1439 | { 1440 | "name": "stderr", 1441 | "output_type": "stream", 1442 | "text": [ 1443 | "To github.com:lyhue1991/git-learn\n", 1444 | " c213efd..b3f4690 master -> master\n" 1445 | ] 1446 | } 1447 | ], 1448 | "source": [ 1449 | "!git push" 1450 | ] 1451 | }, 1452 | { 1453 | "cell_type": "code", 1454 | "execution_count": 15, 1455 | "metadata": { 1456 | "collapsed": true 1457 | }, 1458 | "outputs": [], 1459 | "source": [ 1460 | "!gitk" 1461 | ] 1462 | }, 1463 | { 1464 | "cell_type": "markdown", 1465 | "metadata": {}, 1466 | "source": [ 1467 | "### 十二,克隆远程" 1468 | ] 1469 | }, 1470 | { 1471 | "cell_type": "markdown", 1472 | "metadata": {}, 1473 | "source": [ 1474 | "git clone可以将远程仓库克隆至本地某一目录,相当于下载。不指定仓库时,会克隆到当前目录下的一个文件夹。通常在github中把感兴趣的别人的项目fork到自己的仓库里,然后再使用clone方法下载到本地进行编辑。" 1475 | ] 1476 | }, 1477 | { 1478 | "cell_type": "code", 1479 | "execution_count": 19, 1480 | "metadata": { 1481 | "collapsed": true 1482 | }, 1483 | "outputs": [], 1484 | "source": [ 1485 | "在github中左上角搜索 python教程,找到感兴趣的内容并fork之。" 1486 | ] 1487 | }, 1488 | { 1489 | "cell_type": "code", 1490 | "execution_count": 20, 1491 | "metadata": { 1492 | "collapsed": true 1493 | }, 1494 | "outputs": [ 1495 | { 1496 | "name": "stderr", 1497 | "output_type": "stream", 1498 | "text": [ 1499 | "Cloning into '../python-basic'...\n", 1500 | "Checking out files: 32% (38/118) \n", 1501 | "Checking out files: 33% (39/118) \n", 1502 | "Checking out files: 34% (41/118) \n", 1503 | "Checking out files: 35% (42/118) \n", 1504 | "Checking out files: 36% (43/118) \n", 1505 | "Checking out files: 37% (44/118) \n", 1506 | "Checking out files: 38% (45/118) \n", 1507 | "Checking out files: 39% (47/118) \n", 1508 | "Checking out files: 40% (48/118) \n", 1509 | "Checking out files: 41% (49/118) \n", 1510 | "Checking out files: 42% (50/118) \n", 1511 | "Checking out files: 43% (51/118) \n", 1512 | "Checking out files: 44% (52/118) \n", 1513 | "Checking out files: 45% (54/118) \n", 1514 | "Checking out files: 46% (55/118) \n", 1515 | "Checking out files: 47% (56/118) \n", 1516 | "Checking out files: 48% (57/118) \n", 1517 | "Checking out files: 49% (58/118) \n", 1518 | "Checking out files: 50% (59/118) \n", 1519 | "Checking out files: 51% (61/118) \n", 1520 | "Checking out files: 52% (62/118) \n", 1521 | "Checking out files: 53% (63/118) \n", 1522 | "Checking out files: 54% (64/118) \n", 1523 | "Checking out files: 55% (65/118) \n", 1524 | "Checking out files: 56% (67/118) \n", 1525 | "Checking out files: 57% (68/118) \n", 1526 | "Checking out files: 58% (69/118) \n", 1527 | "Checking out files: 59% (70/118) \n", 1528 | "Checking out files: 60% (71/118) \n", 1529 | "Checking out files: 61% (72/118) \n", 1530 | "Checking out files: 62% (74/118) \n", 1531 | "Checking out files: 63% (75/118) \n", 1532 | "Checking out files: 64% (76/118) \n", 1533 | "Checking out files: 65% (77/118) \n", 1534 | "Checking out files: 66% (78/118) \n", 1535 | "Checking out files: 67% (80/118) \n", 1536 | "Checking out files: 68% (81/118) \n", 1537 | "Checking out files: 69% (82/118) \n", 1538 | "Checking out files: 70% (83/118) \n", 1539 | "Checking out files: 71% (84/118) \n", 1540 | "Checking out files: 72% (85/118) \n", 1541 | "Checking out files: 73% (87/118) \n", 1542 | "Checking out files: 74% (88/118) \n", 1543 | "Checking out files: 75% (89/118) \n", 1544 | "Checking out files: 76% (90/118) \n", 1545 | "Checking out files: 77% (91/118) \n", 1546 | "Checking out files: 78% (93/118) \n", 1547 | "Checking out files: 79% (94/118) \n", 1548 | "Checking out files: 80% (95/118) \n", 1549 | "Checking out files: 81% (96/118) \n", 1550 | "Checking out files: 82% (97/118) \n", 1551 | "Checking out files: 83% (98/118) \n", 1552 | "Checking out files: 84% (100/118) \n", 1553 | "Checking out files: 85% (101/118) \n", 1554 | "Checking out files: 86% (102/118) \n", 1555 | "Checking out files: 87% (103/118) \n", 1556 | "Checking out files: 88% (104/118) \n", 1557 | "Checking out files: 89% (106/118) \n", 1558 | "Checking out files: 90% (107/118) \n", 1559 | "Checking out files: 91% (108/118) \n", 1560 | "Checking out files: 92% (109/118) \n", 1561 | "Checking out files: 93% (110/118) \n", 1562 | "Checking out files: 94% (111/118) \n", 1563 | "Checking out files: 95% (113/118) \n", 1564 | "Checking out files: 96% (114/118) \n", 1565 | "Checking out files: 97% (115/118) \n", 1566 | "Checking out files: 98% (116/118) \n", 1567 | "Checking out files: 99% (117/118) \n", 1568 | "Checking out files: 100% (118/118) \n", 1569 | "Checking out files: 100% (118/118), done.\n" 1570 | ] 1571 | } 1572 | ], 1573 | "source": [ 1574 | "!git clone https://github.com/lyhue1991/python-basic ../python-basic\n", 1575 | " \n", 1576 | "# 通常用 ./表示当前目录,../表示当前目录的父目录" 1577 | ] 1578 | }, 1579 | { 1580 | "cell_type": "code", 1581 | "execution_count": 34, 1582 | "metadata": { 1583 | "collapsed": false 1584 | }, 1585 | "outputs": [ 1586 | { 1587 | "name": "stdout", 1588 | "output_type": "stream", 1589 | "text": [ 1590 | "F:\\shidayunpan\\allCCTVResearch\\PythonFiles\\GitHub\\python-basic\n" 1591 | ] 1592 | } 1593 | ], 1594 | "source": [ 1595 | "%cd ../python-basic" 1596 | ] 1597 | }, 1598 | { 1599 | "cell_type": "code", 1600 | "execution_count": 35, 1601 | "metadata": { 1602 | "collapsed": false 1603 | }, 1604 | "outputs": [ 1605 | { 1606 | "name": "stdout", 1607 | "output_type": "stream", 1608 | "text": [ 1609 | "On branch master\n", 1610 | "Your branch is up to date with 'origin/master'.\n", 1611 | "\n", 1612 | "nothing to commit, working tree clean\n" 1613 | ] 1614 | } 1615 | ], 1616 | "source": [ 1617 | "!git status" 1618 | ] 1619 | }, 1620 | { 1621 | "cell_type": "code", 1622 | "execution_count": 37, 1623 | "metadata": { 1624 | "collapsed": false 1625 | }, 1626 | "outputs": [], 1627 | "source": [ 1628 | "!gitk" 1629 | ] 1630 | }, 1631 | { 1632 | "cell_type": "markdown", 1633 | "metadata": {}, 1634 | "source": [ 1635 | "### 十三,更新远程" 1636 | ] 1637 | }, 1638 | { 1639 | "cell_type": "markdown", 1640 | "metadata": { 1641 | "collapsed": true 1642 | }, 1643 | "source": [ 1644 | "fetch能够获取远程仓库的更新,但不同步到本地仓库。\n", 1645 | "fetch获取到的是修改,不会获取文件,而clone会获取文件。\n", 1646 | "可以在fetch之后利用merge,rebase等方法合并并同步到本地。" 1647 | ] 1648 | }, 1649 | { 1650 | "cell_type": "markdown", 1651 | "metadata": { 1652 | "collapsed": true 1653 | }, 1654 | "source": [ 1655 | "在github中的git-learn项目下新建一个 test.txt文件并commit。" 1656 | ] 1657 | }, 1658 | { 1659 | "cell_type": "code", 1660 | "execution_count": 60, 1661 | "metadata": { 1662 | "collapsed": false 1663 | }, 1664 | "outputs": [ 1665 | { 1666 | "name": "stderr", 1667 | "output_type": "stream", 1668 | "text": [ 1669 | "From github.com:lyhue1991/git-learn\n", 1670 | " * branch master -> FETCH_HEAD\n" 1671 | ] 1672 | } 1673 | ], 1674 | "source": [ 1675 | "!git fetch github master" 1676 | ] 1677 | }, 1678 | { 1679 | "cell_type": "code", 1680 | "execution_count": 58, 1681 | "metadata": { 1682 | "collapsed": false 1683 | }, 1684 | "outputs": [ 1685 | { 1686 | "name": "stdout", 1687 | "output_type": "stream", 1688 | "text": [ 1689 | "On branch master\n", 1690 | "Your branch is behind 'github/master' by 1 commit, and can be fast-forwarded.\n", 1691 | " (use \"git pull\" to update your local branch)\n", 1692 | "\n", 1693 | "nothing to commit, working tree clean\n" 1694 | ] 1695 | } 1696 | ], 1697 | "source": [ 1698 | "!git status" 1699 | ] 1700 | }, 1701 | { 1702 | "cell_type": "code", 1703 | "execution_count": 66, 1704 | "metadata": { 1705 | "collapsed": false 1706 | }, 1707 | "outputs": [ 1708 | { 1709 | "name": "stdout", 1710 | "output_type": "stream", 1711 | "text": [ 1712 | "Updating b3f4690..2b04eab\n", 1713 | "Fast-forward\n", 1714 | " test.txt | 1 +\n", 1715 | " 1 file changed, 1 insertion(+)\n", 1716 | " create mode 100644 test.txt\n" 1717 | ] 1718 | } 1719 | ], 1720 | "source": [ 1721 | "!git merge github/master" 1722 | ] 1723 | }, 1724 | { 1725 | "cell_type": "code", 1726 | "execution_count": 67, 1727 | "metadata": { 1728 | "collapsed": true 1729 | }, 1730 | "outputs": [], 1731 | "source": [ 1732 | "!gitk" 1733 | ] 1734 | }, 1735 | { 1736 | "cell_type": "code", 1737 | "execution_count": null, 1738 | "metadata": { 1739 | "collapsed": true 1740 | }, 1741 | "outputs": [], 1742 | "source": [ 1743 | "\n", 1744 | "\n", 1745 | "\n", 1746 | "\n", 1747 | "\n", 1748 | "\n", 1749 | "\n", 1750 | "\n", 1751 | "\n", 1752 | "\n", 1753 | "\n", 1754 | "\n", 1755 | "\n", 1756 | "\n", 1757 | "\n", 1758 | "\n" 1759 | ] 1760 | }, 1761 | { 1762 | "cell_type": "markdown", 1763 | "metadata": {}, 1764 | "source": [ 1765 | "### 十四,合并远程" 1766 | ] 1767 | }, 1768 | { 1769 | "cell_type": "markdown", 1770 | "metadata": {}, 1771 | "source": [ 1772 | "git pull 能够或取远程仓库的更新,并同步合并到本地。\n", 1773 | "\n", 1774 | "相当于git fetch之后再merge。有时候会有冲突,需要解决。\n", 1775 | "\n", 1776 | "你可以在GitHub网站上的通过fork一些开源项目的代码,然后clone到本地后,尝试完善项目,再发送pull request到项目发起人,如果项目发起人通过了你的请求,那么你就为开源项目做出了贡献。\n" 1777 | ] 1778 | }, 1779 | { 1780 | "cell_type": "markdown", 1781 | "metadata": {}, 1782 | "source": [ 1783 | "在github中的对应git-learn的readme.txt的最后一行加上\"remote line\"并commit.\n", 1784 | "在本地的git-learn的readme.txt的最后一行加上\"local line\"并commit.\n", 1785 | "则会发生冲突。" 1786 | ] 1787 | }, 1788 | { 1789 | "cell_type": "code", 1790 | "execution_count": 68, 1791 | "metadata": { 1792 | "collapsed": true 1793 | }, 1794 | "outputs": [], 1795 | "source": [ 1796 | "!echo \"local line\" >>readme.txt" 1797 | ] 1798 | }, 1799 | { 1800 | "cell_type": "code", 1801 | "execution_count": 69, 1802 | "metadata": { 1803 | "collapsed": true 1804 | }, 1805 | "outputs": [], 1806 | "source": [ 1807 | "%pycat readme.txt" 1808 | ] 1809 | }, 1810 | { 1811 | "cell_type": "code", 1812 | "execution_count": 70, 1813 | "metadata": { 1814 | "collapsed": true 1815 | }, 1816 | "outputs": [], 1817 | "source": [ 1818 | "!git add -A" 1819 | ] 1820 | }, 1821 | { 1822 | "cell_type": "code", 1823 | "execution_count": 71, 1824 | "metadata": { 1825 | "collapsed": false 1826 | }, 1827 | "outputs": [ 1828 | { 1829 | "name": "stdout", 1830 | "output_type": "stream", 1831 | "text": [ 1832 | "[master 4e30082] update local\n", 1833 | " 1 file changed, 1 insertion(+)\n" 1834 | ] 1835 | } 1836 | ], 1837 | "source": [ 1838 | "!git commit -m\"update local\"" 1839 | ] 1840 | }, 1841 | { 1842 | "cell_type": "code", 1843 | "execution_count": 73, 1844 | "metadata": { 1845 | "collapsed": false 1846 | }, 1847 | "outputs": [ 1848 | { 1849 | "name": "stdout", 1850 | "output_type": "stream", 1851 | "text": [ 1852 | "Auto-merging readme.txt\n", 1853 | "CONFLICT (content): Merge conflict in readme.txt\n", 1854 | "Automatic merge failed; fix conflicts and then commit the result.\n" 1855 | ] 1856 | }, 1857 | { 1858 | "name": "stderr", 1859 | "output_type": "stream", 1860 | "text": [ 1861 | "From github.com:lyhue1991/git-learn\n", 1862 | " * branch master -> FETCH_HEAD\n", 1863 | " 2b04eab..2cc5f85 master -> github/master\n" 1864 | ] 1865 | } 1866 | ], 1867 | "source": [ 1868 | "!git pull github master" 1869 | ] 1870 | }, 1871 | { 1872 | "cell_type": "markdown", 1873 | "metadata": {}, 1874 | "source": [ 1875 | "尝试git pull,但是出现了冲突,在本地文件打开readme.txt根据指引解决冲突。\n", 1876 | "\n", 1877 | "解决冲突后add,commit,push即可。" 1878 | ] 1879 | }, 1880 | { 1881 | "cell_type": "code", 1882 | "execution_count": 100, 1883 | "metadata": { 1884 | "collapsed": false 1885 | }, 1886 | "outputs": [], 1887 | "source": [ 1888 | "!git add -A" 1889 | ] 1890 | }, 1891 | { 1892 | "cell_type": "code", 1893 | "execution_count": 101, 1894 | "metadata": { 1895 | "collapsed": false 1896 | }, 1897 | "outputs": [ 1898 | { 1899 | "name": "stdout", 1900 | "output_type": "stream", 1901 | "text": [ 1902 | "On branch master\n", 1903 | "Your branch is up to date with 'github/master'.\n", 1904 | "\n", 1905 | "nothing to commit, working tree clean\n" 1906 | ] 1907 | } 1908 | ], 1909 | "source": [ 1910 | "!git commit -m\"solve conflict\"" 1911 | ] 1912 | }, 1913 | { 1914 | "cell_type": "code", 1915 | "execution_count": 102, 1916 | "metadata": { 1917 | "collapsed": false 1918 | }, 1919 | "outputs": [ 1920 | { 1921 | "name": "stderr", 1922 | "output_type": "stream", 1923 | "text": [ 1924 | "Everything up-to-date\n" 1925 | ] 1926 | } 1927 | ], 1928 | "source": [ 1929 | "!git push" 1930 | ] 1931 | }, 1932 | { 1933 | "cell_type": "code", 1934 | "execution_count": null, 1935 | "metadata": { 1936 | "collapsed": true 1937 | }, 1938 | "outputs": [], 1939 | "source": [ 1940 | "\n", 1941 | "\n", 1942 | "\n", 1943 | "\n", 1944 | "\n", 1945 | "\n", 1946 | "\n", 1947 | "\n", 1948 | "\n", 1949 | "\n", 1950 | "\n", 1951 | "\n", 1952 | "\n", 1953 | "\n", 1954 | "\n" 1955 | ] 1956 | }, 1957 | { 1958 | "cell_type": "markdown", 1959 | "metadata": {}, 1960 | "source": [ 1961 | "### 十五,分支创建" 1962 | ] 1963 | }, 1964 | { 1965 | "cell_type": "markdown", 1966 | "metadata": {}, 1967 | "source": [ 1968 | "通常在团队协作时经常需要使用分支功能。开发时大家在不同的分支上工作,互不冲突。\n", 1969 | "\n", 1970 | "到一定开发阶段再使用分支合并功能综合大家的工作结果。\n", 1971 | "\n", 1972 | "默认的分支为master,可以用git branch develop在当前位置创建分支。" 1973 | ] 1974 | }, 1975 | { 1976 | "cell_type": "markdown", 1977 | "metadata": { 1978 | "collapsed": true 1979 | }, 1980 | "source": [ 1981 | "git分支最佳实践:" 1982 | ] 1983 | }, 1984 | { 1985 | "cell_type": "markdown", 1986 | "metadata": {}, 1987 | "source": [ 1988 | "![git分支](.\\git分支最佳实践.png)" 1989 | ] 1990 | }, 1991 | { 1992 | "cell_type": "markdown", 1993 | "metadata": {}, 1994 | "source": [ 1995 | "* master: 主分支,主要用来版本发布。\n", 1996 | "* develop:日常开发分支,该分支正常保存了开发的最新代码。\n", 1997 | "* feature:具体的功能开发分支,只与 develop 分支交互。\n", 1998 | "* release:release 分支可以认为是 master 分支的未测试版。比如说某一期的功能全部开发完成,那么就将 develop 分支合并到 release 分支,测试没有问题并且到了发布日期就合并到 master 分支,进行发布。\n", 1999 | "* hotfix:线上 bug 修复分支。\n" 2000 | ] 2001 | }, 2002 | { 2003 | "cell_type": "code", 2004 | "execution_count": null, 2005 | "metadata": { 2006 | "collapsed": true 2007 | }, 2008 | "outputs": [], 2009 | "source": [ 2010 | "#创建和删除分支" 2011 | ] 2012 | }, 2013 | { 2014 | "cell_type": "code", 2015 | "execution_count": 111, 2016 | "metadata": { 2017 | "collapsed": false 2018 | }, 2019 | "outputs": [], 2020 | "source": [ 2021 | "#在当前head位置创建分支 develop\n", 2022 | "!git branch develop " 2023 | ] 2024 | }, 2025 | { 2026 | "cell_type": "code", 2027 | "execution_count": 123, 2028 | "metadata": { 2029 | "collapsed": false 2030 | }, 2031 | "outputs": [ 2032 | { 2033 | "name": "stdout", 2034 | "output_type": "stream", 2035 | "text": [ 2036 | "* 4e30082 update local\n", 2037 | "* 2b04eab Create test.txt\n", 2038 | "* b3f4690 C6\n", 2039 | "* c213efd delete new file\n", 2040 | "* 6ed6005 add a newfile\n", 2041 | "* 1c73eda C3\n", 2042 | "* 872640d C2\n", 2043 | "* f7c18e9 C1\n" 2044 | ] 2045 | } 2046 | ], 2047 | "source": [ 2048 | "!git log --oneline --graph" 2049 | ] 2050 | }, 2051 | { 2052 | "cell_type": "code", 2053 | "execution_count": 122, 2054 | "metadata": { 2055 | "collapsed": false 2056 | }, 2057 | "outputs": [], 2058 | "source": [ 2059 | "# 在某个commit id 号位置创建分支 feature\n", 2060 | "!git branch feature 1c73eda" 2061 | ] 2062 | }, 2063 | { 2064 | "cell_type": "code", 2065 | "execution_count": 124, 2066 | "metadata": { 2067 | "collapsed": true 2068 | }, 2069 | "outputs": [], 2070 | "source": [ 2071 | "!gitk" 2072 | ] 2073 | }, 2074 | { 2075 | "cell_type": "code", 2076 | "execution_count": 125, 2077 | "metadata": { 2078 | "collapsed": false 2079 | }, 2080 | "outputs": [ 2081 | { 2082 | "name": "stdout", 2083 | "output_type": "stream", 2084 | "text": [ 2085 | " develop\n", 2086 | " feature\n", 2087 | "* master\n" 2088 | ] 2089 | } 2090 | ], 2091 | "source": [ 2092 | "!git branch" 2093 | ] 2094 | }, 2095 | { 2096 | "cell_type": "code", 2097 | "execution_count": 126, 2098 | "metadata": { 2099 | "collapsed": false 2100 | }, 2101 | "outputs": [ 2102 | { 2103 | "name": "stdout", 2104 | "output_type": "stream", 2105 | "text": [ 2106 | " develop\n", 2107 | " feature\n", 2108 | "* master\n", 2109 | " remotes/github/master\n" 2110 | ] 2111 | } 2112 | ], 2113 | "source": [ 2114 | "# 查看包括远程同步仓库的所有分支\n", 2115 | "!git branch -a" 2116 | ] 2117 | }, 2118 | { 2119 | "cell_type": "code", 2120 | "execution_count": 127, 2121 | "metadata": { 2122 | "collapsed": false 2123 | }, 2124 | "outputs": [ 2125 | { 2126 | "name": "stdout", 2127 | "output_type": "stream", 2128 | "text": [ 2129 | "Deleted branch feature (was 1c73eda).\n" 2130 | ] 2131 | } 2132 | ], 2133 | "source": [ 2134 | "# 删除分支 feature\n", 2135 | "!git branch -d feature" 2136 | ] 2137 | }, 2138 | { 2139 | "cell_type": "code", 2140 | "execution_count": 128, 2141 | "metadata": { 2142 | "collapsed": false 2143 | }, 2144 | "outputs": [ 2145 | { 2146 | "name": "stdout", 2147 | "output_type": "stream", 2148 | "text": [ 2149 | " develop\n", 2150 | "* master\n" 2151 | ] 2152 | } 2153 | ], 2154 | "source": [ 2155 | "!git branch" 2156 | ] 2157 | }, 2158 | { 2159 | "cell_type": "markdown", 2160 | "metadata": {}, 2161 | "source": [ 2162 | "### 十六,分支切换" 2163 | ] 2164 | }, 2165 | { 2166 | "cell_type": "markdown", 2167 | "metadata": { 2168 | "collapsed": true 2169 | }, 2170 | "source": [ 2171 | "使用 git checkout develop 可以从当前分支切换到 develop分支。\n", 2172 | "\n", 2173 | "使用 git checkout -b feature 可以在当前HEAD位置创建新分支 feature 并将HEAD切换到新分支。" 2174 | ] 2175 | }, 2176 | { 2177 | "cell_type": "code", 2178 | "execution_count": 130, 2179 | "metadata": { 2180 | "collapsed": false 2181 | }, 2182 | "outputs": [ 2183 | { 2184 | "name": "stderr", 2185 | "output_type": "stream", 2186 | "text": [ 2187 | "Switched to branch 'develop'\n" 2188 | ] 2189 | } 2190 | ], 2191 | "source": [ 2192 | "!git checkout develop" 2193 | ] 2194 | }, 2195 | { 2196 | "cell_type": "code", 2197 | "execution_count": 131, 2198 | "metadata": { 2199 | "collapsed": true 2200 | }, 2201 | "outputs": [], 2202 | "source": [ 2203 | "!gitk" 2204 | ] 2205 | }, 2206 | { 2207 | "cell_type": "code", 2208 | "execution_count": 132, 2209 | "metadata": { 2210 | "collapsed": false 2211 | }, 2212 | "outputs": [ 2213 | { 2214 | "name": "stderr", 2215 | "output_type": "stream", 2216 | "text": [ 2217 | "Switched to a new branch 'feature'\n" 2218 | ] 2219 | } 2220 | ], 2221 | "source": [ 2222 | "!git checkout -b feature" 2223 | ] 2224 | }, 2225 | { 2226 | "cell_type": "code", 2227 | "execution_count": 134, 2228 | "metadata": { 2229 | "collapsed": false 2230 | }, 2231 | "outputs": [], 2232 | "source": [ 2233 | "!gitk" 2234 | ] 2235 | }, 2236 | { 2237 | "cell_type": "code", 2238 | "execution_count": null, 2239 | "metadata": { 2240 | "collapsed": true 2241 | }, 2242 | "outputs": [], 2243 | "source": [ 2244 | "\n", 2245 | "\n", 2246 | "\n", 2247 | "\n", 2248 | "\n", 2249 | "\n", 2250 | "\n", 2251 | "\n", 2252 | "\n", 2253 | "\n", 2254 | "\n", 2255 | "\n", 2256 | "\n", 2257 | "\n", 2258 | "\n", 2259 | "\n", 2260 | "\n", 2261 | "\n" 2262 | ] 2263 | }, 2264 | { 2265 | "cell_type": "markdown", 2266 | "metadata": {}, 2267 | "source": [ 2268 | "### 十七,分支合并" 2269 | ] 2270 | }, 2271 | { 2272 | "cell_type": "markdown", 2273 | "metadata": { 2274 | "collapsed": true 2275 | }, 2276 | "source": [ 2277 | "merge方法可以将两个有共同祖先节点的不同分支进行合并。\n", 2278 | "\n", 2279 | "如果其中一个分支完全领先于另外一个分支,那么默认会采用fast forward合并方式。\n", 2280 | "\n", 2281 | "如果两个分支在分叉后各自都有新的commit,那么会是一种三方合并方式。\n", 2282 | "\n", 2283 | "第二种情况下可能需要解决冲突。" 2284 | ] 2285 | }, 2286 | { 2287 | "cell_type": "markdown", 2288 | "metadata": {}, 2289 | "source": [ 2290 | "fast forward合并方式:" 2291 | ] 2292 | }, 2293 | { 2294 | "cell_type": "markdown", 2295 | "metadata": {}, 2296 | "source": [ 2297 | "![](.\\git merge 快进式.png)" 2298 | ] 2299 | }, 2300 | { 2301 | "cell_type": "markdown", 2302 | "metadata": {}, 2303 | "source": [ 2304 | "三方合并方式:" 2305 | ] 2306 | }, 2307 | { 2308 | "cell_type": "markdown", 2309 | "metadata": {}, 2310 | "source": [ 2311 | "![](./git的merge原理.png)" 2312 | ] 2313 | }, 2314 | { 2315 | "cell_type": "code", 2316 | "execution_count": null, 2317 | "metadata": { 2318 | "collapsed": true 2319 | }, 2320 | "outputs": [], 2321 | "source": [ 2322 | "使用 no-ff模式可以保留被合并分支的原有信息。" 2323 | ] 2324 | }, 2325 | { 2326 | "cell_type": "markdown", 2327 | "metadata": {}, 2328 | "source": [ 2329 | "![](git merge --no-ff.png)" 2330 | ] 2331 | }, 2332 | { 2333 | "cell_type": "code", 2334 | "execution_count": 142, 2335 | "metadata": { 2336 | "collapsed": false 2337 | }, 2338 | "outputs": [ 2339 | { 2340 | "name": "stdout", 2341 | "output_type": "stream", 2342 | "text": [ 2343 | "Your branch is up to date with 'github/master'.\n" 2344 | ] 2345 | }, 2346 | { 2347 | "name": "stderr", 2348 | "output_type": "stream", 2349 | "text": [ 2350 | "Switched to branch 'master'\n" 2351 | ] 2352 | } 2353 | ], 2354 | "source": [ 2355 | "!git checkout master" 2356 | ] 2357 | }, 2358 | { 2359 | "cell_type": "code", 2360 | "execution_count": 145, 2361 | "metadata": { 2362 | "collapsed": false 2363 | }, 2364 | "outputs": [], 2365 | "source": [ 2366 | "!echo \"writen by master branch\" >>readme.txt" 2367 | ] 2368 | }, 2369 | { 2370 | "cell_type": "code", 2371 | "execution_count": 151, 2372 | "metadata": { 2373 | "collapsed": false 2374 | }, 2375 | "outputs": [], 2376 | "source": [ 2377 | "!git add -A" 2378 | ] 2379 | }, 2380 | { 2381 | "cell_type": "code", 2382 | "execution_count": 153, 2383 | "metadata": { 2384 | "collapsed": false 2385 | }, 2386 | "outputs": [ 2387 | { 2388 | "name": "stdout", 2389 | "output_type": "stream", 2390 | "text": [ 2391 | "[master b16a1bd] master branch\n", 2392 | " 1 file changed, 1 insertion(+)\n" 2393 | ] 2394 | } 2395 | ], 2396 | "source": [ 2397 | "!git commit -m\"master branch\"" 2398 | ] 2399 | }, 2400 | { 2401 | "cell_type": "code", 2402 | "execution_count": 154, 2403 | "metadata": { 2404 | "collapsed": true 2405 | }, 2406 | "outputs": [], 2407 | "source": [ 2408 | "!gitk" 2409 | ] 2410 | }, 2411 | { 2412 | "cell_type": "code", 2413 | "execution_count": 155, 2414 | "metadata": { 2415 | "collapsed": false 2416 | }, 2417 | "outputs": [ 2418 | { 2419 | "name": "stderr", 2420 | "output_type": "stream", 2421 | "text": [ 2422 | "Switched to branch 'develop'\n" 2423 | ] 2424 | } 2425 | ], 2426 | "source": [ 2427 | "!git checkout develop" 2428 | ] 2429 | }, 2430 | { 2431 | "cell_type": "code", 2432 | "execution_count": 156, 2433 | "metadata": { 2434 | "collapsed": true 2435 | }, 2436 | "outputs": [], 2437 | "source": [ 2438 | "!echo \"writen by develop branch\" >>readme.txt" 2439 | ] 2440 | }, 2441 | { 2442 | "cell_type": "code", 2443 | "execution_count": 157, 2444 | "metadata": { 2445 | "collapsed": false 2446 | }, 2447 | "outputs": [ 2448 | { 2449 | "name": "stdout", 2450 | "output_type": "stream", 2451 | "text": [ 2452 | "[develop 710fa6e] develop branch\n", 2453 | " 1 file changed, 1 insertion(+)\n" 2454 | ] 2455 | } 2456 | ], 2457 | "source": [ 2458 | "!git commit -am\"develop branch\"" 2459 | ] 2460 | }, 2461 | { 2462 | "cell_type": "code", 2463 | "execution_count": 160, 2464 | "metadata": { 2465 | "collapsed": false 2466 | }, 2467 | "outputs": [ 2468 | { 2469 | "name": "stdout", 2470 | "output_type": "stream", 2471 | "text": [ 2472 | "* 710fa6e develop branch\n", 2473 | "| * b16a1bd master branch\n", 2474 | "|/ \n", 2475 | "* 4e30082 update local\n", 2476 | "* 2b04eab Create test.txt\n", 2477 | "* b3f4690 C6\n", 2478 | "* c213efd delete new file\n", 2479 | "* 6ed6005 add a newfile\n", 2480 | "* 1c73eda C3\n", 2481 | "* 872640d C2\n", 2482 | "* f7c18e9 C1\n" 2483 | ] 2484 | } 2485 | ], 2486 | "source": [ 2487 | "!git log --oneline --graph --all" 2488 | ] 2489 | }, 2490 | { 2491 | "cell_type": "code", 2492 | "execution_count": 161, 2493 | "metadata": { 2494 | "collapsed": false 2495 | }, 2496 | "outputs": [ 2497 | { 2498 | "name": "stdout", 2499 | "output_type": "stream", 2500 | "text": [ 2501 | "Your branch is ahead of 'github/master' by 1 commit.\n", 2502 | " (use \"git push\" to publish your local commits)\n" 2503 | ] 2504 | }, 2505 | { 2506 | "name": "stderr", 2507 | "output_type": "stream", 2508 | "text": [ 2509 | "Switched to branch 'master'\n" 2510 | ] 2511 | } 2512 | ], 2513 | "source": [ 2514 | "!git checkout master" 2515 | ] 2516 | }, 2517 | { 2518 | "cell_type": "code", 2519 | "execution_count": 163, 2520 | "metadata": { 2521 | "collapsed": false 2522 | }, 2523 | "outputs": [ 2524 | { 2525 | "name": "stdout", 2526 | "output_type": "stream", 2527 | "text": [ 2528 | "Auto-merging readme.txt\n", 2529 | "CONFLICT (content): Merge conflict in readme.txt\n", 2530 | "Automatic merge failed; fix conflicts and then commit the result.\n" 2531 | ] 2532 | } 2533 | ], 2534 | "source": [ 2535 | "!git merge develop" 2536 | ] 2537 | }, 2538 | { 2539 | "cell_type": "code", 2540 | "execution_count": 164, 2541 | "metadata": { 2542 | "collapsed": true 2543 | }, 2544 | "outputs": [], 2545 | "source": [ 2546 | "\"hello world\" \n", 2547 | "\"hello China\" \n", 2548 | "\"hello BeiJing\" \n", 2549 | "\"hello HaiDian\" \n", 2550 | "\"Hello TianAnMen!\"\n", 2551 | "\"local line\" \n", 2552 | "<<<<<<< HEAD\n", 2553 | "\"writen by master branch\" \n", 2554 | "=======\n", 2555 | "\"writen by develop branch\" \n", 2556 | ">>>>>>> develop" 2557 | ] 2558 | }, 2559 | { 2560 | "cell_type": "code", 2561 | "execution_count": null, 2562 | "metadata": { 2563 | "collapsed": true 2564 | }, 2565 | "outputs": [], 2566 | "source": [ 2567 | "解决冲突后重新提交" 2568 | ] 2569 | }, 2570 | { 2571 | "cell_type": "code", 2572 | "execution_count": 165, 2573 | "metadata": { 2574 | "collapsed": true 2575 | }, 2576 | "outputs": [], 2577 | "source": [ 2578 | "!git add -A" 2579 | ] 2580 | }, 2581 | { 2582 | "cell_type": "code", 2583 | "execution_count": 166, 2584 | "metadata": { 2585 | "collapsed": false 2586 | }, 2587 | "outputs": [ 2588 | { 2589 | "name": "stdout", 2590 | "output_type": "stream", 2591 | "text": [ 2592 | "[master 86f7912] merged\n" 2593 | ] 2594 | } 2595 | ], 2596 | "source": [ 2597 | "!git commit -m\"merged\"" 2598 | ] 2599 | }, 2600 | { 2601 | "cell_type": "code", 2602 | "execution_count": 171, 2603 | "metadata": { 2604 | "collapsed": false 2605 | }, 2606 | "outputs": [], 2607 | "source": [ 2608 | "!gitk" 2609 | ] 2610 | }, 2611 | { 2612 | "cell_type": "markdown", 2613 | "metadata": {}, 2614 | "source": [ 2615 | "### 十八,分支重演" 2616 | ] 2617 | }, 2618 | { 2619 | "cell_type": "markdown", 2620 | "metadata": { 2621 | "collapsed": true 2622 | }, 2623 | "source": [ 2624 | "除了使用merge方法整合不同分支的内容外,分支重演rebase也是一种整合不同分支内容的方法。\n", 2625 | "但相对复杂一些。" 2626 | ] 2627 | }, 2628 | { 2629 | "cell_type": "markdown", 2630 | "metadata": {}, 2631 | "source": [ 2632 | "![](.\\git的rebase.png)" 2633 | ] 2634 | }, 2635 | { 2636 | "cell_type": "code", 2637 | "execution_count": 195, 2638 | "metadata": { 2639 | "collapsed": false 2640 | }, 2641 | "outputs": [ 2642 | { 2643 | "name": "stdout", 2644 | "output_type": "stream", 2645 | "text": [ 2646 | "* 710fa6e develop branch\n", 2647 | "| * b16a1bd master branch\n", 2648 | "|/ \n", 2649 | "* 4e30082 update local\n", 2650 | "* 2b04eab Create test.txt\n", 2651 | "* b3f4690 C6\n", 2652 | "* c213efd delete new file\n", 2653 | "* 6ed6005 add a newfile\n", 2654 | "* 1c73eda C3\n", 2655 | "* 872640d C2\n", 2656 | "* f7c18e9 C1\n" 2657 | ] 2658 | } 2659 | ], 2660 | "source": [ 2661 | "!git log --oneline --graph --all" 2662 | ] 2663 | }, 2664 | { 2665 | "cell_type": "code", 2666 | "execution_count": 209, 2667 | "metadata": { 2668 | "collapsed": false 2669 | }, 2670 | "outputs": [ 2671 | { 2672 | "name": "stdout", 2673 | "output_type": "stream", 2674 | "text": [ 2675 | "HEAD is now at b16a1bd master branch\n" 2676 | ] 2677 | } 2678 | ], 2679 | "source": [ 2680 | "!git reset --hard b16a1bd" 2681 | ] 2682 | }, 2683 | { 2684 | "cell_type": "code", 2685 | "execution_count": 210, 2686 | "metadata": { 2687 | "collapsed": false 2688 | }, 2689 | "outputs": [ 2690 | { 2691 | "name": "stdout", 2692 | "output_type": "stream", 2693 | "text": [ 2694 | "First, rewinding head to replay your work on top of it...\n", 2695 | "Applying: master branch\n", 2696 | "Using index info to reconstruct a base tree...\n", 2697 | "M\treadme.txt\n", 2698 | "Falling back to patching base and 3-way merge...\n", 2699 | "Auto-merging readme.txt\n", 2700 | "CONFLICT (content): Merge conflict in readme.txt\n", 2701 | "Patch failed at 0001 master branch\n", 2702 | "The copy of the patch that failed is found in: .git/rebase-apply/patch\n", 2703 | "\n", 2704 | "Resolve all conflicts manually, mark them as resolved with\n", 2705 | "\"git add/rm \", then run \"git rebase --continue\".\n", 2706 | "You can instead skip this commit: run \"git rebase --skip\".\n", 2707 | "To abort and get back to the state before \"git rebase\", run \"git rebase --abort\".\n", 2708 | "\n" 2709 | ] 2710 | }, 2711 | { 2712 | "name": "stderr", 2713 | "output_type": "stream", 2714 | "text": [ 2715 | ".git/rebase-apply/patch:9: trailing whitespace.\n", 2716 | "\"writen by master branch\" \n", 2717 | "warning: 1 line adds whitespace errors.\n", 2718 | "error: Failed to merge in the changes.\n" 2719 | ] 2720 | } 2721 | ], 2722 | "source": [ 2723 | "!git rebase develop" 2724 | ] 2725 | }, 2726 | { 2727 | "cell_type": "code", 2728 | "execution_count": 211, 2729 | "metadata": { 2730 | "collapsed": true 2731 | }, 2732 | "outputs": [], 2733 | "source": [ 2734 | "!git add readme.txt" 2735 | ] 2736 | }, 2737 | { 2738 | "cell_type": "code", 2739 | "execution_count": 212, 2740 | "metadata": { 2741 | "collapsed": false 2742 | }, 2743 | "outputs": [ 2744 | { 2745 | "name": "stdout", 2746 | "output_type": "stream", 2747 | "text": [ 2748 | "Applying: master branch\n" 2749 | ] 2750 | } 2751 | ], 2752 | "source": [ 2753 | "!git rebase --continue" 2754 | ] 2755 | }, 2756 | { 2757 | "cell_type": "code", 2758 | "execution_count": 213, 2759 | "metadata": { 2760 | "collapsed": false 2761 | }, 2762 | "outputs": [ 2763 | { 2764 | "name": "stdout", 2765 | "output_type": "stream", 2766 | "text": [ 2767 | "On branch master\n", 2768 | "Your branch is ahead of 'github/master' by 2 commits.\n", 2769 | " (use \"git push\" to publish your local commits)\n", 2770 | "\n", 2771 | "nothing to commit, working tree clean\n" 2772 | ] 2773 | } 2774 | ], 2775 | "source": [ 2776 | "!git commit -m\"rebase over\"" 2777 | ] 2778 | }, 2779 | { 2780 | "cell_type": "code", 2781 | "execution_count": 215, 2782 | "metadata": { 2783 | "collapsed": false 2784 | }, 2785 | "outputs": [ 2786 | { 2787 | "name": "stdout", 2788 | "output_type": "stream", 2789 | "text": [ 2790 | "Your branch is ahead of 'github/master' by 2 commits.\n", 2791 | " (use \"git push\" to publish your local commits)\n" 2792 | ] 2793 | }, 2794 | { 2795 | "name": "stderr", 2796 | "output_type": "stream", 2797 | "text": [ 2798 | "Already on 'master'\n" 2799 | ] 2800 | } 2801 | ], 2802 | "source": [ 2803 | "!git checkout master" 2804 | ] 2805 | }, 2806 | { 2807 | "cell_type": "code", 2808 | "execution_count": 217, 2809 | "metadata": { 2810 | "collapsed": false 2811 | }, 2812 | "outputs": [ 2813 | { 2814 | "name": "stdout", 2815 | "output_type": "stream", 2816 | "text": [ 2817 | "* c28de3a master branch\n", 2818 | "* 710fa6e develop branch\n", 2819 | "* 4e30082 update local\n", 2820 | "* 2b04eab Create test.txt\n", 2821 | "* b3f4690 C6\n", 2822 | "* c213efd delete new file\n", 2823 | "* 6ed6005 add a newfile\n", 2824 | "* 1c73eda C3\n", 2825 | "* 872640d C2\n", 2826 | "* f7c18e9 C1\n" 2827 | ] 2828 | } 2829 | ], 2830 | "source": [ 2831 | "!git log --oneline --graph --all" 2832 | ] 2833 | }, 2834 | { 2835 | "cell_type": "code", 2836 | "execution_count": 218, 2837 | "metadata": { 2838 | "collapsed": true 2839 | }, 2840 | "outputs": [], 2841 | "source": [ 2842 | "!gitk" 2843 | ] 2844 | }, 2845 | { 2846 | "cell_type": "code", 2847 | "execution_count": null, 2848 | "metadata": { 2849 | "collapsed": true 2850 | }, 2851 | "outputs": [], 2852 | "source": [ 2853 | "\n", 2854 | "\n", 2855 | "\n", 2856 | "\n", 2857 | "\n", 2858 | "\n", 2859 | "\n", 2860 | "\n", 2861 | "\n", 2862 | "\n", 2863 | "\n", 2864 | "\n", 2865 | "\n", 2866 | "\n", 2867 | "\n", 2868 | "\n", 2869 | "\n" 2870 | ] 2871 | }, 2872 | { 2873 | "cell_type": "markdown", 2874 | "metadata": {}, 2875 | "source": [ 2876 | "### 十九,储藏管理" 2877 | ] 2878 | }, 2879 | { 2880 | "cell_type": "markdown", 2881 | "metadata": { 2882 | "collapsed": true 2883 | }, 2884 | "source": [ 2885 | "如果工作只进行到一半,还没法提交。\n", 2886 | "\n", 2887 | "但出现了一个着急的事情需要马上切换去解决,例如修复一个临时bug。\n", 2888 | "\n", 2889 | "有没有什么办法呢?\n", 2890 | "\n", 2891 | "Git提供的stash功能非常适用于这种需要现场保存的情境。\n", 2892 | "\n", 2893 | "把当前工作现场“储藏”起来,等以后恢复现场后继续工作。" 2894 | ] 2895 | }, 2896 | { 2897 | "cell_type": "code", 2898 | "execution_count": 237, 2899 | "metadata": { 2900 | "collapsed": false 2901 | }, 2902 | "outputs": [], 2903 | "source": [ 2904 | "!echo 'not done!' >>readme.txt" 2905 | ] 2906 | }, 2907 | { 2908 | "cell_type": "code", 2909 | "execution_count": null, 2910 | "metadata": { 2911 | "collapsed": true 2912 | }, 2913 | "outputs": [], 2914 | "source": [ 2915 | "git stash;\n", 2916 | "git stash pop;\n", 2917 | "git stash list;\n", 2918 | "git stash save \"message\"\n", 2919 | "git stash apply @{X}" 2920 | ] 2921 | }, 2922 | { 2923 | "cell_type": "code", 2924 | "execution_count": 233, 2925 | "metadata": { 2926 | "collapsed": true 2927 | }, 2928 | "outputs": [], 2929 | "source": [ 2930 | "!git stash list" 2931 | ] 2932 | }, 2933 | { 2934 | "cell_type": "code", 2935 | "execution_count": 238, 2936 | "metadata": { 2937 | "collapsed": false 2938 | }, 2939 | "outputs": [ 2940 | { 2941 | "name": "stdout", 2942 | "output_type": "stream", 2943 | "text": [ 2944 | "Saved working directory and index state WIP on master: c28de3a master branch\n" 2945 | ] 2946 | } 2947 | ], 2948 | "source": [ 2949 | "!git stash" 2950 | ] 2951 | }, 2952 | { 2953 | "cell_type": "code", 2954 | "execution_count": 239, 2955 | "metadata": { 2956 | "collapsed": false 2957 | }, 2958 | "outputs": [ 2959 | { 2960 | "name": "stdout", 2961 | "output_type": "stream", 2962 | "text": [ 2963 | "stash@{0}: WIP on master: c28de3a master branch\n" 2964 | ] 2965 | } 2966 | ], 2967 | "source": [ 2968 | "!git stash list" 2969 | ] 2970 | }, 2971 | { 2972 | "cell_type": "code", 2973 | "execution_count": 240, 2974 | "metadata": { 2975 | "collapsed": false 2976 | }, 2977 | "outputs": [ 2978 | { 2979 | "name": "stdout", 2980 | "output_type": "stream", 2981 | "text": [ 2982 | "On branch master\n", 2983 | "Your branch is up to date with 'github/master'.\n", 2984 | "\n", 2985 | "Changes not staged for commit:\n", 2986 | " (use \"git add ...\" to update what will be committed)\n", 2987 | " (use \"git checkout -- ...\" to discard changes in working directory)\n", 2988 | "\n", 2989 | "\tmodified: readme.txt\n", 2990 | "\n", 2991 | "no changes added to commit (use \"git add\" and/or \"git commit -a\")\n", 2992 | "Dropped refs/stash@{0} (0d10ce6c25480aa842ebb4ecb088b562d29d1905)\n" 2993 | ] 2994 | } 2995 | ], 2996 | "source": [ 2997 | "!git stash pop" 2998 | ] 2999 | }, 3000 | { 3001 | "cell_type": "code", 3002 | "execution_count": 241, 3003 | "metadata": { 3004 | "collapsed": true 3005 | }, 3006 | "outputs": [], 3007 | "source": [ 3008 | "!echo \"done 50%...\" >>readme.txt" 3009 | ] 3010 | }, 3011 | { 3012 | "cell_type": "code", 3013 | "execution_count": 242, 3014 | "metadata": { 3015 | "collapsed": false 3016 | }, 3017 | "outputs": [ 3018 | { 3019 | "name": "stdout", 3020 | "output_type": "stream", 3021 | "text": [ 3022 | "Saved working directory and index state WIP on master: c28de3a master branch\n" 3023 | ] 3024 | } 3025 | ], 3026 | "source": [ 3027 | "!git stash save \"50%done\"" 3028 | ] 3029 | }, 3030 | { 3031 | "cell_type": "code", 3032 | "execution_count": 243, 3033 | "metadata": { 3034 | "collapsed": false 3035 | }, 3036 | "outputs": [ 3037 | { 3038 | "name": "stdout", 3039 | "output_type": "stream", 3040 | "text": [ 3041 | "stash@{0}: WIP on master: c28de3a master branch\n" 3042 | ] 3043 | } 3044 | ], 3045 | "source": [ 3046 | "!git stash list" 3047 | ] 3048 | }, 3049 | { 3050 | "cell_type": "code", 3051 | "execution_count": 244, 3052 | "metadata": { 3053 | "collapsed": true 3054 | }, 3055 | "outputs": [], 3056 | "source": [ 3057 | "!echo \"done 75%...\" >>readme.txt" 3058 | ] 3059 | }, 3060 | { 3061 | "cell_type": "code", 3062 | "execution_count": 245, 3063 | "metadata": { 3064 | "collapsed": false 3065 | }, 3066 | "outputs": [ 3067 | { 3068 | "name": "stdout", 3069 | "output_type": "stream", 3070 | "text": [ 3071 | "Saved working directory and index state WIP on master: c28de3a master branch\n" 3072 | ] 3073 | } 3074 | ], 3075 | "source": [ 3076 | "!git stash" 3077 | ] 3078 | }, 3079 | { 3080 | "cell_type": "code", 3081 | "execution_count": 246, 3082 | "metadata": { 3083 | "collapsed": false 3084 | }, 3085 | "outputs": [ 3086 | { 3087 | "name": "stdout", 3088 | "output_type": "stream", 3089 | "text": [ 3090 | "stash@{0}: WIP on master: c28de3a master branch\n", 3091 | "stash@{1}: WIP on master: c28de3a master branch\n" 3092 | ] 3093 | } 3094 | ], 3095 | "source": [ 3096 | "!git stash list" 3097 | ] 3098 | }, 3099 | { 3100 | "cell_type": "code", 3101 | "execution_count": 253, 3102 | "metadata": { 3103 | "collapsed": false 3104 | }, 3105 | "outputs": [ 3106 | { 3107 | "name": "stdout", 3108 | "output_type": "stream", 3109 | "text": [ 3110 | "On branch master\n", 3111 | "Your branch is up to date with 'github/master'.\n", 3112 | "\n", 3113 | "Changes not staged for commit:\n", 3114 | " (use \"git add ...\" to update what will be committed)\n", 3115 | " (use \"git checkout -- ...\" to discard changes in working directory)\n", 3116 | "\n", 3117 | "\tmodified: readme.txt\n", 3118 | "\n", 3119 | "no changes added to commit (use \"git add\" and/or \"git commit -a\")\n" 3120 | ] 3121 | } 3122 | ], 3123 | "source": [ 3124 | "!git stash apply 0" 3125 | ] 3126 | }, 3127 | { 3128 | "cell_type": "code", 3129 | "execution_count": 254, 3130 | "metadata": { 3131 | "collapsed": false 3132 | }, 3133 | "outputs": [ 3134 | { 3135 | "name": "stdout", 3136 | "output_type": "stream", 3137 | "text": [ 3138 | "stash@{0}: WIP on master: c28de3a master branch\n", 3139 | "stash@{1}: WIP on master: c28de3a master branch\n" 3140 | ] 3141 | } 3142 | ], 3143 | "source": [ 3144 | "!git stash list" 3145 | ] 3146 | }, 3147 | { 3148 | "cell_type": "code", 3149 | "execution_count": 255, 3150 | "metadata": { 3151 | "collapsed": false 3152 | }, 3153 | "outputs": [ 3154 | { 3155 | "name": "stdout", 3156 | "output_type": "stream", 3157 | "text": [ 3158 | "Dropped refs/stash@{0} (f0d23e964be3d2a9f6234e7e23f17bc747ac9c92)\n" 3159 | ] 3160 | } 3161 | ], 3162 | "source": [ 3163 | "!git stash drop 0" 3164 | ] 3165 | }, 3166 | { 3167 | "cell_type": "code", 3168 | "execution_count": 3, 3169 | "metadata": { 3170 | "collapsed": false 3171 | }, 3172 | "outputs": [ 3173 | { 3174 | "name": "stdout", 3175 | "output_type": "stream", 3176 | "text": [ 3177 | "F:\\shidayunpan\\allCCTVResearch\\PythonFiles\\GitHub\\git-learn\n" 3178 | ] 3179 | } 3180 | ], 3181 | "source": [ 3182 | "%cd ./git-learn" 3183 | ] 3184 | }, 3185 | { 3186 | "cell_type": "code", 3187 | "execution_count": 5, 3188 | "metadata": { 3189 | "collapsed": false 3190 | }, 3191 | "outputs": [], 3192 | "source": [ 3193 | "!gitk --all" 3194 | ] 3195 | }, 3196 | { 3197 | "cell_type": "markdown", 3198 | "metadata": {}, 3199 | "source": [ 3200 | "### 二十,标签管理" 3201 | ] 3202 | }, 3203 | { 3204 | "cell_type": "markdown", 3205 | "metadata": { 3206 | "collapsed": true 3207 | }, 3208 | "source": [ 3209 | "用标签记录版本号会更加方便协作管理。\n", 3210 | "\n", 3211 | "如果团队间按如下方式沟通将很麻烦:\n", 3212 | "\n", 3213 | "“请把上周一的那个版本打包发布,commit号是6a5819e...”\n", 3214 | "\n", 3215 | "而有了版本号将会方便许多:\n", 3216 | "\n", 3217 | "“请把上周一的那个版本打包发布,版本号是v1.2”\n", 3218 | "\n", 3219 | "标签和分支很像,都是指向commit id的指针,但标签不能移动而分支能够移动。" 3220 | ] 3221 | }, 3222 | { 3223 | "cell_type": "code", 3224 | "execution_count": 221, 3225 | "metadata": { 3226 | "collapsed": true 3227 | }, 3228 | "outputs": [], 3229 | "source": [ 3230 | "# 当前head位置打上v2.0标签\n", 3231 | "!git tag v2.0" 3232 | ] 3233 | }, 3234 | { 3235 | "cell_type": "code", 3236 | "execution_count": 222, 3237 | "metadata": { 3238 | "collapsed": true 3239 | }, 3240 | "outputs": [], 3241 | "source": [ 3242 | "# 在commit id 为 4e30082位置打上 v1.0标签\n", 3243 | "!git tag v1.0 4e30082" 3244 | ] 3245 | }, 3246 | { 3247 | "cell_type": "code", 3248 | "execution_count": 224, 3249 | "metadata": { 3250 | "collapsed": false, 3251 | "scrolled": true 3252 | }, 3253 | "outputs": [ 3254 | { 3255 | "name": "stdout", 3256 | "output_type": "stream", 3257 | "text": [ 3258 | "v1.0\n", 3259 | "v2.0\n" 3260 | ] 3261 | } 3262 | ], 3263 | "source": [ 3264 | "# 查看全部标签\n", 3265 | "!git tag" 3266 | ] 3267 | }, 3268 | { 3269 | "cell_type": "code", 3270 | "execution_count": 225, 3271 | "metadata": { 3272 | "collapsed": false, 3273 | "scrolled": false 3274 | }, 3275 | "outputs": [ 3276 | { 3277 | "name": "stdout", 3278 | "output_type": "stream", 3279 | "text": [ 3280 | "commit 4e30082dfda0cd965ed06dc2e8e5ee4e99fcdb8d\n", 3281 | "Author: lyhue1991 \n", 3282 | "Date: Sat Mar 10 21:06:41 2018 +0800\n", 3283 | "\n", 3284 | " update local\n", 3285 | "\n", 3286 | "diff --git a/readme.txt b/readme.txt\n", 3287 | "index 998866a..e85bfb1 100644\n", 3288 | "--- a/readme.txt\n", 3289 | "+++ b/readme.txt\n", 3290 | "@@ -3,3 +3,4 @@\n", 3291 | " \"hello BeiJing\" \n", 3292 | " \"hello HaiDian\" \n", 3293 | " \"Hello TianAnMen!\"\n", 3294 | "+\"local line\" \n" 3295 | ] 3296 | } 3297 | ], 3298 | "source": [ 3299 | "# 查看标签对应的版本号等信息\n", 3300 | "!git show v1.0" 3301 | ] 3302 | }, 3303 | { 3304 | "cell_type": "code", 3305 | "execution_count": 228, 3306 | "metadata": { 3307 | "collapsed": false, 3308 | "scrolled": true 3309 | }, 3310 | "outputs": [ 3311 | { 3312 | "name": "stderr", 3313 | "output_type": "stream", 3314 | "text": [ 3315 | "To github.com:lyhue1991/git-learn\n", 3316 | " * [new tag] v1.0 -> v1.0\n" 3317 | ] 3318 | } 3319 | ], 3320 | "source": [ 3321 | "# 推送标签到远程\n", 3322 | "!git push github v1.0" 3323 | ] 3324 | }, 3325 | { 3326 | "cell_type": "code", 3327 | "execution_count": 229, 3328 | "metadata": { 3329 | "collapsed": false 3330 | }, 3331 | "outputs": [ 3332 | { 3333 | "name": "stdout", 3334 | "output_type": "stream", 3335 | "text": [ 3336 | "Deleted tag 'v1.0' (was 4e30082)\n" 3337 | ] 3338 | } 3339 | ], 3340 | "source": [ 3341 | "# 删除标签\n", 3342 | "!git tag -d v1.0" 3343 | ] 3344 | }, 3345 | { 3346 | "cell_type": "code", 3347 | "execution_count": 232, 3348 | "metadata": { 3349 | "collapsed": false, 3350 | "scrolled": true 3351 | }, 3352 | "outputs": [ 3353 | { 3354 | "name": "stderr", 3355 | "output_type": "stream", 3356 | "text": [ 3357 | "To github.com:lyhue1991/git-learn\n", 3358 | " - [deleted] v1.0\n" 3359 | ] 3360 | } 3361 | ], 3362 | "source": [ 3363 | "# 删除远程标签\n", 3364 | "!git push github :refs/tags/v1.0" 3365 | ] 3366 | }, 3367 | { 3368 | "cell_type": "code", 3369 | "execution_count": 220, 3370 | "metadata": { 3371 | "collapsed": false 3372 | }, 3373 | "outputs": [ 3374 | { 3375 | "name": "stdout", 3376 | "output_type": "stream", 3377 | "text": [ 3378 | "c28de3a master branch\n", 3379 | "710fa6e develop branch\n", 3380 | "4e30082 update local\n", 3381 | "2b04eab Create test.txt\n", 3382 | "b3f4690 C6\n", 3383 | "c213efd delete new file\n", 3384 | "6ed6005 add a newfile\n", 3385 | "1c73eda C3\n", 3386 | "872640d C2\n", 3387 | "f7c18e9 C1\n" 3388 | ] 3389 | } 3390 | ], 3391 | "source": [ 3392 | "!git log --oneline" 3393 | ] 3394 | } 3395 | ], 3396 | "metadata": { 3397 | "anaconda-cloud": {}, 3398 | "kernelspec": { 3399 | "display_name": "Python [default]", 3400 | "language": "python", 3401 | "name": "python3" 3402 | }, 3403 | "language_info": { 3404 | "codemirror_mode": { 3405 | "name": "ipython", 3406 | "version": 3 3407 | }, 3408 | "file_extension": ".py", 3409 | "mimetype": "text/x-python", 3410 | "name": "python", 3411 | "nbconvert_exporter": "python", 3412 | "pygments_lexer": "ipython3", 3413 | "version": "3.5.2" 3414 | } 3415 | }, 3416 | "nbformat": 4, 3417 | "nbformat_minor": 2 3418 | } 3419 | --------------------------------------------------------------------------------