[toc]

git 干货

简述&特性

2005年Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!2008年,GitHub网站上线.
CVS及SVN都是集中式的版本控制系统,而Git是分布式版本控制系统
分布式版本控制系统根本没有“中央服务器”,每个人的电脑上都是一个完整的版本库
安全性和集中式版本控制系统相比,分布式版本控制系统的安全性要高很多,因为每个人电脑里都有完整的版本库,某一个人的电脑坏掉了不要紧,随便从其他人那里复制一个就可以了。而集中式版本控制系统的中央服务器要是出了问题,所有人都没法干活了。

安装&账户设置

不过好多机器默认都已经安装了git可以首先git下看

centos可以使用yum安装
yum install git
如果是基于debian的系统可以使用如下命令
sudo apt-get install git

苹果系统的小伙伴可以使用https://git-scm.com/download/mac下载即可

这里以centos7.4为例进行账户设置因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。

[root@hhk ~]# git config --global user.name "winds"
[root@hhk ~]# git config  --global  user.email "228966994@qq.com"

注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

版本库设置

repository n. 贮藏室,仓库;知识库;智囊团

创建一个版本需要进行版本控制库的目录

[root@hhk ~]# mkdir git
[root@hhk ~]# cd git/
[root@hhk git]# pwd
/root/git

初始化

git init命令把这个目录变成Git可以管理的仓库

[root@hhk git]# git init
Initialized empty Git repository in /root/git/.git/

当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。

[root@hhk git]# ls -ah
.  ..  .git

所有的版本控制系统,其实只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等等,Git也不例外。版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词“Linux”,在第8行删了一个单词“Windows”。而图片、视频这些二进制文件,虽然也能由版本控制系统管理,但没法跟踪文件的变化,只能把二进制文件每次改动串起来,也就是只知道图片从100KB改成了120KB,但到底改了啥,版本控制系统不知道,也没法知道。
tps千万不要使用Windows自带的记事本编辑任何文本文件。原因是Microsoft开发记事本的团队使用了一个非常弱智的行为来保存UTF-8编码的文件,他们自作聪明地在每个文件开头添加了0xefbbbf(十六进制)的字符,你会遇到很多不可思议的问题,比如,网页第一行可能会显示一个“?”,明明正确的程序一编译就报语法错误,等等,都是由记事本的弱智行为带来的。建议你下载Notepad++代替记事本,不但功能强大,而且免费!记得把Notepad++的默认编码设置为UTF-8 without BOM即可

git一个文件

[root@hhk git]# vim a.txt
`在所在的文件夹中`
[root@hhk git]# git add a.txt



`查看状态`
[root@hhk git]# git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#  new file:   a.txt
#
`将要进行提交的文件,可以使用 (use "git rm --cached <file>..." 来进行撤销)`

查看状态可以知道这时只是添加并没有进行提交

下面正式进行提交刚才add的a.txt文件

[root@hhk git]# git commit -m "注释"


[master (root-commit) fd0c556] 注释
 1 file changed, 2 insertions(+)
 create mode 100644 a.txt


这才正式提交,可以对多个文件进行git add 后在进行一起commit

对一个已经提交过的文件进行修改查看状态

root@hhk git]# vim a.txt 
`修改文件`
[root@hhk git]# git status 
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
`提示可以基于加入需要入库的文件`
#   (use "git checkout -- <file>..." to discard changes in working directory)
`提示使用git checkout 来放弃当前本地的目录的修改并使用之前的版本`
#
#  modified:   a.txt
`提示上述文件进行了修改`
#
no changes added to commit (use "git add" and/or "git commit -a")

修改的内容可以使用git diff a.txt命令查看

[root@hhk git]# git diff a.txt
diff --git a/a.txt b/a.txt
index 1584923..0d84d15 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,4 @@
 test
-hello word!
+
+
+sdfasdfhello word!

`显示的格式正是Unix通用的diff格式`

修改,使用之前常规方法进行提交

[root@hhk git]# git add a.txt

`入库然后查看状态`
[root@hhk git]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#  modified:   a.txt

`提交并注释`
[root@hhk git]# git commit -m "bbb"
[master eafc885] bbb
 1 file changed, 3 insertions(+), 1 deletion(-)

查看状态

[root@hhk git]# git status 
# On branch master
nothing to commit, working directory clean

再次进行修改

[root@hhk git]# vim a.txt 
[root@hhk git]# git add a.txt
[root@hhk git]# git commit -m "相关信息"
[master 197ddf4] 相关信息
 1 file changed, 3 insertions(+), 1 deletion(-)
[root@hhk git]# ll
total 4
-rw-r--r-- 1 root root 40 May 12 21:09 a.txt
[root@hhk git]# git status 
# On branch master
nothing to commit, working directory clean

查看日志

[root@hhk git]# git log
commit 197ddf49598c407e4c224c93d1d5da678a64175a
Author: winds <228966994@qq.com>
Date:   Sat May 12 21:10:41 2018 +0800

    相关信息

commit eafc885e91555af9de1c4ee1fd0c6ece9c928424
Author: winds <228966994@qq.com>
Date:   Sat May 12 20:03:20 2018 +0800

    bbb

commit fd0c556ee0a80f1c5fa66e9aca4af1559afb88bb
Author: winds <228966994@qq.com>
Date:   Fri May 11 05:01:03 2018 +0800

    注释

    `--pretty=oneline 美化操作`
[root@hhk git]# git log --pretty=oneline 
197ddf49598c407e4c224c93d1d5da678a64175a 相关信息
eafc885e91555af9de1c4ee1fd0c6ece9c928424 bbb

尝试版本回退

Git中,用HEAD表示当前版本,也就是最新的提交3628164…882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
git reset --hard HEAD^

[root@hhk git]# git reset --hard HEAD
HEAD is now at eafc885 bbb


usage: git reset [–mixed | –soft | –hard | –merge | –keep] [-q] []
or: git reset [-q] [–]
or: git reset –patch [] [–] […]
-q, –quiet be quiet, only report errors
–mixed reset HEAD and index
–soft reset only HEAD
–hard reset HEAD, index and working tree
–merge reset HEAD, index and working tree
–keep reset HEAD but keep local changes
-p, –patch select hunks interactively

已知指定版本号进行前往

[root@hhk git]# git reset --hard 197
fatal: ambiguous argument '197': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
`虽然版本号的部分不用打全但是还是不要太短`
[root@hhk git]# git reset --hard 197ddf4
HEAD is now at 197ddf4 相关信息

`再次git log 之前看不到的部分就又再次出现了`
[root@hhk git]# git log
commit 197ddf49598c407e4c224c93d1d5da678a64175a
Author: winds <228966994@qq.com>
Date:   Sat May 12 21:10:41 2018 +0800

    相关信息

commit eafc885e91555af9de1c4ee1fd0c6ece9c928424
Author: winds <228966994@qq.com>
Date:   Sat May 12 20:03:20 2018 +0800

    bbb

commit fd0c556ee0a80f1c5fa66e9aca4af1559afb88bb
Author: winds <228966994@qq.com>
Date:   Fri May 11 05:01:03 2018 +0800


`Git提供了一个命令git reflog用来记录你的每一次命令:`
[root@hhk git]# git reflog 
197ddf4 HEAD@{0}: reset: moving to 197ddf4
fd0c556 HEAD@{1}: reset: moving to HEAD^
eafc885 HEAD@{2}: reset: moving to HEAD^
197ddf4 HEAD@{3}: commit: 相关信息
eafc885 HEAD@{4}: commit: bbb
fd0c556 HEAD@{5}: commit (initial): 注释

stage(或者叫index)的暂存区

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。暂存区这时是变成空的.

git checkout — file git reset HEAD file

[root@hhk git]# vim a.txt
`修改一个文件`
[root@hhk git]# git checkout -- a.txt
`如果没有保存到暂存区会从master检出若已经保存到index stage则从暂存区检出`

[root@hhk git]# vim a.txt 
[root@hhk git]# git add a.txt
[root@hhk git]# git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
`提示清除暂存区指定文件的信息(use "git reset HEAD <file>..." to unstage)`
#
#  modified:   a.txt
#
`下面进行清除刚才的暂存`
[root@hhk git]# git reset HEAD a.txt
Unstaged changes after reset:
M   a.txt

`这时再执行检出操作就是从分支节点而不是暂存区进行检出了`

因此如下进行删除一个已经add的文件后可以通过git checkout -- ccc.txt进行恢复

[root@hhk git]# vim ccc.txt
[root@hhk git]# git add ccc.txt
[root@hhk git]# git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#  new file:   ccc.txt
#
# 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:   a.txt
#
[root@hhk git]# rm ccc.txt 
rm: remove regular file ‘ccc.txt’? y
[root@hhk git]# ll
total 8
-rw-r--r-- 1 root root 62 May 12 22:12 a.txt
-rw-r--r-- 1 root root  4 May 12 21:43 bbb.txt
[root@hhk git]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#  new file:   ccc.txt
#
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#  modified:   a.txt
#  deleted:    ccc.txt
#
[root@hhk git]# git checkout -- ccc.txt


我们还可以确认丢掉这个已经删除的文件从暂存区中

[root@hhk git]# rm ccc.txt 
rm: remove regular file ‘ccc.txt’? y
[root@hhk git]# git rm ccc.txt
rm 'ccc.txt'

创建SSH key

一切皆按照默认配置一路会车即可

[root@hhk .ssh]# ssh-keygen  -t rsa -C "228966994@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:vuEFTK5s7RSCalSgfsSZ/03YJeRgf86jZt4e0eEHlU0 228966994@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|  .    o .     oE|
| ...o . =     ...|
|.  =.   .+ o o   |
|. .... +o * o o  |
| .......So = o . |
| ... ..=oo. o .  |
|  o   +.==..     |
| .   . +++. .    |
|        +..o     |
+----[SHA256]-----+

qu@DESKTOP-7CQT99I MINGW64 /c/git
$ ssh-keygen -t rsa -C "228966994@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/qu/.ssh/id_rsa):
Created directory '/c/Users/qu/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/qu/.ssh/id_rsa.
Your public key has been saved in /c/Users/qu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:A7QvRBNj3pJm79VSht/dfU0iQt3L8MYKJ9hsvQecbOs 228966994@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|      B.  .. .   |
|     = * . .o .  |
|      X .+o++*...|
|     + =. **Oo*++|
|      . S.o=o*. *|
|       o o .+ . .|
|        .  . .   |
|            E    |
|                 |
+----[SHA256]-----+

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
下面是win下创建的方法

qu@DESKTOP-7CQT99I MINGW64 /c/git
$ ssh-keygen -t rsa -C "228966994@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/qu/.ssh/id_rsa):
Created directory '/c/Users/qu/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/qu/.ssh/id_rsa.
Your public key has been saved in /c/Users/qu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:A7QvRBNj3pJm79VSht/dfU0iQt3L8MYKJ9hsvQecbOs 228966994@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|      B.  .. .   |
|     = * . .o .  |
|      X .+o++*...|
|     + =. **Oo*++|
|      . S.o=o*. *|
|       o o .+ . .|
|        .  . .   |
|            E    |
|                 |
+----[SHA256]-----+

上传id_rsa.pub的内容公钥到github


两个都上传后

github上创建一个库然后进行连接


按照图网站的提示将项目进行提交

…or create a new repository on the command line
echo "# stocksocket" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:oklutu/stocksocket.git
git push -u origin master


…or push an existing repository from the command line
git remote add origin git@github.com:oklutu/stocksocket.git
git push -u origin master



…or import code from another repository
You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

上面的是网站上的提示部分同图当然这是按照我的项目来进行输入的

使用下面命令进行连接提交

[root@hhk git]# vim README.md 
[root@hhk git]# git add README.md
[root@hhk git]# git commit -m "first commit"
[master df40556] first commit
 1 file changed, 2 insertions(+), 1 deletion(-)
[root@hhk git]# git remote add origin git@github.com:oklutu/stocksocket.git
[root@hhk git]# git push -u origin master
Counting objects: 24, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (24/24), 2.06 KiB | 0 bytes/s, done.
Total 24 (delta 0), reused 0 (delta 0)
To git@github.com:oklutu/stocksocket.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库。

由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。
从现在起,只要本地作了提交,就可以通过命令:

$ git push origin master

把本地master分支的最新修改推送至GitHub,现在,你就拥有了真正的分布式版本库!

SSH警告

当你第一次使用Git的clone或者push命令连接GitHub时,会得到一个警告:

The authenticity of host ‘github.com (xx.xx.xx.xx)’ can’t be established.
RSA key fingerprint is xx.xx.xx.xx.xx.
Are you sure you want to continue connecting (yes/no)?
这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。

Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了:

Warning: Permanently added ‘github.com’ (RSA) to the list of known hosts.

小结 要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git;

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

从远程库克隆

首先新建一个项目组合 资源库 储存库 资料库new respository
勾选Initialize this repository with a README
比如我这里创建一个简单的企业站的项目dldxkj
下面就需要注意不要直接回车要输入yes

qu@DESKTOP-7CQT99I MINGW64 /c/git
$ git clone git@github.com:oklutu/dldxkj.git
Cloning into 'dldxkj'...
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

qu@DESKTOP-7CQT99I MINGW64 /c/git
$
qu@DESKTOP-7CQT99I MINGW64 /c/git
$ git clone git@github.com:oklutu/dldxkj.git
Cloning into 'dldxkj'...
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

qu@DESKTOP-7CQT99I MINGW64 /c/git
$

此时当前目录就会有一个镜像文件夹
Git支持多种协议,默认的git://使用ssh,但也可以使用https等其他协议。
但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https。

分支


qu@DESKTOP-7CQT99I MINGW64 /c/git $ cd dldxkj qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master) $ git checkout -b dev Switched to a new branch 'dev' `-b 代表创建分支并进行切换相当于下面两个命令 branch` $ git branch dev $ git checkout dev Switched to branch 'dev'

git branch命令查看当前分支:

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git branch
* dev
  master

再次进行修改

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git status
On branch dev
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

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

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git add README.md

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git commit -m "添加相关描述"

[dev b6c6b7e] 添加相关描述
 1 file changed, 2 insertions(+), 1 deletion(-)

切换回master

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

此时发现之前的修改的本地文件已经变成master上的样子了

融合分支git merge命令用于合并指定分支到当前分支。合并后,再查看readme.txt的内容,就可以看到,和dev分支的最新提交是完全一样的。

$ git merge dev
Updating 25f2737..b6c6b7e
Fast-forward
 README.md | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

这时dev分支依然存在

删除分支 git branch -d 分支名称

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git branch -d dev
Deleted branch dev (was b6c6b7e).

分支常用命令集合

命令 代码
查看分支 git branch
创建分支 git branch <name>
切换分支 git checkout <name>
创建并进行切换 git checkout -b <name>
分支合并到当前分支 git merge <name>
删除分支 git branch -b <name>

冲突的解决

创建一个分支

$ git checkout -b vranc
Switched to a new branch 'vranc'

对文件进行修改在进行提交

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (vranc)
$ git add README.md

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (vranc)
$ git commit README.md -m "添加too_"
[vranc 7e52b5a] 添加too_
 1 file changed, 4 insertions(+), 1 deletion(-)

切换到master然后进行修改文件

$ 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)

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git branch
* master
  vranc


`这时候对README.md进行修改下面在进行提交`



qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git add README.md

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git commit README.md -m  "master"
[master 283c20a] master
 1 file changed, 3 insertions(+), 1 deletion(-)






`下面进行融合操作会发现报错`
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git merge vranc
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.





`git status提示`
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   README.md

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

这时候提示需要对指定文件惊醒修改在进行提交
进行修改add commit 不在赘述代码
下面使用git log --graph查看结构

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git log --graph
*   commit ba0720dcf98763c31593edffecdf229dcf965d18 (HEAD -> master)
|\  Merge: 283c20a 7e52b5a
| | Author: windswin <228966994@qq.com>
| | Date:   Sun May 13 22:43:24 2018 +0800
| |
| |     fix
| |
| * commit 7e52b5aa6ebc1c2814454dcd390360681de52fd8 (vranc)
| | Author: windswin <228966994@qq.com>
| | Date:   Sun May 13 21:52:45 2018 +0800
| |
| |     <E6><B7><BB><E5><8A><A0>too_
| |
* | commit 283c20a18561229f8f00750749efae9b51610b8a
|/  Author: windswin <228966994@qq.com>
|   Date:   Sun May 13 22:24:08 2018 +0800
|
|       master
|
* commit b6c6b7e8845ebfd42702c1bd59192c2ac7b42306
| Author: oklutu <228966994@qq.com>
| Date:   Sun May 13 21:24:05 2018 +0800
|
|     <E6><B7><BB><E5><8A><A0><E7>B8><E5><85><B3><E6><8F><8F><E8><BF><B0>
|
* commit 25f2737026c607d5a9dea3206a23f8c82594e4ef (origin/master, origin/HEAD)
  Author: oklutu <228966994@qq.com>
  Date:   Sun May 13 15:09:59 2018 +0800

      Initial commit
`删除分支后再次进行查看`
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git branch -d vranc
Deleted branch vranc (was 7e52b5a).

qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git log --graph
*   commit ba0720dcf98763c31593edffecdf229dcf965d18 (HEAD -> master)
|\  Merge: 283c20a 7e52b5a
| | Author: windswin <228966994@qq.com>
| | Date:   Sun May 13 22:43:24 2018 +0800
| |
| |     fix
| |
| * commit 7e52b5aa6ebc1c2814454dcd390360681de52fd8
| | Author: windswin <228966994@qq.com>
| | Date:   Sun May 13 21:52:45 2018 +0800
| |
| |     <E6><B7><BB><E5><8A><A0>too_
| |
* | commit 283c20a18561229f8f00750749efae9b51610b8a
|/  Author: windswin <228966994@qq.com>
|   Date:   Sun May 13 22:24:08 2018 +0800
|
|       master
|
* commit b6c6b7e8845ebfd42702c1bd59192c2ac7b42306
| Author: oklutu <228966994@qq.com>
| Date:   Sun May 13 21:24:05 2018 +0800
|
|     <E6><B7><BB><E5><8A><A0><E7>B8><E5><85><B3><E6><8F><8F><E8><BF><B0>
|
* commit 25f2737026c607d5a9dea3206a23f8c82594e4ef (origin/master, origin/HEAD)
  Author: oklutu <228966994@qq.com>
  Date:   Sun May 13 15:09:59 2018 +0800

      Initial commit

git log --graph命令可以看到分支合并图。

分支融合模式

合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。下面我们实战一下–no-ff方式的git merge:
再次创建分支
“`bash
$ git checkout -b dev
Switched to a new branch ‘dev’

进行修改文件
```bash
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git add README.md
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git commit -m "dev提交"
[dev 07f7be2] dev鎻愪氦
 1 file changed, 1 insertion(+), 5 deletions(-)
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (dev)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
  (use "git push" to publish your local commits)
qu@DESKTOP-7CQT99I MINGW64 /c/git/dldxkj (master)
$ git merge --no-ff -m "with --no-ff merge"
Already up to date.

推荐好文关于git常见错误的解决方案
https://blog.csdn.net/dengjianqiang2011/article/details/9260435

分类: 未分类

发表评论

电子邮件地址不会被公开。 必填项已用*标注