Git工具

Git工具

命令行

Git有多重方式使用

  • 原生命令行,才能使用git所有命令,会git命令再去用gui图形工具,完全无压力
  • GUI图形软件,只是实现了git的部分功能,以减免操作难度,难以记住git原生命令
  • 不同的人会有不同的GUI图形工具,但是所有人用的git原生命令都一样,推荐学习命令

Windows安装

在Windows上使用Git,可以从Git官网直接下载安装程序,然后按默认选项安装即可。

安装完成后,在开始菜单里找到“Git”->“Git Bash”,蹦出一个类似命令行窗口的东西,就说明Git安装成功!

在 Mac 上安装

在mac安装git方式很多,最简单是用brew包管理

安装homebrew,然后通过homebrew安装Git,具体方法请参考homebrew的文档:http://brew.sh/。

brew install git

如果你想安装更新的版本,可以使用二进制安装程序。 官方维护的 OSX Git 安装程序可以在 Git 官方网站下载,网址为 http://git-scm.com/download/mac。

在 Linux 上安装

如果你想在 Linux 上用二进制安装程序来安装 Git,可以使用发行版包含的基础软件包管理工具来安装。 如果以Centos 上为例,你可以使用 yum:

sudo yum install git

  

如果你在基于 Debian 的发行版上,请尝试用 apt-get:

sudo apt-get install git

  

环境准备

准备好一台linux机器,且进行环境初始化,主机名、配置yum源、安装基础软件包、关闭防火墙、同步系统时间等。

安装git

[root@chaogelinux ~]# yum install git -y

[root@chaogelinux ~]# git --version
git version 1.8.3.1

  

运行git前的配置

既然已经在系统上安装了 Git,你会想要做几件事来定制你的 Git 环境。 每台计算机上只需要配置一次,程序升级时会保留配置信息。 你可以在任何时候再次通过运行命令来修改它们。

Git 自带一个 git config 的工具来帮助设置控制 Git 外观和行为的配置变量。 这些变量存储在三个不同的位置:

这个用户指的是linux用户

三种环境参数

  • --system
  • --global
  • --local

  • /etc/gitconfig 文件: 包含系统上每一个用户及他们仓库的通用配置。 如果使用带有 --system 选项的 git config 时,它会从此文件读写配置变量。

  • ~/.gitconfig~/.config/git/config 文件:只针对当前用户。 可以传递 --global 选项让 Git 读写此文件。
  • 当前使用仓库的 Git 目录中的 config 文件(就是 .git/config):针对该仓库。 --local 当前仓库配置

用户信息配置

通常配置git,只需要配置好你是谁,你的邮箱,这样就知道是谁在提交代码了。

$ git config --global user.name "Only yu"
$ git config --global user.email "yc_uuu@163.com"
$ git config --global color.ui true

我们这里配置的是--global参数,因此是在用户家目录下,可以查看
[root@chaogelinux ~]# cat .gitconfig
[user]
    name = pyyu
    email = yc_uuu@163.com
[color]
    ui = true

  

Git配置相关命令

yum install git -y  安装git

git --version  查看git版本

git config --system --list 查看系统所有linux用户的通用配置,此命令检查/etc/gitconfig

git config --global --list 查看当前linux用户的配置,检查~/.gitconfig文件

git config --local --list 查看git目录中的仓库配置文件,.git/config文件

git config --global user.name "pyyu"  配置当前linux用户全局用户名,这台机器所有git仓库都会用这个配置

git config --global user.email "yc_uuu@163.com"  配置当前linux用户全局邮箱

git config --global color.ui true 配置git语法高亮显示

git config --list 列出git能找到的所有配置,从不同的文件中读取所有结果

git config user.name  列出git某一项配置

git help 获取git帮助

man git man手册

git help config 获取config命令的手册

  

Git工作流程

Git四个区域

使用git就是将本地文件(工作目录workspace)的文件,添加到暂存区(stage),然后提交到本地仓库(repository),最终可以协同开发,推送到远程仓库(remote)。

 

 

Git实践

git版本库,也叫做git仓库(repository),也就是一个文件夹。

这个目录的所有内容被git软件管理,所有的修改,删除,git都会跟踪记录,便于可以跟踪历史记录,以后可以还原文件。

三种场景需求:

1.把已有的项目代码,纳入git管理

cd mysite    mysite项目所在代码
git init        初始化git仓库

git init命令会创建一个.git隐藏子目录,这个目录包含初始化git仓库所有的核心文件。
此步仅仅是初始化,此时项目里的代码还没有被git跟踪,因此还需要git add对项目文件跟踪,然后git commit提交到本地仓库

  

想知道.git文件做了什么事,请看git原理 >Git 内部原理

2.新建一个项目,直接用git管理

cd 某个文件夹
git init mysite      此步会在当前路径创建mysite文件夹,mysite文件夹中包含了.git的初始化文件夹,所有配置

  

PS:git原理

.git这个目录中

[root@pyyuc ~/git_learning/mysite 11:08:19]#tree .git
.git
├── branches
├── config    这个项目独有的配置
├── description
├── HEAD    head文件指示目前被检出的分支
├── hooks  hooks目录包含服务端和客户端的钩子脚本 hook scripts
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── index  index文件保存暂存区的信息,只有git add之后才会生成,默认还没有这个文件
├── info    info目录是全局性排除文件,用于放置不想被记录在.gitignore文件中的忽略模式(ignored patterns)
│   └── exclude
├── objects  存储所有数据内容
│   ├── info
│   └── pack
└── refs  refs目录存储指向数据(分支)的提交对象的指针
    ├── heads
    └── tags

.git文件夹解析

  

3.获取远程代码仓库代码

如果你想获取github上的代码,或者你公司gitlab私有仓库的代码,可以使用git clone命令,下载克隆远程仓库的代码。

git clone https://github.com/django/django.git
下载出来的代码已经是被git管理的本地仓库
你会发现所有的项目文件都在这里,等待后续开发

  

Git生命周期演练

git工作区

在我们进行git init mygit初始化一个git项目时,这个mygit文件夹,就是一个工作区(working Directory)

yudanL-2:mygit root# pwd
/data/mygit
yudanL-2:mygit root# ls
.git    my.txt

  

git仓库

工作区里有一个.git隐藏文件夹,就是git的本地仓库

.git文件夹里有一个index文件,就是git的暂存区,也叫做stage

.git文件夹里的HEAD文件就是git的一个指针

原理图

 

 

git init mysite                          初始化git仓库

git status                                 查看git状态

echo 'print("挣了一个亿")' > main.py        新建一个代码文件,此时是未被git跟踪的

git status                                查看状态

    On branch master

    No commits yet

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

        main.py

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

git add main.py  开始跟踪main.py文件

git status   此时再看已经被跟踪,现在已是可以被提交的状态,此时处于暂存区

git commit -m "echo main.py"  告诉git,把暂存区的main.py提交到本地仓库

git log     查看刚才的commit记录

  

检查git文件状态

git status 
此命令查看git工作目录的文件,处于生命周期的哪一个状态 注意,只能在git工作目录中输入这个命令,他会去找.git文件夹 
第一次输入git status会看到此状态,没有任何东西需要提交
[root@pyyuc ~/git_learning/mysite 12:00:34]#git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

说明当前工作目录很干净,所有的已跟踪文件,已经被提交且未更改。
此时处在master默认分支。

  

给文件重命名

有同学会说,这也太简单了,mv不就得了吗

我们还是在git版本库中操作
修改main.py为mymain.py
mv main.py  mymain.py
查看状态
git status直接mv的操作,会被git记录为两个形容,一、删除原有文件、二、新建了mymain.py文件此时新文件还未被跟踪,需要git add , git commit原本的main.py还需要从暂存区删除
[root@pyyuc ~/mysite 14:57:57]#git status
# On branch master
# 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)
#
#    deleted:    main.py
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    mymain.py
no changes added to commit (use "git add" and/or "git commit -a")git rm main.py  删除暂存区的main.pygit commit -m "mv mymain.py"  提交新的mymain.py

  

这样的步骤很麻烦,可以直接git mv 命令即可

刚才的mv记录,可以通过git log查看历史记录,已经提交的id

可以通过git reset 回退历史版本,回退到改名之前

[root@pyyuc ~/mysite 15:10:12]#git log
commit f60fa7f1312843aa57edc9464192c9d891f23fb5
Author: pyyu <yc_uuu@163.com>
Date:   Sat Dec 22 15:08:02 2018 +0800

    mv mymain.py

commit 65e0a2239909fd5aabc5928ec4431de3f163a195
Author: pyyu <yc_uuu@163.com>
Date:   Sat Dec 22 14:51:07 2018 +0800

    echo main.py


回退到上一次commit版本,(注意这个命令,很危险,慎用)
git reset --hard 65e0a2239909fd5aabc5928ec4431de3f163a195 
--hard 清空暂存区和工作目录资料
文件改名最正确的姿势

git mv main.py mymain.py  

git commit -m "mv mymain.py"

  

git版本历史

在我们使用git的时候,会对代码文件不停的修改,不断的提交到代码仓库。

这个就如同我们打游戏时候,保存关卡记录的操作。

 

 

 

在打boss之前,先做一个存档,防止你这个渣渣,被boss一招秒杀,又得从头再来。。。。。

因此被boss弄死,可以从存档,重新开始游戏。。。。

git log

当你的代码写好了一部分功能,就可以保存一个"存档",这个存档操作就是git commit,如果代码出错,可以随时回到"存档"记录

查看"存档"记录,查看commit提交记录的命令 git log

我们可以吧git commit操作与虚拟机的快照做对比理解,简单理解就是每次commit,就等于我们对代码仓库做了一个快照。

可以演示下vmware快照

那么我们如何知道文件快照了多少次呢?

git log命令显示,从最新的commit记录到最远的记录顺序。

git log --oneline    一行显示git记录
git log --oneline  --all  一行显示所有分支git记录
git log --oneline --all -4 --graph 显示所有分支的版本演进的最近4条
git log -4  显示最近4条记录
git log --all     显示所有分支的commit信息




git branch -v 查看分支信息
git help --web log 以web界面显示log的参数

  

git文件对比

我们先记住如下几个命令的作用:

  • git diff:
    • 当工作区有改动,暂存区为空,diff比较的是工作区最后一次commit提交的仓库的共同文件
    • 当工作区有改动,暂存区不为空,diff比较的是工作区暂存区的共同文件
  • git diff --cached或git diff --staged
    • 显示暂存区(已add但未commit的文件)和最后一次commit之间所有的不相同的文件,增删改信息。
  • git diff HEAD:
    • 显示工作区(已跟踪但未add的文件)和暂存区(已add但未commit的文件)和本地仓库(最后一次commit的文件)所有不相同文件的增删改。

命令实践

情况一:工作区,暂存区,都没有改动

# 情况一,工作区,暂存区,都没有改动
[yuchao@yumac ~/learn_git]$git status
On branch master
nothing to commit, working tree clean
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git diff

  

情况二:工作区有改动,暂存区为空(未git add的情况),比较的是工作区和本地仓库

# 情况二,工作区有改动,暂存区为空(未git add的情况),比较的是工作区和本地仓库
[yuchao@yumac ~/learn_git]$cat learn.txt
超哥带你学git
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$echo "超哥带你学git,好嗨哦" >> learn.txt
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   learn.txt

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


[yuchao@yumac ~/learn_git]$git diff
diff --git a/learn.txt b/learn.txt
index 46cf52a..8f433a7 100644
--- a/learn.txt
+++ b/learn.txt
@@ -1 +1,2 @@
 超哥带你学git
+超哥带你学git,好嗨哦

  

情况三:工作区有改动,暂存区不为空,比较的是工作区和本地仓库

# 情况三,工作区有改动,暂存区不为空,比较的是工作区和本地仓库
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git diff
# 看不到任何结果,是因为工作区和暂存区内容一样的
# 因此可以在本地再添加一些信息
[yuchao@yumac ~/learn_git]$echo "超哥带你学git,真的很嗨哦" >> learn.txt
[yuchao@yumac ~/learn_git]$git diff
diff --git a/learn.txt b/learn.txt
index 8f433a7..dc0a6ba 100644
--- a/learn.txt
+++ b/learn.txt
@@ -1,2 +1,3 @@
 超哥带你学git
 超哥带你学git,好嗨哦
+超哥带你学git,真的很嗨哦

# 添加工作区的内容放入,暂存区
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git diff
[yuchao@yumac ~/learn_git]$git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   learn.txt

# 提交暂存区的内容
[yuchao@yumac ~/learn_git]$git commit -m "second commit with modified learn.txt"
[master 688c0ba] second commit with modified learn.txt
 1 file changed, 2 insertions(+)
[yuchao@yumac ~/learn_git]$

[yuchao@yumac ~/learn_git]$git diff

  

情况四:刚才比较的都是本地工作区的文件区别,现在比较暂存区本地仓库的区别

# 案例1,当前暂存区为空
[yuchao@yumac ~/learn_git]$git status
On branch master
nothing to commit, working tree clean
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git diff --cached

# 暂存区有内容
[yuchao@yumac ~/learn_git]$echo "git diff很强大哦" >> learn.txt
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   learn.txt

[yuchao@yumac ~/learn_git]$

# 使用git diff查看git diff --cached区别
# 直接git diff没有结果,因为比较的是工作区和暂存区信息,是一样的,已经git add了
[yuchao@yumac ~/learn_git]$git diff  

# 使用git add --cached,发现区别,比较的是暂存区和本地仓库的区别
[yuchao@yumac ~/learn_git]$git diff  --cached
diff --git a/learn.txt b/learn.txt
index dc0a6ba..52ef4f1 100644
--- a/learn.txt
+++ b/learn.txt
@@ -1,3 +1,4 @@
 超哥带你学git
 超哥带你学git,好嗨哦
 超哥带你学git,真的很嗨哦
+git diff很强大哦

  

情况五:比较所有区域的区别

git diff head:

  • 显示工作区(已跟踪但未add的文件)和暂存区(已add但未commit的文件)和本地仓库(最后一次commit的文件)所有不相同文件的增删改。
# 所有区域没有内容变化,没有结果
[yuchao@yumac ~/learn_git]$git diff HEAD

# 工作区有内容变化,和本地仓库有文件差异
[yuchao@yumac ~/learn_git]$echo "git diff 最后一个示例了" >> learn.txt
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git diff head
diff --git a/learn.txt b/learn.txt
index 52ef4f1..a382ed8 100644
--- a/learn.txt
+++ b/learn.txt
@@ -2,3 +2,4 @@
 超哥带你学git,好嗨哦
 超哥带你学git,真的很嗨哦
 git diff很强大哦
+git diff 最后一个示例了

# 工作区,暂存区,本地仓库三个比较,仍有区别
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git diff head
diff --git a/learn.txt b/learn.txt
index 52ef4f1..a382ed8 100644
--- a/learn.txt
+++ b/learn.txt
@@ -2,3 +2,4 @@
 超哥带你学git,好嗨哦
 超哥带你学git,真的很嗨哦
 git diff很强大哦
+git diff 最后一个示例了

# 提交暂存区后,所有区域没有区别了
[yuchao@yumac ~/learn_git]$git commit -m "learn git diff end."
[master e670d84] learn git diff end.
 1 file changed, 1 insertion(+)
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git diff head
总结,看来git diff head能最大程度的检查出git区域间的文件一致。

  

git仓库文件管理生命周期

还记得git的四个区域吗?本地文件夹,暂存区,本地仓库,远程仓库吗?

本地文件夹未初始化,git是不认识的

本地文件git init后,就成了git仓库

请记住,在工作文件夹的每一个文件,只有两种状态,一个是未跟踪,一个是已跟踪

已跟踪的指的是已经被纳入git版本管理的文件,在git快照中有他的记录

未跟踪的是这个文件既不在git快照中,也不在暂存区

git init初始化时的工作文件夹,都属于已跟踪了,后续的编辑操作都会标记为,已修改文件,因此需要将修改后的文件,加入暂存区,然后提交暂存区的文件。

 

 

Git版本回退

学到这里,超哥想起来那年十八岁,听着周董的《回到过去》。。

 

 

我们已知git commit可以提交代码到本地仓库,如同虚拟机的快照,当也可以进行版本回退。

git log可以查看历史版本记录
git reset --hard命令可以回退版本
git reset --hard HEAD^ 回退到上个版本
HEAD表示当前版版本
HEAD^表示上个版本
HEAD^^上上个版本

也可以直接git reset --hard 版本id号

这个时候就发现,git commit -m 所标记的注释信息非常重要了吧

  

版本回退原理

[yuchao@yumac ~/learn_git]$git reset --hard 81f0c645e4f4ca3fa20876939b04aa0da7996624
HEAD is now at 81f0c64 my first commit with touch learn.txt

  

git版本指针

 

 

git穿梭未来

穿梭未来有点吓人,其实还是基于git log的恢复,如快照的恢复了。

当你发现你git reset回退版本选错了ID,怎么办?别怕

git reflog帮你记录你每一次执行的命令

git reflog
80f9496 HEAD@{1}: reset: moving to HEAD^
b7a8740 (HEAD -> master) HEAD@{2}: commit: echo 123
80f9496 HEAD@{3}: commit: echo my.txt
bf5879e HEAD@{4}: commit (initial): echo my.txt

  

我想回到某一个点,可以再次git reset --hard 版本id

git stash

保存当前暂存区和工作区的改动存储起来,执行完毕git stash之后,再次运行git status就会发现当前已是个干净的工作区,通过git stash list查看结果

命令整理

git stash 保存暂存区,工作区进度

git stash list 查看stash保存的列表以及id

git stash pop  恢复最新的stash进度到工作区

git stash pop stash_id  恢复指定的stash进度

git stash clear 清空所有存储的stash进度

git stash drop stash_id  删除一个存储的stash进度

  

git stash实际用法

git stash会把所有未提交的修改(已经git add 还未git commit)都保存起来,用于以后续恢复当前工作目录。 比如下面的中间状态,通过git stash命令推送一个新的储藏,当前的工作目录就干净了。

[yuchao@yumac ~/learn_git]$echo "练习stash" >>  learn_stash.txt
[yuchao@yumac ~/learn_git]$echo "练习stash22" >>  learn_stash22.txt
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   learn_stash.txt
    new file:   learn_stash22.txt

[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$ls
learn.txt        learn_stash.txt        learn_stash22.txt

# 如上有3个文件,已经被加入了暂存区
# 使用stash可以吧暂存区还未提交的内容,临时存储起来
[yuchao@yumac ~/learn_git]$git stash save "my two file named learn_stash.txt"
Saved working directory and index state On master: my two file named learn_stash.txt
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
# 发现只剩下一个文件了
[yuchao@yumac ~/learn_git]$ls
learn.txt

# 找回stash文件内容
# 查看储藏室列表
[yuchao@yumac ~/learn_git]$git stash list
stash@{0}: On master: my two file named learn_stash.txt

# 找回储藏室的文件内容
[yuchao@yumac ~/learn_git]$git stash pop
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   learn_stash.txt
    new file:   learn_stash22.txt

Dropped refs/stash@{0} (e5a0fb591a48d66753d7d8f43574529efd02af29)
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$ls
learn.txt        learn_stash.txt        learn_stash22.txt

  

Git分支

在前面我们基本了解Git的使用方法,这一节我们看下GIt重要概念【分支】

在开发软件的时候,可能有多人同时开发一个软件功能或者修复BUG。

假设超哥要开发一个同性在线交友的网站,这个写代码的工作进行分配,分给两个小弟进行功能开发,一个是武沛奇分支,一个是苑昊分支,他俩自己的分支别人看不到,当他俩代码写完后,合并到master主分支上,这样既保证主代码的安全,又能协同开发,互不影响。

 

 

git分支命令

1.查看分支,查看当前分支情况,在哪一个就有*符
[yuchao@yumac ~/learn_git]$git branch
* master

此例的意思就是,我们有一个叫做 master 的分支,并且该分支是当前分支。
当你执行 git init 的时候,默认情况下 Git 就会为你创建 master 分支。
如果我们要手动创建一个分支。执行 git branch (branchname) 即可。



2.创建分支
[yuchao@yumac ~/learn_git]$git branch onlyu
[yuchao@yumac ~/learn_git]$git branch
* master
  onlyu

现在我们可以看到,有了一个新分支 onlyu。
接下来我们将演示如何切换分支,我们用 git checkout (branch) 切换到我们要修改的分支。  


3.git分支练习
[yuchao@yumac ~/learn_git]$echo "超哥带你学git分支" > branch.txt
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git commit -m "add branch.txt"
[master 91618a9] add branch.txt
 1 file changed, 1 insertion(+)
 create mode 100644 branch.txt
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt

# 此时切换分支,Git会还原工作区的内容,到创建分支时的状态
[yuchao@yumac ~/learn_git]$git checkout onlyu
Switched to branch 'onlyu'
[yuchao@yumac ~/learn_git]$ls
learn.txt
# 切换回master,数据又回来了
[yuchao@yumac ~/learn_git]$git checkout master
Switched to branch 'master'
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt

4.我们也可以直接创建且切换分支
[yuchao@yumac ~/learn_git]$git checkout -b yuchao
Switched to a new branch 'yuchao'
[yuchao@yumac ~/learn_git]$git branch
  master
  onlyu
* yuchao
[yuchao@yumac ~/learn_git]$echo "我是分支yuchao,你好" >> yuchao.txt
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git commit -m "add yuchao.txt"
[yuchao 1304234] add yuchao.txt
 1 file changed, 1 insertion(+)
 create mode 100644 yuchao.txt
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt    yuchao.txt

# 如缩减,当我们回退到主分支的时候,新创建的文件已然消失了
# 使用分支,实现了在不同的分支环境下工作,且可以来回切换
[yuchao@yumac ~/learn_git]$git checkout master
Switched to branch 'master'
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt

5.删除分支,删除分支后,随之分支管理的文件内容也被删了
[yuchao@yumac ~/learn_git]$git branch -d onlyu
Deleted branch onlyu (was e670d84).
[yuchao@yumac ~/learn_git]$git branch
* master
  yuchao


6.一旦分支有了独立的内容,你最终目的是,将分支独立开发的内容合并到主干分支。
# 主分支下内容
[yuchao@yumac ~/learn_git]$git branch
* master
  onlyu
  yuchao
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt

# yuchao分支下的内容
[yuchao@yumac ~/learn_git]$git checkout yuchao
Switched to branch 'yuchao'
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt    yuchao.txt


# 合并yuchao分支的文件内容到主干master
# 回退到master分支,然后执行命令
[yuchao@yumac ~/learn_git]$git checkout master
Switched to branch 'master'
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt
[yuchao@yumac ~/learn_git]$git merge yuchao
Updating 91618a9..1304234
Fast-forward
 yuchao.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 yuchao.txt
 # 此时发现已经在master主干分支,新增了一个文件
 [yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt    yuchao.txt

合并完之后,就可以删除该分支了,下次需要用分支开发,可以再次创建即可
[yuchao@yumac ~/learn_git]$git branch -d yuchao
Deleted branch yuchao (was 1304234).

  

git合并分支冲突

合并不仅仅是简单的对文件添加、移除,git可以合并且修改。

我们准备数据

1.准备开始进行开发了,新建一个分支
[yuchao@yumac ~/learn_git]$git branch chaoge
[yuchao@yumac ~/learn_git]$git checkout chaoge
Switched to branch 'chaoge'
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt    yuchao.txt
[yuchao@yumac ~/learn_git]$vim chaoge.php
[yuchao@yumac ~/learn_git]$head -3 chaoge.php
<?php
echo "超哥带你学git合并冲突如何解决";
?>

2.将创建的文件内容,提交到chaoge分支
[yuchao@yumac ~/learn_git]$git commit -m "add chaoge.php"
[chaoge 3f6cccf] add chaoge.php
 1 file changed, 3 insertions(+)
 create mode 100644 chaoge.php

 3.此时我们切换回master分支,chaoge.php文件应该是不存在的
 [yuchao@yumac ~/learn_git]$git checkout master
Switched to branch 'master'
[yuchao@yumac ~/learn_git]$ls
branch.txt    learn.txt    yuchao.txt

4.我们在master分支下,也创建名为chaoge.php的文件,且写入内容
[yuchao@yumac ~/learn_git]$git checkout master
[yuchao@yumac ~/learn_git]$head -3 chaoge.php
<?php
echo "我是冲突,嘿嘿嘿";
?>
# 提交到本地仓库
[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git commit -m "add chaoge.php  by master"
[master 4d74537] add chaoge.php  by master
 1 file changed, 3 insertions(+)
 create mode 100644 chaoge.php

 5.此时你会发现,chaoge.php文件在master分支和chaoge分支,都有自己的代码内容,是不是?
 如果此时你合并分支,会发现如下问题
 [yuchao@yumac ~/learn_git]$git merge chaoge
CONFLICT (add/add): Merge conflict in chaoge.php
Auto-merging chaoge.php
Automatic merge failed; fix conflicts and then commit the result.
发现了git给与的提示是,自动合并失败了,需要你自己手动解决冲突,然后提交结果

6.查看文件内容,文件被git自动修改了
第二行发生了文件内容冲突
master主分支上内容是 echo "我是冲突,嘿嘿嘿"
chaoge分支合并过来的内容是 echo "超哥带你学git合并冲突如何解决"
此时根据你自己的需求,决定如何修改代码文件,再次提交即可
[yuchao@yumac ~/learn_git]$cat -n  chaoge.php
     1    <?php
     2    <<<<<<< HEAD
     3    echo "我是冲突,嘿嘿嘿";
     4    =======
     5    echo "超哥带你学git合并冲突如何解决";
     6    >>>>>>> chaoge
     7    ?>

7.自己决定对代码修改
[yuchao@yumac ~/learn_git]$git diff
diff --cc chaoge.php
index b4689a3,ae89ca8..0000000
--- a/chaoge.php
+++ b/chaoge.php
@@@ -1,3 -1,3 +1,7 @@@
  <?php
++<<<<<<< HEAD
 +echo "我是冲突,嘿嘿嘿";
++=======
+ echo "超哥带你学git合并冲突如何解决";
++>>>>>>> chaoge
  ?>

# 手动修改代码为确定版本,比如两个代码我都要保留
[yuchao@yumac ~/learn_git]$cat chaoge.php
<?php
echo "我是冲突,嘿嘿嘿";
echo "超哥带你学git合并冲突如何解决";
?>

[yuchao@yumac ~/learn_git]$git add .
[yuchao@yumac ~/learn_git]$git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
    modified:   chaoge.php

[yuchao@yumac ~/learn_git]$git commit -m "merge chaoge with change chaoge.php"
[master 4ba1c7d] merge chaoge with change chaoge.php

现在我们就解决了合并中的冲突,然后提交了结果

  

Git标签

git标签有什么用

Git仓库内的数据发生变化时,我们经常会打上一个类似于软件版本的标签tag,这样通过标签就可以把版本库中的某个版本给记录下来,便于以后我们可以将特定的数据取出来。

标签就是版本库的一个快照。

为啥用git标签

git不是已经有commit了吗,可以附加提交信息,为什么还要tag呢?

开发小王:请吧上周一发布的版本打包发布,commit_id是3f6cccf0708525b58fe191c80325b73a54adee99

运维小于:你这什么乱七八糟的数字,,,太难找了,请你换个方法

开发小王换了个方式:请吧上周一发布的版本打包,版本号是v1.2,按照tag v1.2查找commit 记录就行了!

所以tag就是一个容易记住的名字,和某个commit记录绑定在一起。

git标签使用

对当前提交的代码创建标签,-a标签名称,-m标签描述。

1.# 对当前最新的commit记录进行标签
[yuchao@yumac ~/learn_git]$git tag -a "v1.0" -m "修复了支付bug"

2.# 直接输入,则查看当前标签
[yuchao@yumac ~/learn_git]$git tag
v1.0

3.# 指定commitID创建标签,选中commit id 前7位就够了
git tag -a v1 1304234 -m "添加了yuchao.txt"

4.# 查看标签列表
[yuchao@yumac ~/learn_git]$git tag
v1
v1.0

5.# 查看标签里有什么git show
[yuchao@yumac ~/learn_git]$git show v1
tag v1
Tagger: yc <yc_uuu@163.com>
Date:   Tue Jul 7 15:26:15 2020 +0800

添加了yuchao.txt

commit 1304234d1240469587a7d448ba3f8e0c09f8a340 (tag: v1)
Author: yc <yc_uuu@163.com>
Date:   Tue Jul 7 11:37:20 2020 +0800

    add yuchao.txt

diff --git a/yuchao.txt b/yuchao.txt
new file mode 100644
index 0000000..9b91134
--- /dev/null
+++ b/yuchao.txt
@@ -0,0 +1 @@
+我是分支yuchao,你好

6.# 通过git log查看tag
# --graph:显示ASCII图形表示的分支合并历史
# —pretty=:使用其他格式显示历史提交信息
# -abbrev-commit:仅显示SHA-1的前几个字符,而非所有的40个字符 
[yuchao@yumac ~/learn_git]$git log --oneline --decorate --graph
*   4ba1c7d (HEAD -> master, tag: v1.0) merge chaoge with change chaoge.php
|\
| * 3f6cccf (chaoge) add chaoge.php
* | 4d74537 add chaoge.php  by master
|/
* 1304234 (tag: v1) add yuchao.txt
* 91618a9 add branch.txt
* e670d84 learn git diff end.
* eeef559 modify learn.txt
* 688c0ba second commit with modified learn.txt
* 81f0c64 my first commit with touch learn.txt

7.删除tag
[yuchao@yumac ~/learn_git]$git tag -d v1
Deleted tag 'v1' (was e2118b3)
[yuchao@yumac ~/learn_git]$git tag
v1.0

  

 

 


posted @ 2021-01-31 05:30  时间的侵蚀  阅读(93)  评论(0编辑  收藏  举报