gitk

第二部分 常见操作

获取, 查看和修改git版本库

Created by Charles Liu

Agenda

  • 获取版本库
  • 审查版本库
  • 创建版本库
  • 提交一个修改
  • 多分支
  • 复用commit
  • 冲突解决

获取一个repository

git clone

试着获取emoji-minesweeper框架代码
https://github.com/dodoinblue/emoji-minesweeper


# 需要联网
$ git clone https://github.com/dodoinblue/emoji-minesweeper.git
  

进入工作目录


$ cd emoji-minesweeper
$ ls
README.md game.js  index.html
    

查看repo的历史记录

git log

$ git log
commit d2ddef48a916da9dd78526d004bcad5e9fa44ad5
Author: Mu-An Chiou <muan@github.com>
Date:   Mon Feb 8 01:48:05 2016 +0800

:grimacing:

commit e4c78a2cd35d9fb92e69f0f80b426f64cd327504
Author: Mu-An Chiou <muan@github.com>
Date:   Sun Feb 7 16:46:24 2016 +0800

☔

commit a3dcc600deee31309acaf2676ab6bfe5d7322e6d
Author: Mu-An Chiou <muan@github.com>
Date:   Sun Feb 7 16:42:11 2016 +0800

Add a set

commit 1ba3c811150d23380140fe497140681e196c4ea9
Author: Mu-An Chiou <muan@github.com>
Date:   Sun Feb 7 16:10:02 2016 +0800

Up native emoji font-size

commit 2b8f14aa2d74c1d287634da52808acf08b1fece6
Merge: 2707be9 03f873b
Author: Mu-An Chiou <muan@github.com>
Date:   Tue Dec 1 21:50:57 2015 +0900

Merge pull request #16 from francisfuzz/patch-1

:
    

log内容:

Commit Id - 作者 - 时间 - 描述

定制log的输出内容和格式


$ git log --oneline # 每个commit用一行显示
d2ddef4 :grimacing:
e4c78a2 ☔
a3dcc60 Add a set
1ba3c81 Up native emoji font-size
2b8f14a Merge pull request #16 from francisfuzz/patch-1
03f873b (docs) correct typo
2707be9 Add link to GitHub
b76a7d6 @jsstrn did this! :tada:
d983f5f Merge pull request #15 from jsstrn/gh-pages
14c88f7 Add selection for different emoji sets
b272295 Merge pull request #12 from eush77/restart-key
6fba66c Merge pull request #11 from eush77/firefox-textcontent-fix
4a06015 Add shortcut for restarting the game
019a03f Replace innerText with textContent
9e0b44f Merge pull request #10 from nkantar/gh-pages
738b6ed Oops, moar double quotes
6182c68 Oops, double quotes
1eee74a Adds max='500' to input fields
bbeb0ee Adds min='1' to input fields
ebdf69d Rearrange readme sections
8efa43f :straight_ruler: Align
cff9c55 :infomation_desk_person: in case you've not played minesweeper before
d9537b4 :beetle: fix sometimes getting one extra bomb
caa970b :x: remove borders
327c0b2 :heart_eyes: update gif
fd2ed50 :flower: style divider
3a729b5 :nail_care: hide all settings in a popup so much cleaner but much css
c2e5791 Merge pull request #7 from javierbyte/gh-pages
61a79d4 Stop stats' shake.
c7cf40f :iphone: see
b780bb2 :white_check_mark: these are done thanks to @notwaldorf wow
    

$ git log -n 1 # 显示最近的一个commit信息
commit d2ddef48a916da9dd78526d004bcad5e9fa44ad5
Author: Mu-An Chiou <muan@github.com>
Date:   Mon Feb 8 01:48:05 2016 +0800

:grimacing:
    

也可以结合多个命令一起使用


$ git log --oneline -n 5 # 显示最近的5个commit的简要信息
d2ddef4 :grimacing:
e4c78a2 ☔
a3dcc60 Add a set
1ba3c81 Up native emoji font-size
2b8f14a Merge pull request #16 from francisfuzz/patch-1
        

$ git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
* d2ddef4 - (HEAD -> gh-pages, origin/gh-pages, origin/HEAD) :grimacing: (5 months ago) <Mu-An Chiou>
* e4c78a2 - ☔ (5 months ago) <Mu-An Chiou>
* a3dcc60 - Add a set (5 months ago) <Mu-An Chiou>
* 1ba3c81 - Up native emoji font-size (5 months ago) <Mu-An Chiou>
*   2b8f14a - Merge pull request #16 from francisfuzz/patch-1 (7 months ago) <Mu-An Chiou>
|\
| * 03f873b - (docs) correct typo (7 months ago) <Francis>
|/
* 2707be9 - Add link to GitHub (8 months ago) <Mu-An Chiou>
* b76a7d6 - @jsstrn did this! :tada: (8 months ago) <Mu-An Chiou>
*   d983f5f - Merge pull request #15 from jsstrn/gh-pages (8 months ago) <Mu-An Chiou>
|\
| * 14c88f7 - Add selection for different emoji sets (8 months ago) <jsstrn>
|/
*   b272295 - Merge pull request #12 from eush77/restart-key (8 months ago) <Mu-An Chiou>
        

用图形化工具gitk查看


$ gitk
    

git log练习

查找题为"Rearrange readme sections"的commit

git命令别名

可以通过配置别名来简化命令输入


$ git config --global alias.st status
$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  

查看某一commit的具体内容

git show commit-id

$ git show a3dcc60
commit a3dcc600deee31309acaf2676ab6bfe5d7322e6d
Author: Mu-An Chiou <muan@github.com>
Date:   Sun Feb 7 16:42:11 2016 +0800

Add a set

diff --git a/index.html b/index.html
index 1e1a141..345fef0 100644
--- a/index.html
+++ b/index.html
@@ -284,7 +284,8 @@
<option value="<U+1F423> <U+1F4A3> <U+1F6A7> ◻️"><U+1F423> <U+1F4A3> <U+1F6A7></option>
<option value="<U+1F370> <U+1F47E> <U+1F4CC> ◻️"><U+1F370> <U+1F47E> <U+1F4CC></option>
<option value="<U+1F337> ⚡️ <U+1F41E> ◻️"><U+1F337> ⚡️ <U+1F41E></option>
-         <option value="<U+2B55>️ <U+274C> <U+2757>️ ◻️"><U+2B55>️ <U+274C> <U+2757>️</option>
+         <option value="<U+2B55>️ <U+274C> <U+2757>️ ✖️️"><U+2B55>️ <U+274C> <U+2757>️ ✖️</option>
+         <option value="☀ ⚡ ☂ ☁️"> ☀ ⚡ ☂ ☁️</option>
</select>
<button class="js-popup-new-game btn">Restart Game</button>
</div>
      

git show练习

Commit "(docs) correct typo" 修改了哪个typo? 对哪个文件做的修改?

比较版本变化

git diff commit1 commit2

比较"Up native emoji font-size" 和 "Add a set"这两个版本之间的区别


$ git diff 1ba3c..a3dcc60
diff --git a/index.html b/index.html
index 1e1a141..345fef0 100644
--- a/index.html
+++ b/index.html
@@ -284,7 +284,8 @@
<option value="<U+1F423> <U+1F4A3> <U+1F6A7> ◻️"><U+1F423> <U+1F4A3> <U+1F6A7></option>
<option value="<U+1F370> <U+1F47E> <U+1F4CC> ◻️"><U+1F370> <U+1F47E> <U+1F4CC></option>
<option value="<U+1F337> ⚡️ <U+1F41E> ◻️"><U+1F337> ⚡️ <U+1F41E></option>
-         <option value="<U+2B55>️ <U+274C> <U+2757>️ ◻️"><U+2B55>️ <U+274C> <U+2757>️</option>
+         <option value="<U+2B55>️ <U+274C> <U+2757>️ ✖️️"><U+2B55>️ <U+274C> <U+2757>️ ✖️</option>
+         <option value="☀ ⚡ ☂ ☁️"> ☀ ⚡ ☂ ☁️</option>
</select>
<button class="js-popup-new-game btn">Restart Game</button>
</div>
        

跨越多个commit的比较变化

显示b76a到03f87的变化


$ git diff b76a..03f87
diff --git a/README.md b/README.md
index 409d5a5..49761fc 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@

- Left click to step on a spot
- Right click to mark a spot as a bomb
-- Double clcik to open all 8 spots nearby a target (except ones already marked as bombs using right clicks)
+- Double click to open all 8 spots nearby a target (except ones already marked as bombs using right clicks)

**This is a work in progress**

diff --git a/index.html b/index.html
index ef87916..19b9f62 100644
--- a/index.html
+++ b/index.html
@@ -189,7 +189,6 @@
padding: 8px 10px 5px;
display: table-cell;
text-align: center;
-        font-family: "Exo 2", sans-serif;
}

.stat:after {
@@ -250,6 +249,19 @@
color: #999;
font-size: 0.65em;
}
+
+      .stat,
+      .link-to-github {
+        font-family: "Exo 2", sans-serif;
+      }
+
+      .link-to-github {
+        margin: 40px auto;
+        display: block;
+        font-size: 12px;
+        color: #08c;
+        text-decoration: none;
+      }
</style>
</head>
<body>
@@ -287,7 +299,7 @@
<div title="TIME" id="timer" class="stat flex-cell">0</div>
</div>
</div>
-    <br>
+    <a href="https://github.com/muan/emoji-minesweeper" class="link-to-github">[Code]</a>

<script>
var emojiset = document.getElementById('emojiset').value.split(' ')
    

git diff练习

从commit "Up native emoji font-size" 到最新的版本之间, 一共修改了几个文件, 几行增加, 几行删减?

查看当前状态

git status

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
  

切换到特定版本

git checkout commitId

看看在"Add a set"这个提交之前, emoji-sweeper游戏的运行效果是什么样的?


$ git checkout a3dcc60
# Now run index.html and find the difference.
  

切换回master


$ git checkout master
    

创建版本库

git init

创建一个目录, 查看版本历史和目录结构


repository $ mkdir todo-list
repository $ cd todo-list
todo-list  $ git log
    

fatal: Not a git repository (or any of the parent directories): .git
    

$ ls
$ ls -a #查看隐藏文件
    

. ..
    

创建新版本库


$ git init
Initialized empty Git repository in /Users/Charles/repository/todo-list/.git/
    

$ ls -a
. .. .git
    

新版本库的状态

git status


$ git status

On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
    

暂存文件改动

git add filename

首先要对文件做一些修改


$ echo "# Todo List" >> Readme.md
$ git status

On branch master

Initial commit

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

Readme.md

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

跟踪新文件--执行git add


$ git add Readme.md
$ git status
    

On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file:   Readme.md
    

缓存已跟踪文件

对已跟踪文件再次做出修改


$ echo "another change" >> Readme.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

modified:   Readme.md

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:   Readme.md
    

在缓存前, 确认改动内容

git diff

$ git diff
diff --git a/Readme.md b/Readme.md
index 9da002c..1aad9fa 100644
--- a/Readme.md
+++ b/Readme.md
@@ -4,3 +4,4 @@ buy soy-milk
 buy kiwifruit
 buy apple
 something
+another change
    

再次执行git add


$ git add Readme.md
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified:   Readme.md
    

未跟踪, 未修改, 已修改, 已缓存之间的转换

sequenceDiagram participant Untracked as 未跟踪 participant Unmodified as 未修改 participant Modified as 已修改 participant Staged as 已缓存 Untracked ->> Staged: git add Unmodified ->> Modified: 编辑/修改代码 Modified ->> Staged: git add Unmodified ->> Untracked: git rm Staged ->> Unmodified: git commit

git add 思考

git log会看到commit记录么?

提交改动

git commit

提交前审查文件变化


$ git diff --cached
diff --git a/Readme.md b/Readme.md
index c76d50f..9da002c 100644
--- a/Readme.md
+++ b/Readme.md
@@ -3,3 +3,4 @@
 buy soy-milk
 buy kiwifruit
 buy apple
+something
    

执行git commit


$ git commit
# 界面跳转到vim或指定编辑器编辑"提交说明". VIM界面如下:
    

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#	new file:   Readme.md
#
~
~
~
".git/COMMIT_EDITMSG" 9L, 283C
    

可以通过*-m*参数直接写入提交说明


$ git commit -m "a change to demo git usage"
    

通过git-gui缓存和提交

gitk

commit message的书写要求


修改的摘要(50 个字符或更少)

如果必要的话,加入更详细的解释文字。在
大概 72 个字符的时候换行。在某些情形下,
第一行被当作一封电子邮件的标题,剩下的
文本作为正文。分隔摘要与正文的空行是
必须的(除非你完全省略正文);如果你将
两者混在一起,那么类似变基等工具无法
正常工作。

空行接着更进一步的段落。

  - 句号也是可以的。

  - 项目符号可以使用典型的连字符或星号
    前面一个空格,之间用空行隔开,
    但是可以依据不同的惯例有所不同。

如果你使用缺陷跟踪系统, 在正文最下方引用它们的id

Resolves: #123
See also: #456, #789
    
  • 标题简要描述所修改的问题
  • 一定要空行. 空行是git区别标题和正文的方式
  • 正文每行不超过72字符.
  • 正文可以详细描述一下问题的现象, 原因及解决思路. 有要求的话可以引用BugId, TaskId或者FeatureId, 方便项目跟踪.

git commit思考题

git log会看到什么?

git status会看到什么?

工作区, 管存区和版本库

sequenceDiagram participant Working as 工作区 participant Staging as 缓存区 participant Repo as 版本库 Repo ->> Working: git checkout Working ->> Staging: git add Staging ->> Repo: git commit

  • 对文件系统的改动, 首先会被记录在工作区, 作为Unstaged Chanages或者Untracked Files
  • 一个Commit, 对于git来说是一个存储记录.
  • 缓存区是在工作区和Commit记录之间的缓冲. 可以有选择的将一部分文件系统改动添加进缓存区, 让Commit更灵活
  • Commit时可以写描述, 方便区分和回忆改动的内容和目的

修改最近一次提交

git commit --amend

$ echo "some amends" >> Readme.md
$ git add Readme.md
$ git commit --amend
# 编辑Commit消息然后保存退出
  

$ git log --oneline
  

忽略特定文件

.gitignore文件

在工作区, 可以建立也个特殊的文件叫.gitignore, 这个文件里面保存一个不需要git跟踪的文件列表. 这样的文件包括, Visual Studio生成的项目配置文件, 比如c编译出来的obj文件, Windows图片文件夹的缩略图缓存文件Thumbs.db等.

  • 所有空行或者以 # 开头的行都会被 Git 忽略。
  • 可以使用标准的 glob 模式匹配。
  • 匹配模式可以以(/)开头防止递归。
  • 匹配模式可以以(/)结尾指定目录。
  • 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

gitignore文件样例


# Object files
*.o
*.ko
*.obj
*.elf

# matlab
# Simulink Code Generation
slprj/

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
    

https://github.com/github/gitignore 项目维护了常见开发环境的gitignore模板