Git 的使用基础和示例

首先我们要检出一个 Git 项目:
git clone

> ~/git cd ~/git/                                                                                                                                                
⋊> ~/git mkdir demo                                                                                                                                               
⋊> ~/git cd demo/                                                                                                                                                 
⋊> ~/g/demo git clone ssh://git@xxxx.xxx.xxx/Demo/GitAndSourceTree.git                                                                              
Cloning into 'GitAndSourceTree'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
⋊> ~/g/demo ls                                                                                                                                                    
GitAndSourceTree

添加文件夹到项目中:

> ~/g/d/GitAndSourceTree on master ⨯ mkdir Upload                                                                                                                
⋊> ~/g/d/GitAndSourceTree on master ⨯ mkdir TheCoreFramework

创建 Git 忽略文件:

> ~/g/d/GitAndSourceTree on master ⨯ touch .gitignore                                                                                                            
⋊> ~/g/d/GitAndSourceTree on master ⨯ vim .gitignore                                                                                                              
⋊> ~/g/d/GitAndSourceTree on master ⨯ cat .gitignore                                                                                                              
.idea

空文件夹是会被Git 自动忽略的所以为了给演示增加一个(.keep)文件

> ~/g/d/GitAndSourceTree on master ⨯ touch ./Upload/.keep                                                                                                        
⋊> ~/g/d/GitAndSourceTree on master ⨯ git status                                                                                                                  
On branch master

Initial commit

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

    .gitignore
    Upload/

nothing added to commit but untracked files present (use "git add" to track)> ~/g/d/GitAndSourceTree on master ⨯ touch ./TheCoreFramework/Core.php                                                                                           
⋊> ~/g/d/GitAndSourceTree on master ⨯ git status                                                                                                                  
On branch master

Initial commit

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

    .gitignore
    TheCoreFramework/
    Upload/

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

可以看到 加了 .keep 文件的 Upload 文件夹 是可以被 git add 的 TheCoreFramework则不可以

我们在TheCoreFramework增加一个 Core.php 后就就一样可以被 git add

我们进行一次 添加 提交 推送:

添加: git add

提交:git commit

推送:git push

nothing added to commit but untracked files present (use "git add" to track)> ~/g/d/GitAndSourceTree on master ⨯ git add --all                                                                                                               
⋊> ~/g/d/GitAndSourceTree on master ⨯ git status                                                                                                                  
On branch master

Initial commit

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

    new file:   .gitignore
    new file:   TheCoreFramework/Core.php
    new file:   Upload/.keep

⋊> ~/g/d/GitAndSourceTree on master ⨯ touch Upload/upload.php                                                                                                     
⋊> ~/g/d/GitAndSourceTree on master ⨯ rm Upload/upload.php                                                                                                        
⋊> ~/g/d/GitAndSourceTree on master ⨯ git commit -m '第一次提交 和兴代码框架 空白的文件夹'                                                                        
[master (root-commit) 244aafa] 第一次提交 和兴代码框架 空白的文件夹
 3 files changed, 1 insertion(+)
 create mode 100644 .gitignore
 create mode 100644 TheCoreFramework/Core.php
 create mode 100644 Upload/.keep
⋊> ~/g/d/GitAndSourceTree on master  git push origin master                                                                                                       
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 441 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
 * [new branch]      master -> master
⋊> ~/g/d/GitAndSourceTree on master ◦ git status                                                                                                                  
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

查看本地分支:git branch

查看远端分支:git branch -a

创建本地分支:git branch 分支名称

我们可以看到在本地创建的分支在远端是不存在的我们需要推送到远端 远端才会存在这个分支 其他 有当前分支权限的人 才能检出或者更改你推送到远端的分支 远端分支修改 只要不 拉取 就不会影响本地的分支内容 同样提交不推送也不会影响到远端

> ~/g/d/GitAndSourceTree on master ◦ git branch                                                                                                                  
* master
⋊> ~/g/d/GitAndSourceTree on master ◦ git branch -a                                                                                                               
* master
  remotes/origin/master
⋊> ~/g/d/GitAndSourceTree on master ◦ git branch feature/a_new_feature                                                                                            
⋊> ~/g/d/GitAndSourceTree on master ◦ git branch                                                                                                                  
  feature/a_new_feature
* master
⋊> ~/g/d/GitAndSourceTree on master ◦ git branch -a                                                                                                               
  feature/a_new_feature
* master
  remotes/origin/master
⋊> ~/g/d/GitAndSourceTree on master ◦ 

当创建了新分支 会自动切换到新的分支 我们想要切换分支

切换分支 git checkout

> ~/g/d/GitAndSourceTree on feature/a_new_feature 
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  git checkout master                                                                                           
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.> ~/g/d/GitAndSourceTree on master ◦

两个分之下的代码是互相不影响的

> ~/g/d/GitAndSourceTree on feature/a_new_feature  git checkout master                                                                                           
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.> ~/g/d/GitAndSourceTree on master ◦ ls                                                                                                                          
TheCoreFramework Upload
⋊> ~/g/d/GitAndSourceTree on master ◦ touch master.php                                                                                                            
⋊> ~/g/d/GitAndSourceTree on master ⨯ git status                                                                                                                  
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    master.php

nothing added to commit but untracked files present (use "git add" to track)> ~/g/d/GitAndSourceTree on master ⨯ git add master.php                                                                                                          
⋊> ~/g/d/GitAndSourceTree on master ⨯ git commit -m '这是master.php'                                                                                              
[master 74158b3] 这是master.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master.php
⋊> ~/g/d/GitAndSourceTree on master ↑ ls                                                                                                                          
TheCoreFramework Upload           master.php
⋊> ~/g/d/GitAndSourceTree on master ↑ git branch                                                                                                                  
  feature/a_new_feature
* master
⋊> ~/g/d/GitAndSourceTree on master ↑ git checkout feature/a_new_feature                                                                                          
Switched to branch 'feature/a_new_feature'> ~/g/d/GitAndSourceTree on feature/a_new_feature  ls                                                                                                            
TheCoreFramework Upload
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  git checkout master                                                                                           
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)> ~/g/d/GitAndSourceTree on master ↑ ls                                                                                                                          
TheCoreFramework Upload           master.php
⋊> ~/g/d/GitAndSourceTree on master ↑

我们可以看到在master分支提交的代码 master.php 当我们切换到 feature/a_new_feature 分支上的时候 是不会存在到 这个分支上的 当切换回 master 的时候 master.php 又出现了 但是注意 我们没有 push 到远端的时候 其他人还是看不到的 它只存在于你的本地master分支

我们切换到feature/a_new_feature 分支创建一个aNewFeature.php 提交

> ~/g/d/GitAndSourceTree on master ◦ git checkout feature/a_new_feature                                                                                          
Switched to branch 'feature/a_new_feature'> ~/g/d/GitAndSourceTree on feature/a_new_feature  ls                                                                                                            
TheCoreFramework Upload
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature                                                                                                                
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  touch aNewFeature.php                                                                                         
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ ls                                                                                                           
TheCoreFramework Upload           aNewFeature.php
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git add --all                                                                                                
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git commit -m '这是aNewFeature.php'                                                                          
[feature/a_new_feature a6cbdac] 这是aNewFeature.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aNewFeature.php

这个时候我们这个新的功能已经写完了 我们可以合并到master上了

> ~/g/d/GitAndSourceTree on feature/a_new_feature  git checkout master                                                                                           
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.> ~/g/d/GitAndSourceTree on master ◦ git merge --no-ff feature/a_new_feature                                                                                     
Merge made by the 'recursive' strategy.
 aNewFeature.php | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aNewFeature.php
⋊> ~/g/d/GitAndSourceTree on master ↑

我们要注意要想把feature/a_new_feature合并到master上我们要先 checkout 到 master分支

之后执行 git merge –no-ff feature/a_new_feature (–no-ff 将分支合并当做是一次commit)

如果有冲突请解决冲突后 git commit

如果没有冲突会让你填写 merge commit 的信息 wq! 即可

这个时候 一次合并就完成了 如果需要 我们 可以 push 到远端

> ~/g/d/GitAndSourceTree on master ↑ git push origin master                                                                                                      
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 474 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
   74158b3..c330534  master -> master
⋊> ~/g/d/GitAndSourceTree on master ◦

我们有的时候并不想把没开发完分支的代码全部提交到master分支上去我们只想提交一次commit 的代码过去我们可以

> ~/g/d/GitAndSourceTree on master ◦ git checkout feature/a_new_feature                                                                                          
Switched to branch 'feature/a_new_feature'> ~/g/d/GitAndSourceTree on feature/a_new_feature  ls                                                                                                            
TheCoreFramework Upload           aNewFeature.php
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  touch commit1.php                                                                                             
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git add --all                                                                                                
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git commit -m '这是commit1.php'                                                                              
[feature/a_new_feature 1ee3101] 这是commit1.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 commit1.php
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  touch commit2.php                                                                                             
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git add --all                                                                                                
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git commit -m '这是commit2.php'                                                                              
[feature/a_new_feature 777827a] 这是commit2.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 commit2.php
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  touch commit3.php                                                                                             
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git add --all                                                                                                
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature ⨯ git commit -m '这是commit3.php'                                                                              
[feature/a_new_feature 346cbce] 这是commit3.php
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 commit3.php
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  git log --graph feature/a_new_feature                                                                         
* commit 346cbce1304bc7a8c3ae85b5b90c2401ccf4d1cd
| Author: 田野 <3217834@qq.com>
| Date:   Wed Feb 15 16:06:39 2017 +0800
| 
|     这是commit3.php
|  
* commit 777827ac7eff48a18eeec86cf09e2f91af799da3
| Author: 田野 <3217834@qq.com>
| Date:   Wed Feb 15 16:06:29 2017 +0800
| 
|     这是commit2.php
|  
* commit 1ee31018e0b2e76311ea8e2a60b22cef22891716
| Author: 田野 <3217834@qq.com>
| Date:   Wed Feb 15 16:06:16 2017 +0800
| 
|     这是commit1.php
|  
* commit a6cbdac70e045d532f60d64091402adfba5d16a6
| Author: 田野 <3217834@qq.com>
| Date:   Wed Feb 15 15:55:27 2017 +0800
| 
|     这是aNewFeature.php
|  
* commit 244aafaed976ab462acd89a56921778d9c1c68e4
  Author: 田野 <3217834@qq.com>
  Date:   Wed Feb 15 15:22:06 2017 +0800

      第一次提交 和兴代码框架 空白的文件夹
⋊> ~/g/d/GitAndSourceTree on feature/a_new_feature  git checkout master                                                                                           
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.> ~/g/d/GitAndSourceTree on master ◦ git cherry-pick 777827ac7eff48a18eeec86cf09e2f91af799da3                                                                    
[master 79d629c] 这是commit2.php
 Date: Wed Feb 15 16:06:29 2017 +0800
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 commit2.php
⋊> ~/g/d/GitAndSourceTree on master ↑ ls                                                                                                                          
TheCoreFramework Upload           aNewFeature.php  commit2.php      master.php
⋊> ~/g/d/GitAndSourceTree on master ↑     

我们可以看到 在 feature/a_new_feature 上有三次提交

我们使用* git log –graph feature/a_new_feature* 查看 这个分支上的commit hash

我们切换到master

使用 git cherry-pick 777827ac7eff48a18eeec86cf09e2f91af799da3

把这次 commit 挑选 提交到 master 上

这个时候我们可以看到 master 有了 一次 和 777827ac7eff48a18eeec86cf09e2f91af799da3 一模一样的提交

并不会影响下次合并

> ~/g/d/GitAndSourceTree on master ↑ git merge --no-ff feature/a_new_feature                                                                                     
Merge made by the 'recursive' strategy.
 commit1.php | 0
 commit3.php | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 commit1.php
 create mode 100644 commit3.php
⋊> ~/g/d/GitAndSourceTree on master ↑ git push origin master                                                                                                      
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 844 bytes | 0 bytes/s, done.
Total 8 (delta 4), reused 0 (delta 0)
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
   79d629c..bd49e85  master -> master
⋊> ~/g/d/GitAndSourceTree on master ◦ git status                                                                                                                  
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
⋊> ~/g/d/GitAndSourceTree on master ◦ ls                                                                                                                          
TheCoreFramework Upload           aNewFeature.php  commit1.php      commit2.php      commit3.php      master.php
⋊> ~/g/d/GitAndSourceTree on master ◦   

我们有了这么多次的提交 我们看一下提交的log吧

  • –oneline- 压缩模式,在每个提交的旁边显示经过精简的提交哈希码和提交信息,以一行显示。
  • –graph- 图形模式,使用该选项会在输出的左边绘制一张基于文本格式的历史信息表示图。如果你查看的是单个分支的历史记录的话,该选项无效。
  • –all- 显示所有分支的历史记录
> ~/g/d/GitAndSourceTree on master ◦ git log --pretty=oneline                                                                                                    
bd49e85d696f29a87953c14727eb0cae2253dae3 Merge branch 'feature/a_new_feature'i全部合并
79d629c875e91caa9a86c1ded4446daabbab99a2 这是commit2.php
346cbce1304bc7a8c3ae85b5b90c2401ccf4d1cd 这是commit3.php
777827ac7eff48a18eeec86cf09e2f91af799da3 这是commit2.php
1ee31018e0b2e76311ea8e2a60b22cef22891716 这是commit1.php
c330534b3f2d5c2402e02731dfb2418a05dc7b77 Merge branch 'feature/a_new_feature'
a6cbdac70e045d532f60d64091402adfba5d16a6 这是aNewFeature.php
74158b391fdd2070d05fd489078587ee046c0e5d 这是master.php
244aafaed976ab462acd89a56921778d9c1c68e4 第一次提交 和兴代码框架 空白的文件夹
⋊> ~/g/d/GitAndSourceTree on master ◦ git reflog                                                                                                                  
bd49e85 HEAD@{0}: merge feature/a_new_feature: Merge made by the 'recursive' strategy.
79d629c HEAD@{1}: cherry-pick: 这是commit2.php
c330534 HEAD@{2}: checkout: moving from feature/a_new_feature to master
346cbce HEAD@{3}: commit: 这是commit3.php
777827a HEAD@{4}: commit: 这是commit2.php
1ee3101 HEAD@{5}: commit: 这是commit1.php
a6cbdac HEAD@{6}: checkout: moving from master to feature/a_new_feature
c330534 HEAD@{7}: merge feature/a_new_feature: Merge made by the 'recursive' strategy.
74158b3 HEAD@{8}: checkout: moving from feature/a_new_feature to master
a6cbdac HEAD@{9}: commit: 这是aNewFeature.php
244aafa HEAD@{10}: checkout: moving from master to feature/a_new_feature
74158b3 HEAD@{11}: checkout: moving from feature/a_new_feature to master
244aafa HEAD@{12}: checkout: moving from master to feature/a_new_feature
74158b3 HEAD@{13}: commit: 这是master.php
244aafa HEAD@{14}: checkout: moving from feature/a_new_feature to master
244aafa HEAD@{15}: checkout: moving from master to feature/a_new_feature
244aafa HEAD@{16}: commit (initial): 第一次提交 和兴代码框架 空白的文件夹

我们有的时候并不是要从当前工作提交最新的分支检出新的分支 可能从某一次提交检出新的分支

> ~/g/d/GitAndSourceTree on master ◦ git checkout -b feature/the_second_feature 74158b391fdd2070d05fd489078587ee046c0e5d                                         16:19:47
Switched to a new branch 'feature/the_second_feature'> ~/g/d/GitAndSourceTree on feature/the_second_feature  git branch --list                                                                                        
  feature/a_new_feature
* feature/the_second_feature
  master
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  ls                                                                                                       
TheCoreFramework Upload           master.php
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature

我们根据hash值创建了一个新的分支 叫做 feature/the_second_feature

里面有一直到 74158b391fdd2070d05fd489078587ee046c0e5d 结束的代码 我们就可以继续码代码了

作的差不多了我们也来搞个冲突解决一下吧

> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git diff                                                                                                
diff --cc theSecondFeature.php
index 8792830,dd28965..0000000
--- a/theSecondFeature.php
+++ b/theSecondFeature.php
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
 +211111冲突兔兔突突
++=======
+ 311111冲突兔兔突突
++>>>>>>> feature/the_second_feature2
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git status -s                                                                                           
UU theSecondFeature.php
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git diff -w theSecondFeature.php                                                                        
diff --cc theSecondFeature.php
index 8792830,dd28965..0000000
--- a/theSecondFeature.php
+++ b/theSecondFeature.php
@@@ -1,1 -1,1 +1,5 @@@
++<<<<<<< HEAD
 +211111冲突兔兔突突
++=======
+ 311111冲突兔兔突突
++>>>>>>> feature/the_second_feature2
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git reset --hard                                                                                        
HEAD is now at 3c2239b 来个冲突3
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  cat theSecondFeature.php                                                                                 
211111冲突兔兔突突

我们用git diff 来查看冲突的文件 -w 指定文件

如果我们想用远端的覆盖本地的冲突使用 git reset —hard

建议使用冲突解决工具 如 phpstrom 自带的解决工具 或者 使用 SourceTree 等

有的时候我们想要解决冲突 或者 切换分支 等等 但是害怕代码丢失 或者 不想让它影响现在的 代码 我们可以 给它 暂存起来 则不会在 分支里面存在这个代码 也不会再 本地 被完全的储藏了起来

> ~/g/d/GitAndSourceTree on feature/the_second_feature  ls                                                                                                       
TheCoreFramework     Upload               master.php           theSecondFeature.php
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature                                                                                                           
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  touch stash1.php                                                                                         
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ touch stash2.php                                                                                        
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ touch stash3.php                                                                                        
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ ls                                                                                                      
TheCoreFramework     Upload               master.php           stash1.php           stash2.php           stash3.php           theSecondFeature.php
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git add --all
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git stash save "stash1" 
Saved working directory and index state On feature/the_second_feature: stash1
HEAD is now at 3c2239b 来个冲突3
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  git stash list                                                                                           
stash@{0}: On feature/the_second_feature: stash1
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  git stash show 'stash@{1}'                                                                               
 stash1.php | 0
 stash2.php | 0
 stash3.php | 0
 3 files changed, 0 insertions(+), 0 deletions(-)

我们创建了3个文件 stash1.php stash2.php stash3.php

之后我们使用 git add —all 添加

我们储藏add后的文件 使用 git stash save “描述”

我们想要查看存储列表* git stash list*

想要查看某一个存储块的内容 git stash show ‘stash@{1}’

如果我们想要使用stash@{1}里面的内容并且使用后自动删除存储 使用 git stash pop ‘stash@{1}’

> ~/g/d/GitAndSourceTree on feature/the_second_feature  git stash pop 'stash@{1}'                                                                                
On branch feature/the_second_feature
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   stash1.php
    new file:   stash2.php
    new file:   stash3.php

Dropped stash@{1} (f1abc0ba062464be50ea3283d250e4b8ecffc270)> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git stash list                                                                                          
stash@{0}: On feature/the_second_feature: stash2
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯

只使用存储块 git stash apply

删除存储块 git stash drop

我们常常希望添加一个Tag用来上线

应为打上Tag的提交内容不能够在被更改

我们可以随时 使用 这个 Tag 来做自动上线 或者 是一些包 的版本更新

> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag v1.0.0 244aafaed976ab462acd89a56921778d9c1c68e4                                                 
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag                                                                                                 
v1.0.0
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git show v1.0.0                                                                                         
commit 244aafaed976ab462acd89a56921778d9c1c68e4
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 15:22:06 2017 +0800

    第一次提交 和兴代码框架 空白的文件夹

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..485dee6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+.idea
diff --git a/TheCoreFramework/Core.php b/TheCoreFramework/Core.php
new file mode 100644
index 0000000..e69de29
diff --git a/Upload/.keep b/Upload/.keep
new file mode 100644
index 0000000..e69de29
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag -a v1.0.1 -m "描述" 74158b391fdd2070d05fd489078587ee046c0e5d                                    
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag                                                                                                 
v1.0.0
v1.0.1
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git show v1.0.1                                                                                         
tag v1.0.1
Tagger: 田野 <3217834@qq.com>
Date:   Wed Feb 15 17:49:41 2017 +0800

描述

commit 74158b391fdd2070d05fd489078587ee046c0e5d
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 15:46:57 2017 +0800

    这是master.php

diff --git a/master.php b/master.php
new file mode 100644
index 0000000..e69de29
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag v1.0.2 244aafaed976ab462acd89a56921778d9c1c68e4                                                 
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag                                                                                                 
v1.0.0
v1.0.1
v1.0.2
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag -d v1.0.2                                                                                       
Deleted tag 'v1.0.2' (was 244aafa)> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag                                                                                                 
v1.0.0
v1.0.1
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git push origin v1.0.0                                                                                  
Total 0 (delta 0), reused 0 (delta 0)
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
 * [new tag]         v1.0.0 -> v1.0.0
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git push origin --tags                                                                                  
Counting objects: 1, done.
Writing objects: 100% (1/1), 165 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
 * [new tag]         v1.0.1 -> v1.0.1
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag v1.0.2 244aafaed976ab462acd89a56921778d9c1c68e4                                                 
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git push origin --tags                                                                                  
Total 0 (delta 0), reused 0 (delta 0)
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
 * [new tag]         v1.0.2 -> v1.0.2
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git tag -d v1.0.2                                                                                       
Deleted tag 'v1.0.2' (was 244aafa)> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯ git push origin :refs/targs/v1.0.2                                                                      
remote: warning: Deleting a non-existent ref.
To ssh://git@xxx.xxx.xxx/Demo/GitAndSourceTree.git
 - [deleted]         refs/targs/v1.0.2
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature ⨯

git tag v1.0 给当前的分支,当前版本打上标签为1.0

git tag v1.0 6224937 给6224937…这个版本号打上标签 1.0

git tag 查看标签(标签不是按时间顺序列出,而是按字母顺序排序的)

git show tagname 查看标签信息

git tag -a v0.1 -m “description” 6224937 创建带有说明的标签

git tag -s v0.2 -m “description” fec145c 通过-s用私钥签名一个标签

git tag -d v0.1 删除标签

git push origin v1.0 推送某个标签到远程
git push origin —tags 一次性推送全部尚未推送到远程的标签

git push origin :refs/targs/v0.9 从远程删除标签(要先删除本地的标签)

我们总是会有很多次垃圾提交我们来销毁它们

> ~/g/d/GitAndSourceTree on feature/the_second_feature  git log                                                                                                  
commit ee28795273089df6cd02e07e1adb2e1893bc8bc9
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 18:36:51 2017 +0800

    来个冲突啊0

    来个冲突吧1

commit 86e0f2982eca68f9f21e454461ad732e31979e48
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 18:36:41 2017 +0800

    来个冲突啊

commit 74158b391fdd2070d05fd489078587ee046c0e5d
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 15:46:57 2017 +0800

    这是master.php

commit 244aafaed976ab462acd89a56921778d9c1c68e4
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 15:22:06 2017 +0800

    第一次提交 和兴代码框架 空白的文件夹
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  git rebase -i HEAD~2
pick 86e0f29 来个冲突啊
pick(修改为 squash) ee28795 来个冲突啊0

# Rebase 74158b3..ee28795 onto 74158b3 (2 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

wq! 保存后会让重新填写commit message

# This is a combination of 2 commits.
# The first commit's message is:

来个冲突啊

# This is the 2nd commit message:

来个冲突啊0

来个冲突吧1

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Feb 15 18:36:41 2017 +0800
#
# interactive rebase in progress; onto 74158b3
# Last commands done (2 commands done):
#    pick 86e0f29 来个冲突啊
#    squash ee28795 来个冲突啊0
# No commands remaining.
# You are currently editing a commit while rebasing branch 'feature/the_second_feature' on '74158b3'.
#
# Changes to be committed:
#       new file:   aaa1.php
#       new file:   aaa2.php
#       new file:   aaa3.php
#       new file:   aaa4.php
#

看一下我们销毁的成果

[detached HEAD 4cdf9a7] 来个冲突啊
 Date: Wed Feb 15 18:36:41 2017 +0800
 4 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 aaa1.php
 create mode 100644 aaa2.php
 create mode 100644 aaa3.php
 create mode 100644 aaa4.php
Successfully rebased and updated refs/heads/feature/the_second_feature.
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature  git log                                                                                                  
commit 4cdf9a7abc1ef0d357ff64846e78cbbbc08c4ef3
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 18:36:41 2017 +0800

    来个冲突啊

    来个冲突啊0

    来个冲突吧1

commit 74158b391fdd2070d05fd489078587ee046c0e5d
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 15:46:57 2017 +0800

    这是master.php

commit 244aafaed976ab462acd89a56921778d9c1c68e4
Author: 田野 <3217834@qq.com>
Date:   Wed Feb 15 15:22:06 2017 +0800

    第一次提交 和兴代码框架 空白的文件夹
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature

git reflog 可以看到每一次的提交操作

> ~/g/d/GitAndSourceTree on feature/the_second_feature  git reflog                                                                                               18:45:02
4cdf9a7 HEAD@{0}: rebase -i (finish): returning to refs/heads/feature/the_second_feature
4cdf9a7 HEAD@{1}: rebase -i (squash): 来个冲突啊
86e0f29 HEAD@{2}: rebase -i (start): checkout HEAD~2
ee28795 HEAD@{3}: rebase -i (finish): returning to refs/heads/feature/the_second_feature
ee28795 HEAD@{4}: rebase -i (squash): 来个冲突啊0
a049e60 HEAD@{5}: rebase -i (start): checkout HEAD~2
84c7c2e HEAD@{6}: commit: 来个冲突吧
a049e60 HEAD@{7}: rebase -i (finish): returning to refs/heads/feature/the_second_feature
a049e60 HEAD@{8}: rebase -i (start): checkout HEAD~2
9cc2b97 HEAD@{9}: rebase -i (finish): returning to refs/heads/feature/the_second_feature
9cc2b97 HEAD@{10}: rebase -i (start): checkout HEAD~4
9cc2b97 HEAD@{11}: commit: 来个冲突啊
a049e60 HEAD@{12}: commit: 来个冲突啊
86e0f29 HEAD@{13}: commit: 来个冲突啊
74158b3 HEAD@{14}: rebase -i (finish): returning to refs/heads/feature/the_second_feature
74158b3 HEAD@{15}: rebase -i (start): checkout HEAD~6
e3afa3e HEAD@{16}: commit: 来个冲突啊
3c2239b HEAD@{17}: checkout: moving from feature/the_second_feature2 to feature/the_second_feature
a2eff45 HEAD@{18}: commit: 来个冲突4
9d2abbc HEAD@{19}: checkout: moving from feature/the_second_feature to feature/the_second_feature2
3c2239b HEAD@{20}: commit: 来个冲突3
9d2abbc HEAD@{21}: merge feature/the_second_feature2: Fast-forward
210cd34 HEAD@{22}: checkout: moving from feature/the_second_feature2 to feature/the_second_feature
9d2abbc HEAD@{23}: commit: 来个冲突2
7800707 HEAD@{24}: checkout: moving from feature/the_second_feature to feature/the_second_feature2
210cd34 HEAD@{25}: checkout: moving from feature/the_second_feature2 to feature/the_second_feature
7800707 HEAD@{26}: commit: 来个冲突2
210cd34 HEAD@{27}: checkout: moving from feature/the_second_feature to feature/the_second_feature2
210cd34 HEAD@{28}: checkout: moving from feature/the_second_feature2 to feature/the_second_feature
210cd34 HEAD@{29}: checkout: moving from feature/the_second_feature to feature/the_second_feature2
210cd34 HEAD@{30}: commit: 来个冲突1
74158b3 HEAD@{31}: checkout: moving from master to feature/the_second_feature
bd49e85 HEAD@{32}: merge feature/a_new_feature: Merge made by the 'recursive' strategy.
79d629c HEAD@{33}: cherry-pick: 这是commit2.php
c330534 HEAD@{34}: checkout: moving from feature/a_new_feature to master
346cbce HEAD@{35}: commit: 这是commit3.php
777827a HEAD@{36}: commit: 这是commit2.php
1ee3101 HEAD@{37}: commit: 这是commit1.php
a6cbdac HEAD@{38}: checkout: moving from master to feature/a_new_feature
c330534 HEAD@{39}: merge feature/a_new_feature: Merge made by the 'recursive' strategy.
74158b3 HEAD@{40}: checkout: moving from feature/a_new_feature to master
a6cbdac HEAD@{41}: commit: 这是aNewFeature.php
244aafa HEAD@{42}: checkout: moving from master to feature/a_new_feature
74158b3 HEAD@{43}: checkout: moving from feature/a_new_feature to master
244aafa HEAD@{44}: checkout: moving from master to feature/a_new_feature
74158b3 HEAD@{45}: commit: 这是master.php
244aafa HEAD@{46}: checkout: moving from feature/a_new_feature to master
244aafa HEAD@{47}: checkout: moving from master to feature/a_new_feature
244aafa HEAD@{48}: commit (initial): 第一次提交 和兴代码框架 空白的文件夹
⋊> ~/g/d/GitAndSourceTree on feature/the_second_feature

看一下文件的每一次被操作

git blame 文件名

> ~/g/pay_itbt on basis/center ⨯ git blame composer.json                                                                                                         18:49:40
^0f9b035 (田野      2016-11-08 11:58:31 +0800  1) {
^0f9b035 (田野      2016-11-08 11:58:31 +0800  2)     "name": "framework/staging",
^0f9b035 (田野      2016-11-08 11:58:31 +0800  3)     "description": "The ThinkPHP Framework.",
^0f9b035 (田野      2016-11-08 11:58:31 +0800  4)     "keywords": [
^0f9b035 (田野      2016-11-08 11:58:31 +0800  5)         "framework",
^0f9b035 (田野      2016-11-08 11:58:31 +0800  6)         "thinkphp"
^0f9b035 (田野      2016-11-08 11:58:31 +0800  7)     ],
^0f9b035 (田野      2016-11-08 11:58:31 +0800  8)     "license": "MIT",
^0f9b035 (田野      2016-11-08 11:58:31 +0800  9)     "type": "project",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 10)     "require": {
738757ec (sunxs     2016-11-26 17:06:57 +0800 11)         "SDK/dealer-api":"^1.0.4",
807fe0f9 (sunxs     2016-11-27 11:21:54 +0800 12)         "SDK/distributor-api":"^1.0.10",
807fe0f9 (sunxs     2016-11-27 11:21:54 +0800 13)               "framework/thinkphp": "~3.2",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 14)         "robmorgan/phinx": "^0.6.5",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 15)         "friendsofphp/php-cs-fixer": "^1.12",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 16)         "overtrue/validation": "^2.0",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 17)         "predis/predis": "^1.1",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 18)         "guzzlehttp/guzzle": "~6.2",
de7611a9 (田野      2016-11-08 16:13:38 +0800 19)         "symfony/http-foundation": "^3.1",
05dfafd3 (田野      2016-11-09 17:20:10 +0800 20)         "nesbot/carbon": "^1.21",
05dfafd3 (田野      2016-11-09 17:20:10 +0800 21)         "wei587/pay-sdk": "~6.1",
05cf10fe (qindeming 2017-01-18 14:03:09 +0800 22)         "SDK/helper": "~1.1",
05cf10fe (qindeming 2017-01-18 14:03:09 +0800 23)         "endroid/qrcode": "^1.9"
^0f9b035 (田野      2016-11-08 11:58:31 +0800 24)     },
^0f9b035 (田野      2016-11-08 11:58:31 +0800 25)     "repositories": [
^0f9b035 (田野      2016-11-08 11:58:31 +0800 26)         {
^0f9b035 (田野      2016-11-08 11:58:31 +0800 27)             "type": "composer",
^0f9b035 (田野      2016-11-08 11:58:31 +0800 28)             "url": "http://packages.xxx.xxx"
^0f9b035 (田野      2016-11-08 11:58:31 +0800 29)         }
^0f9b035 (田野      2016-11-08 11:58:31 +0800 30)     ],
^0f9b035 (田野      2016-11-08 11:58:31 +0800 31)     "config": {
^0f9b035 (田野      2016-11-08 11:58:31 +0800 32)         "secure-http": false
^0f9b035 (田野      2016-11-08 11:58:31 +0800 33)     }
^0f9b035 (田野      2016-11-08 11:58:31 +0800 34) }> ~/g/pay_itbt on basis/center ⨯


话锋一转,来到了 composer 的 tag 版本号控制需要注意的一些小细节.

对于打标签

1.1.1.20170215_beta

主版本号.次版本号.修订版本号.日期版本号_希腊字母版本号

主版本号:大的项目或者大规模的改版可以不向下兼容

次版本号:增加新的功能此版本号要更新

修正版本号:如果代码有BUG修复不是添加功能本此版本号要更新

日期版本号:可以选填 根据实际情况

希腊字母版本号:

Base:此版本表示该软件仅仅是一个假页面链接,通常包括所有的功能和页面布局,但是 页面中的功能都没有做完整的实现,只是做为整体网站的一个基础架构。

Alpha :软件的初级版本,表示该软件在此阶段以实现软件功能为主,通常只在软件开发者 内部交流,一般而言,该版本软件的Bug较多,需要继续修改,是测试版本。测试 人员提交Bug经开发人员修改确认之后,发布到测试网址让测试人员测试,此时可 将软件版本标注为alpha版。

Beta :该版本相对于Alpha 版已经有了很大的进步,消除了严重错误,但还需要经过多次 测试来进一步消除,此版本主要的修改对象是软件的UI。修改的的Bug 经测试人 员测试确认后可发布到外网上,此时可将软件版本标注为 beta版。

RC :该版本已经相当成熟了,基本上不存在导致错误的Bug,与即将发行的正式版本相差 无几。

Release:该版本意味“最终版本”,在前面版本的一系列测试版之后,终归会有一个正式的 版本,是最终交付用户使用的一个版本。该版本有时也称标准版。

COMPOSER 版本号选择:

“~”:指定向后兼容的最小版本

~1.2 等于 >=1.2.0 && <1.3.0

~1.2.3 等于 >=1.2.3 && <1.3.0 •


“^”:允许大版本前的所有版本

• •

^1.2 等于 >=1.2 && <2.0.0
^1.2.3 等于 >= 1.2.3 && < 2.0 ( )

使用版本号

确切版本: 1.0.2

范围版本: >=1.0 >=1.0 <2.0 >=1.0 <1.1 || >=1.2

连字符范围: 1.0 - 2.0

通配符: 1.0.*
波浪运算符: ~1.2.3
^运算符: ^1.2.3

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1
liyu001989

话锋一转,来到了composer
但是

^1.2 等于 >=1.2 && <2.0.0

应该是

^1.2 等于 >=1.2.0 && <1.3.0

https://getcomposer.org/doc/articles/versi...

7年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!