应用场景
本地工作区将代码通过git add上传到本地暂存区,然后提交到本地Git仓库,在推送到Git分布式版本管理仓库。
开发人员:
修改完代码上传到Git远程本地仓库,git的大型仓库(gitlab)也叫私有仓库,带有web界面的仓库叫gitlab,git本身有个简易仓库,Jenkins原来是从SVN拉取代码,现在是从gitlab拉取代码,拉取代码的过程,叫做持续集成,持续构建(CI)持续部署,持续交换(CD),那么maven是CI,Jenkins也是CI,项目发布是CD的顺至如果加一个第三方代码质量管理,就有一个软件集群在Jenkins上,就可以自动检测代码的完整性。
测试人员
然后交给测试人员进行集成开发测试,然后单元测试,有一个压力测试软件(jemeter),无论是黑盒还是白盒,或者一些测试软件,测试成功以后就发布,只有发布的时候叫CD,单元测试在部署的时候也有可能是CD的过程,别的都叫持续集成测试,持续集成构建。

其实这个过程就是gitlab代替了SVN

java的打包过程
java是一种容器运行的,它本身直接把一个java开发的源代码是直接运行不了的,它需要很多支持包,依赖包一起打包,叫.war或.jar

war:java是后端语言,前端语言加上后端语言,加上web页面在一起打个包叫war
jar:没有网页端的代码就叫jar(可以这么理解,只有学了开发的时候你才会理解)

maven:可以看成yum仓库,没有maven的话,你的依赖包只能一个一个下载,有了maven它会自动下载依赖包,maven自动打成一个包,交给Jenkins,maven专门打包的,这个只是java需要的,php是不需要的,maven里面有一个配置文件,通过配置文件(pom.xml)打包,这个配置文件不是运维写的,开发人员在上传代码的时候,同时写好一个pom.xml,一起上传到gitlab仓库,maven会读取开发上传pom.xml,是maven需要打包的配置文件,然后maven可以打包了

Git介绍

Git在Wikipedia上的定义:它是一个免费的、分布式的版本控制工具,与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持(wingeddevil注:这得分是用什么样的服务端,使用http协议或者git协议等不太一样。并且在push和pull的时候和服务器端还是有交互的。),使源代码的发布和交流极其方便。 Git 的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的合并跟踪(merge tracing)能力。

分布式版本控制介绍

本地版本控制

本地的文件上传到本地仓库里,本地帮我记录版本

许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会改名加上备份时间以示区别。这么做唯一的好处就是简单。不过坏处也不少:有时候会混淆所在的工作目录,一旦弄错文件丢了数据就没法撤销恢复。

集中化的版本控制


多个客户端可以集中朝着一个版本仓库提交,以及下载代码,一但集中化版本仓库宕了,每个开发就没法弄了。

接下来人们又遇到一个问题,如何让在不同系统上的开发者协同工作?于是,集中化的版本控制系统( Centralized Version Control Systems,简称 CVCS )应运而生。这类系统,诸如 CVS,Subversion 以及 Perforce 等,都有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人们都通过客户端连到这台服务器,取出最新的文件或者提交更新。多年以来,这已成为版本控制系统的标准做法

分布式版本控制


它是集成了远程仓库和本地仓库的结合体,本地的文件A和B把文件里面的代码记录在本地仓库,然后本地仓库记录推到远程仓库。(先推本地,在到远程),就算没有远程仓库,俩个开发的本地仓库也可以互相推送(相当于A文件的远程仓库就是B文件的远程仓库)。

于是分布式版本控制系统( Distributed Version Control System,简称 DVCS )面世了。在这类系统中,像 Git,Mercurial,Bazaar 以及 Darcs 等,客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来。这么一来,任何一处协同工作用的服务器发生故障,事后都可以用任何一个镜像出来的本地仓库恢复。因为每一次的提取操作,实际上都是一次对代码仓库的完整备份


左边Git
主干代码(master),一但是主版本更新了,所有用户都更新了,为了不影响主版本,在开发功能开启分支,也就是说master复制一份,然后在分支代码,一但功能没问题然后在合并主代码,如果覆盖的时候文件有冲突的时候,会有提示的,然后人工介入。

右边SVN

中间是主干,复制主干一份,完全复制,然后在开发下去,到合并的时候,分支里面的代码合并到主版本里,也就说是覆盖的,多分支覆盖会出问题的,新分支的在覆盖到原来的会冲突的。

开发测试环境

您Jenkins拉取的什么代码
拉取的是测试分支代码
一般有三分
测试分支(test),分支dev,master分支

线上环境

也有一个Jenkins
拉取的是master分支代码

公有仓库

可以注册GitHub账号

Git的安装
Windows客户端的地址: https://git-scm.com/downloads


部署git客户端

#安装环境查看
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# uname -r
3.10.0-862.el7.x86_64
#安装git客户端
[root@localhost ~]# which git
/usr/bin/git
[root@localhost ~]# git --version
git version 1.8.3.1
#Git全局配置
[root@localhost ~]# git config --global user.name "zxw"      #配置git使用用户
[root@localhost ~]# git config --global user.email "2420813702@qq.com"   #配置git使用邮箱
[root@localhost ~]# git config --global color.ui true  #语法有颜色 高亮
[root@localhost ~]# git config --global --list  #语法高亮
user.name=zxw
user.email=2420813702@qq.com
color.ui=true
注意
如果没有提前设置Git的全局配置,那么在第一次进行代码提交的时候,会要求输入使用者的邮箱和姓名
#源码安装git-2.9.5.tar.gz
[root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@localhost ~]# yum -y install gcc perl-ExtUtils-MakeMaker
[root@localhost git-2.9.5]# tar xf git-2.9.5.tar.gz -C /usr/src/
[root@localhost git-2.9.5]# cd /usr/src/git-2.9.5/
[root@localhost git-2.9.5]# ./configure --prefix=/usr/local/git
[root@localhost git-2.9.5]# make && make install
[root@localhost bin]# ln -sf  /usr/local/git/bin/* /usr/bin/
[root@localhost ~]# which git
/usr/bin/git
#源码编译需要链接git的命令库
[root@localhost ~]# ln -s /usr/libexec/git-core/* /usr/bin/
[root@localhost ~]# git --version
git version 2.9.5
至此,利用源码包安装linux操作系统的git客户端就安装好了。

Git的命令入门

  • 工作区
    你建立的本地git项目目录(代码放在了工作区)
  • 暂存区 (add)
    将工作区里的变更部分(与上一版本不同的部分)暂时存储起来的地方 (git add)
  • 本地仓库
    在本地创建的git版本仓库,用于提交工作区的变更。 (git commit)
  • 远程仓库
    githab,gitlab或者队友机器上所建立的一个仓库 (git push)

git帮助文档

当前配置文件中文生效
[root@localhost ~]# export LANG=zh_CN.UTF8
[root@localhost ~]# git
usage: git [--version] [--help] [-C <path>] [-c name=value][--exec-path[=<path>]] [--html-path] [--man-path] [--info-path][-p | --paginate | --no-pager] [--no-replace-objects] [--bare][--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]<command> [<args>]这些是各种场合常见的 Git 命令:开始一个工作区(参见:git help tutorial)clone      克隆一个仓库到一个新目录init       创建一个空的 Git 仓库或重新初始化一个已存在的仓库在当前变更上工作(参见:git help everyday)add        添加文件内容至索引mv         移动或重命名一个文件、目录或符号链接reset      重置当前 HEAD 到指定状态rm         从工作区和索引中删除文件检查历史和状态(参见:git help revisions)bisect     通过二分查找定位引入 bug 的提交grep       输出和模式匹配的行log        显示提交日志show       显示各种类型的对象status     显示工作区状态扩展、标记和调校您的历史记录branch     列出、创建或删除分支checkout   切换分支或恢复工作区文件commit     记录变更到仓库diff       显示提交之间、提交和工作区之间等的差异merge      合并两个或更多开发历史rebase     在另一个分支上重新应用提交tag        创建、列出、删除或校验一个 GPG 签名的标签对象协同(参见:git help workflows)fetch      从另外一个仓库下载对象和引用pull       获取并整合另外的仓库或一个本地分支push       更新远程引用和相关的对象命令 'git help -a' 和 'git help -g' 显示可用的子命令和一些概念帮助。
查看 'git help <命令>' 或 'git help <概念>' 以获取给定子命令或概念的
帮助。

Git init初始化GIT工作目录

[root@localhost ~]# mkdir -p /mycode
[root@localhost /]# cd mycode/
[root@localhost mycode]# git init
初始化空的 Git 仓库于 /mycode/.git/
[root@localhost mycode]# ll -a
总用量 0
drwxr-xr-x.  3 root root  18 12月 20 01:00 .
dr-xr-xr-x. 18 root root 238 12月 20 01:00 ..
drwxr-xr-x.  7 root root 119 12月 20 01:00 .git

git add 将变更添加进入暂存区

#在linux上
[root@localhost mycode]# touch test.txt
[root@localhost mycode]# git add test.txt
[root@localhost mycode]# git status
位于分支 master初始提交要提交的变更:(使用 "git rm --cached <文件>..." 以取消暂存)新文件:   test.txt           #有一个新文件被添加到了暂存区,没有提交到本地仓库

git commit将变更从暂存区提交到本地仓库

[root@Git01 mycode]# git commit -m "test"*** Please tell me who you are.         #请告诉我你是谁Run #运行git config --global user.email "you@example.com"  #运行命令告诉git你的邮箱
git config --global user.name "Your Name"         #运行命令告诉git你的名字to set your accounts default identity.
Omit --global to set the identity only in this repository.fatal: unable to auto-detect email address (got 'root@Git.(none)')说明:
因为我们安装git时,并没有注册自己的个人信息。但是当变更的代码初次提交到本地仓库上时是需要注册你的个人信息的。因此,我们执行这两条命令注册自己的个人信息
#注册自己的个人信息
[root@Git01 mycode]# git config --global user.email "2420813702@qq.com"
[root@Git01 mycode]# git config --global user.name "zxw"#再次尝试提交
[root@Git01 mycode]# git commit -m "test"
[master(根提交) 6bff4b4] test
1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 test.txt

创建GitHub账号,并创建个人GitHub远程仓库

修改全局账号和密码

今天刚刚入门了下git,,然后初始化的时候将用户名弄错了。。。就很气啊。然后网上找了半天都找不出一个可靠的修改全局用户名和邮箱的方法。。

最后还是自己摸索出来了。。其实也很简单。

首先进入git bash

0:

输入
$ git config --list

可以查看配置的一些东西。可以看到user.name 和user.email 分别是什么。。
如果你没有初始化过。那么直接:
$ git config --global user.name “输入你的用户名”
$ git config --global user.email “输入你的邮箱”

这样就可以初始化了。

1:

如果你已经初始化过了,但是不小心把邮箱和用户名输错了,那么就要修改了。
我看到网上有人说继续$ git config --global user.name "输入你的用户名"或者 $ git config --global user.email “输入你的邮箱” 来修改邮箱和密码。我尝试了一下,是不行的(至少在window10的环境下)会给出这样的错误:
warning: user.name has multiple values
error: cannot overwrite multiple values with a single value
Use a regexp, --add or --replace-all to change user.name.

这边给出了–repalce-all 这个东西。

然后我尝试着用
$ git config --global --replace-all user.email “输入你的邮箱”
$ git config --global --replace-all user.name “输入你的用户名”

然后再查看下
$ git config --list
发现修改成功了。

2:

再说说git bash和git cmd的区别啊。。简单一句话,,git cmd是git bash的子集。所以直接用git bash就行了。 然后git gui是图形界面。

以下内容来自网络:

Git

版本控制工具,支持该工具的网站有Github、BitBucket、Gitorious、国内的osChina仓库、csdn仓库等等。

shell

是Linux、unix系统的外壳,也可以理解为命令行,就是你输入并执行命令的地方,git通过命令行和图形界面两种方式使用shell。

bash

是shell的一种,最常用的shell之一。

git Bash

方便你在windows下使用git命令的模拟终端(windows自带的cmd功能太弱)linux、unix可以直接使用git。

git shell

它是安装了git的shell,bash是一种shell。

注册GitHub账号,以及创建仓库

第一步
输入网址,然后帮你的账号名字和邮箱以及你随意填写密码,然后进入到第二步

第二步

这里的用户名一定是你刚刚那会写的账号和邮箱要不然会报错,密码一定要有复杂性,简易的密码是行不通的

第三步:直接默认点击就行


第四步


第五步:登录界面

第六步


第七步:创建仓库

如果这里没有的话,说明你的代码只有提交到了本地仓库,并没有吧代码提交到远程仓库,remote(远程的意思)

这个是默认代号

开发分支是DEV,测试分支test

git remote 用于管理远程仓库

[root@Git mycode]# git remote add --help
用法:git remote add [<选项>] <名称> <地址>-f, --fetch           抓取远程的分支
--tags                抓取时导入所有的标签和关联对象
-t, --track <分支>    跟踪的分支-m, --master <分支>   主线分支--mirror[=<push|fetch>]
把远程设置为用以推送或抓取的镜像

git remote add

添加一个远程仓库的URL
命令格式:git remote add <仓库的名字> <仓库的URL>

####在linux上
[root@localhost mycode]# git remote -v
[root@localhost mycode]# git remote add zxw https://github.com/zhuxinwang01/Git166.git

git push将本地仓库的变更推送到远程仓库的某个分支

命令格式:
git push -u <远程仓库的名字> <远程仓库的某一分支名字>

#在Linux上推送本地仓库变更到远程仓库的master分支
[root@Git01 mycode]# git push -u test master
fatal: Unable to find remote helper for 'https'
说明:
这是因为我们在源码编译的时候,git功能没有安装全。
环境变量PATH找不到/usr/libexec/git-core目录
#我们添加一个软连接
[root@Git01 mycode]# ln -s /usr/libexec/git-core/* /usr/bin/#再次推送本地仓库的变更
[root@Git01 mycode]# git push -u test master
Username for 'https://github.com': 2420813702@qq.com #第一次推送需要输入登陆账号和密码
Password for 'https://2420813702@qq.com@github.com':
对象计数中: 3, 完成.
写入对象中: 100% (3/3), 201 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote:      https://github.com/yinsendemogui/yunjisuan/pull/new/master
remote:
To https://github.com/yinsendemogui/yunjisuan.git* [new branch]      master -> master
分支 master 设置为跟踪来自 test 的远程分支 master。

再次在浏览器进行访问查看你的github地址

git clone克隆一个现有仓库到本地

[root@localhost ~]# yum install -y git
[root@localhost ~]# mkdir -p /mycode2
[root@localhost ~]# cd /mycode2/#初始化GIT工作目录并将己有远程仓库克隆到本地
[root@localhost mycode2]# git init
Initialized empty Git repository in /mycode2/.git/
[root@localhost mycode2]# git clone https://github.com/zhuxinwang01/Git166.git
Cloning into 'Git166'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@localhost mycode2]# tree
.
└── Git166└── test.txt1 directory, 1 file#修改仓库里的文件内容,并提交变更到本地,推送变更到远程仓库
[root@localhost mycode2]# echo "welcome" >> Git166/test.txt
[root@localhost mycode2]# cd Git166/#将变更加入缓存区
[root@localhost Git166]# git add test.txt#将缓存区的变更提交到本地仓库
[root@Git02 yunjisuan]# git commit -m "修改了test.txt"
[master 40f3e3d] 修改了test.txt1 file changed, 2 insertions(+)#由于我们并没有添加对远程仓库的管理,所以我们要直接推送仓库的URL
[root@localhost Git166]# git push https://github.com/zhuxinwang01/Git166.git
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:git config --global push.default matching
To squelch this message and adopt the new behavior now, use:git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)Username for 'https://github.com': 2420813702@qq.com
Password for 'https://2420813702@qq.com@github.com':
Counting objects: 5, done.
Writing objects: 100% (3/3), 265 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/zhuxinwang01/Git166.git8b12081..7f8dfd3  master -> master#添加对远程仓库的管理
[root@localhost Git166]# git remote add zxw https://github.com/zhuxinwang01/Git166.git#查看所有已经纳入git管理的远程仓库URL
[root@localhost Git166]# git remote -v
origin  https://github.com/zhuxinwang01/Git166.git (fetch)
origin  https://github.com/zhuxinwang01/Git166.git (push)
zxw https://github.com/zhuxinwang01/Git166.git (fetch)
zxw https://github.com/zhuxinwang01/Git166.git (push)#再次改动test.txt文件并将变更提交到远程github仓库
[root@localhost Git166]# echo "www" >> test.txt
[root@localhost Git166]# git add test.txt
[root@localhost Git166]# git commit -m "再次改动一次test.txt"
[master a02fcb7] 再次改动一次test.txt1 file changed, 1 insertion(+)
[root@localhost Git166]# git push zxw master
Username for 'https://github.com': 2420813702@qq.com
Password for 'https://2420813702@qq.com@github.com':
Counting objects: 5, done.
Writing objects: 100% (3/3), 284 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/zhuxinwang01/Git166.git7f8dfd3..a02fcb7  master -> master

浏览器打开GitHub查看变更提交情况

git fetch 将远程仓库的变更拉取到本地仓库

[root@localhost mycode]# git fetch
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/zhuxinwang01/Git1668b12081..a02fcb7  master     -> zxw/master#查看文件是否修改
[root@localhost mycode]# cat test.txt   #没有被修改说明:
应用git fetch拉取到本地仓库时,并不修改本地工作目录中的代码
如果要进行修改,那么需要进行git merge变更合并

get checkout检查工作目录代码与本地仓库中的代码的差异

[root@localhost mycode]# git checkout
Your branch is behind 'zxw/master' by 2 commits, and can be fast-forwarded.(use "git pull" to update your local branch)
# 你有俩个提交的

git merge 将远程仓库的变更,更新到本地工作目录中

[root@localhost mycode]# git merge zxw/master
Updating 8b12081..a02fcb7
Fast-forwardtest.txt | 2 ++1 file changed, 2 insertions(+)[root@localhost mycode]# cat test.txt
welcome
www

git pull 将远程仓库的变更拉取到本地仓库,并更新本地工作目录。

git pull ====> git fetch + git merge

[root@localhost mycode]# ls
test.txt[root@localhost mycode]# echo "welcome yo yunjisuan" >> test.txt [root@localhost mycode]# cat test.txt
welcome
www
welcome yo yunjisuan[root@localhost mycode]# git add *[root@localhost mycode]# git commit -m "git服务端修改了test.txt"
[master 5ef619a] git服务端修改了test.txt1 file changed, 1 insertion(+)[root@localhost mycode]# git push -u zxw master
Username for 'https://github.com': 2420813702@qq.com
Password for 'https://2420813702@qq.com@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 297 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/zhuxinwang01/Git166.gita02fcb7..5ef619a  master -> master
Branch master set up to track remote branch master from zxw.#在git_agent上,拉取远程仓库的变更后直接合并进本地仓库的master分支
[root@localhost Git166]# git pull zxw master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/zhuxinwang01/Git166* branch            master     -> FETCH_HEAD
Updating a02fcb7..5ef619a
Fast-forwardtest.txt | 1 +1 file changed, 1 insertion(+)
[root@localhost Git166]# cat test.txt
welcome
www
welcome yo yunjisuan

git mv && git reset 暂存区文件的修改和撤销

#如果文件还没有被添加到暂存区,那么linux命令直接改名即可
[root@localhost mycode]# git status
On branch master
Your branch is up-to-date with 'zxw/master'.
nothing to commit, working tree clean[root@localhost mycode]# git status
On branch master
Your branch is up-to-date with 'zxw/master'.
Untracked files:(use "git add <file>..." to include in what will be committed)benet.txt         #还未提交到暂存区,但是git已经判断出你的变化文件了。nothing added to commit but untracked files present (use "git add" to track)#提交为空,但是存在尚未跟踪的文件(使用“git add” 建立跟踪)
#假如变动文件已经添加到了暂存区
[root@localhost mycode]# git add *
[root@localhost mycode]# git status
On branch master
Your branch is up-to-date with 'zxw/master'.
Changes to be committed:(use "git reset HEAD <file>..." to unstage)new file:   benet.txt            #变动文件已经添加到了暂存区#通过git mv 来给自己添加到暂存区的文件改名
[root@localhost mycode]# git mv benet.txt yunjisuan.txt
[root@localhost mycode]# git status
On branch master
Your branch is up-to-date with 'zxw/master'.
Changes to be committed:(use "git reset HEAD <file>..." to unstage)new file:   yunjisuan.txt[root@localhost mycode]# ls
test.txt  yunjisuan.txt         #我们发现本地文件同时改名了#### 通过git reset 来给已经添加到暂存区的文件撤销掉
[root@localhost mycode]# git reset yunjisuan.txt[root@localhost mycode]# git status
On branch master
Your branch is up-to-date with 'zxw/master'.
Changes to be committed:(use "git reset HEAD <file>..." to unstage)new file:   yunjisuan.txt

git rm 提交文件的删除变更到暂存区

git add 可以提交新增文件,修改文件的变更到暂存区;
git rm 则是提交删除文件的变更到暂存区

[root@localhost mycode]# touch {1..10}
[root@localhost mycode]# ls
1  10  2  3  4  5  6  7  8  9  test.txt  yunjisuan.txt[root@localhost mycode]# git rm -f  1 2 3 4
rm '1'
rm '2'
rm '3'
rm '4'[root@localhost mycode]# git commit -m "delte 1 2 3 4"
[master 013533b] delte 1 2 3 44 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 1create mode 100644 2create mode 100644 3create mode 100644 4

git diff 文件对比利器

git diff命令可以将本地工作目录中的文件与本地仓库中的文件进行对比

[root@localhost mycode]# git diff yunjisuan.txt
[root@localhost mycode]# echo "welcome" >> yunjisuan.txt
[root@localhost mycode]# echo "welcome" >> yunjisuan.txt
[root@localhost mycode]# git diff yunjisuan.txt
diff --git a/yunjisuan.txt b/yunjisuan.txt
index e69de29..6aa5347 100644
--- a/yunjisuan.txt
+++ b/yunjisuan.txt
@@ -0,0 +1,2 @@
+welcome           #工作目录文件与本地仓库对比,多了此行内容
+welcome           #工作目录文件与本地仓库对比,多了此行内容

git log 查看git提交历史记录

git log:查看提交历史记录

  • git log -2 :查看最近几条记录
  • git log -p -1 : -p 显示每次提交的内容差异
  • git log --start -2 : stat简要显示数据增改行数,这样就能看到提交中修改过的内容
  • git log --pretty=oneline:一行显示提交的历史记录
[root@localhost mycode]# git log -2
commit 013533b88d0e8db294f09baa0ab73486d948d789
Author: zxw <2420813702@qq.com>
Date:   Thu Dec 20 18:14:36 2018 +0800delte 1 2 3 4commit d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06
Author: zxw <2420813702@qq.com>
Date:   Thu Dec 20 18:00:53 2018 +0800delte 1 2 3 4#显示最近一次提交的内容差异
[root@localhost mycode]# git log -p -1
commit 013533b88d0e8db294f09baa0ab73486d948d789
Author: zxw <2420813702@qq.com>
Date:   Thu Dec 20 18:14:36 2018 +0800delte 1 2 3 4diff --git a/1 b/1
new file mode 100644
index 0000000..e69de29
diff --git a/2 b/2
new file mode 100644
index 0000000..e69de29
diff --git a/3 b/3
new file mode 100644
index 0000000..e69de29
diff --git a/4 b/4
new file mode 100644
index 0000000..e69de29
#显示提交内容的修改概要
[root@localhost mycode]# git log --stat -2
commit 013533b88d0e8db294f09baa0ab73486d948d789
Author: zxw <2420813702@qq.com>
Date:   Thu Dec 20 18:14:36 2018 +0800delte 1 2 3 41 | 02 | 03 | 04 | 04 files changed, 0 insertions(+), 0 deletions(-)commit d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06
Author: zxw <2420813702@qq.com>
Date:   Thu Dec 20 18:00:53 2018 +0800delte 1 2 3 410            | 05             | 06             | 07             | 08             | 09             | 0yunjisuan.txt | 07 files changed, 0 insertions(+), 0 deletions(-)
#用一行显示提交的历史记录
[root@localhost mycode]# git log --pretty=oneline
013533b88d0e8db294f09baa0ab73486d948d789 delte 1 2 3 4
d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06 delte 1 2 3 4
5ef619ab43bbe02f41aa7f6263e63a4d00826c72 git服务端修改了test.txt
a02fcb73cc845b4ecb551106f552a9e1cda7ac98 再次改动一次test.txt
7f8dfd36d0db6bf4d4d37e00ef1af2ba5b3687a9 修改了test.txt
8b12081334b53c816bf045b827e2e5aea03723bf test.txt

追根溯源-git log

Git还原历史数据

Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈西字符串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录,而上一个提交版本会叫(HEAD^),上上一个版本则会叫做(HEAD俩个尖叫号),当然一般会用HEAD~5来表示往上数第五个提交版本。

  • git reset --hard HEAD^ #–>还原历史提交版本上一次
  • git reset --hard 3de15d4 #–>找到历史还原点的SHA-1值后,就可以还原(值不写全,系统会自动匹配)
修改一个文件,并提交到本地仓库
[root@localhost mycode]# echo "www" >> yunjisuan.txt
[root@localhost mycode]# echo "www" >> yunjisuan.txt
[root@localhost mycode]# echo "www" >> yunjisuan.txt
[root@localhost mycode]# cat yunjisuan.txt
welcome
welcome
www
www
www
[root@localhost mycode]# git add *
[root@localhost mycode]# git commit -m "yunjisuan.txt添加了内容"
[master a395f3e] yunjisuan.txt添加了内容1 file changed, 5 insertions(+)#查看历史几条记录,并回滚到上一个提交版本
[root@localhost mycode]# git log --pretry=oneline
fatal: 未能识别的参数:--pretry=oneline
[root@localhost mycode]# git log --pretty=oneline
a395f3e06ea0a0bff5d291f4e1507d0371d2126a yunjisuan.txt添加了内容
013533b88d0e8db294f09baa0ab73486d948d789 delte 1 2 3 4
d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06 delte 1 2 3 4
5ef619ab43bbe02f41aa7f6263e63a4d00826c72 git服务端修改了test.txt
a02fcb73cc845b4ecb551106f552a9e1cda7ac98 再次改动一次test.txt
7f8dfd36d0db6bf4d4d37e00ef1af2ba5b3687a9 修改了test.txt
8b12081334b53c816bf045b827e2e5aea03723bf test.txt#回滚到当前版本
[root@localhost mycode]# git reset --hard HEAD^
HEAD 现在位于 013533b delte 1 2 3 4#所处的位置
[root@localhost mycode]# git log --pretty=oneline
013533b88d0e8db294f09baa0ab73486d948d789 delte 1 2 3 4
d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06 delte 1 2 3 4
5ef619ab43bbe02f41aa7f6263e63a4d00826c72 git服务端修改了test.txt
a02fcb73cc845b4ecb551106f552a9e1cda7ac98 再次改动一次test.txt
7f8dfd36d0db6bf4d4d37e00ef1af2ba5b3687a9 修改了test.txt
8b12081334b53c816bf045b827e2e5aea03723bf test.txt#查看之前改动的文件
[root@localhost mycode]# cat yunjisuan.txt         #没有任何内容#回滚到指定提交的版本
[root@localhost mycode]# git log --pretty=oneline
013533b88d0e8db294f09baa0ab73486d948d789 delte 1 2 3 4
d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06 delte 1 2 3 4
5ef619ab43bbe02f41aa7f6263e63a4d00826c72 git服务端修改了test.txt
a02fcb73cc845b4ecb551106f552a9e1cda7ac98 再次改动一次test.txt         #计划回滚到这个版本
7f8dfd36d0db6bf4d4d37e00ef1af2ba5b3687a9 修改了test.txt
8b12081334b53c816bf045b827e2e5aea03723bf test.txt#查看test文件内容
[root@localhost mycode]# cat test.txt
welcome
www
welcome yo yunjisuan#回滚到再次改动一次文件的版本
[root@localhost mycode]# git reset --hard a02fcb73
HEAD 现在位于 a02fcb7 再次改动一次test.txt#查看文件内容
[root@localhost mycode]# cat test.txt
welcome
www
[root@localhost mycode]# git log --pretty=oneline
a02fcb73cc845b4ecb551106f552a9e1cda7ac98 再次改动一次test.txt
7f8dfd36d0db6bf4d4d37e00ef1af2ba5b3687a9 修改了test.txt
8b12081334b53c816bf045b827e2e5aea03723bf test.txt

Git还原未来数据

当我们回滚到历史某个提交版本了以后;
我们发现我们已经没有在那个版本之后的提交记录了;
也就是说,我们一旦还原了历史版本,想要再次还原回去,那么就回不去了。
如此一来,一旦错了。我们怎么办呢?

git reflog:查看未来历史更新点

#查看未来历史更新点
[root@localhost mycode]# git reflog
a02fcb7 HEAD@{0}: reset: moving to a02fcb73
013533b HEAD@{1}: reset: moving to HEAD^
a395f3e HEAD@{2}: commit: yunjisuan.txt添加了内容
013533b HEAD@{3}: commit: delte 1 2 3 4
d1a2f2a HEAD@{4}: commit: delte 1 2 3 4
5ef619a HEAD@{5}: commit: git服务端修改了test.txt
a02fcb7 HEAD@{6}: merge zxw/master: Fast-forward
8b12081 HEAD@{7}: commit (initial): test.txt#还原之前的版本
[root@localhost mycode]# git reflog
a02fcb7 HEAD@{0}: reset: moving to a02fcb73
013533b HEAD@{1}: reset: moving to HEAD^
a395f3e HEAD@{2}: commit: yunjisuan.txt添加了内容
013533b HEAD@{3}: commit: delte 1 2 3 4
d1a2f2a HEAD@{4}: commit: delte 1 2 3 4
5ef619a HEAD@{5}: commit: git服务端修改了test.txt
a02fcb7 HEAD@{6}: merge zxw/master: Fast-forward
8b12081 HEAD@{7}: commit (initial): test.txt
[root@localhost mycode]# ls
test.txt
[root@localhost mycode]# git reset --hard a395f3e
HEAD 现在位于 a395f3e yunjisuan.txt添加了内容
[root@localhost mycode]# ls
1  10  2  3  4  5  6  7  8  9  test.txt  yunjisuan.txt
[root@localhost mycode]# git log --pretty=oneline
a395f3e06ea0a0bff5d291f4e1507d0371d2126a yunjisuan.txt添加了内容
013533b88d0e8db294f09baa0ab73486d948d789 delte 1 2 3 4
d1a2f2ae0869e08218a1ff6ac24135a7a0cb5d06 delte 1 2 3 4
5ef619ab43bbe02f41aa7f6263e63a4d00826c72 git服务端修改了test.txt
a02fcb73cc845b4ecb551106f552a9e1cda7ac98 再次改动一次test.txt
7f8dfd36d0db6bf4d4d37e00ef1af2ba5b3687a9 修改了test.txt
8b12081334b53c816bf045b827e2e5aea03723bf test.txt

Git的标签使用

前面回滚使用的是一串字符串,又长又难记
git tag <标签> -m “描述”
每次提交都可以打一个标签

  • git tag v1.0 : 给当前提交内容打一个标签(方便回滚)
  • git tag : 查看当前所有的标签
  • git show v1.0 :查看当前1.0版本的详细信息
  • git tag v1.2 -m “描述”
  • git tag -d v1.0 :删除之前的v1.0标签

gitignore文件匹配规则

*.log 表示忽略项目中所有以.log结尾的文件
123?.log 表示忽略项目中所有以123加任意一个字符的文件
/error.log 表示忽略根目录下的error.log文件
**/java/ 匹配所有java目录下的所有文件
!/error.log 表示在前面的匹配规则中,被忽略了的文件,你不想它被忽略,那么就可以在文件前加叹号

#在Git01上
[root@Git01 mycode]# ls
10  5  6  7  8  9  access.log  benet.txt  test.txt  x.idea  xx.idea
[root@Git01 mycode]# git status
位于分支 master
您的分支领先 'test/master' 共 2 个提交。(使用 "git push" 来发布您的本地提交)
未跟踪的文件:(使用 "git add <文件>..." 以包含要提交的内容)access.log          #三个文件需要被addx.idea              #三个文件需要被addxx.idea             #三个文件需要被add
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
#添加.gitignore文件
[root@Git01 mycode]# cat .gitignore
target
*.log       #忽略任意开头.log结尾文件
?.idea      #忽略任意一个字符开头.idea结尾文件
#查看git工作目录文件状态
[root@Git01 mycode]# git status
位于分支 master
您的分支领先 'test/master' 共 2 个提交。(使用 "git push" 来发布您的本地提交)
未跟踪的文件:(使用 "git add <文件>..." 以包含要提交的内容).gitignore          #刚才被规则覆盖的文件没了xx.idea             #刚才被规则覆盖的文件没了
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

GIT分支管理

在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。
那在哪干活呢?干活都在dev分支上。每个人从dev分支创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。
所以,团队的合作分支看起来会像下图那样。

分支管理工作流程:
在工作中,为了保证master分支稳定,产品经理通常会从master分支上复制一份代码作为dev分支;
然后成员开发A在从dev分支上复制一份代码叫做michael;
然后成员开发B再从dev分支上复制一份代码叫做bob;
平时开发A和开发B推送和拉取代码都在自己的个人分支michael和bob上。
当一个新功能开发完毕,或者一个bug修改完毕以后。开发人员会先将代码变更推送到自己的个人分支,然后再把个人分支的变更合并到dev分支里;
当开发经理和测试人员拉取dev分支的代码进行测试以后,如果没问题,那么开发经理会把dev分支的变更合并进master版本;
最后,由于master版本新增了测试过的新功能,那么就需要进行项目发布或者代码上线了。

GIT本地分支管理

本地分支的创建与切换
git branch : 查看当前分支情况,当前分支前有*号
git branch linux : 创建分支
git checkout: 检查本地分支与远程分支的变更差异
git checkout linux:切换分支

[root@localhost mycode]# git branch     #当前所处的位置
* master
[root@localhost mycode]# git branch linux      #创建分支linux
[root@localhost mycode]# git branch        #当前所处的位置linux
* master
[root@localhost mycode]# git checkout linux        #切换分支
切换到分支 'linux'
[root@localhost mycode]# git branch        #当前所处的位置
* linuxmaster

尝试在linux本地分支进行代码提交

[root@localhost mycode]# echo "我是代码" >> yunjisuan.txt
[root@localhost mycode]# ls
1  10  2  3  4  5  6  7  8  9  test.txt  yunjisuan.txt
[root@localhost mycode]#
[root@localhost mycode]# cat yunjisuan.txt
welcome
welcome
www
www
www
我是代码
[root@localhost mycode]# git add *
[root@localhost mycode]# git status
位于分支 linux
要提交的变更:(使用 "git reset HEAD <文件>..." 以取消暂存)修改:     yunjisuan.txt[root@localhost mycode]# git commit -m "123123"
[linux 66841d6] 1231231 file changed, 1 insertion(+)

Github公有仓库相关推荐

  1. GitHub 版本控制 项目托管 04 创建GitHub远程仓库

    (本博客GitHub分支下所有相关资料的统一下载链接:http://pan.baidu.com/s/1bo5RrtX) (PS:任何问题欢迎在下方评论区留言,12小时内回复~) 上接<GitHu ...

  2. GitHub 版本控制 项目托管 04 创建GitHub远程仓库-阿里云开发者社区

    (本博客GitHub分支下所有相关资料的统一下载链接:http://pan.baidu.com/s/1bo5RrtX) (PS:任何问题欢迎在下方评论区留言,12小时内回复~) 上接<GitHu ...

  3. GitHub 远程仓库 de 第一次配置

    GitHub远程仓库, Git是分布式版本控制系统,同一个Git仓库,可以分布到不同的机器上.首先找一台电脑充当服务器的角色, 每天24小时开机,其他每个人都从这个"服务器"仓库克 ...

  4. git 上传代码到指定仓库_初次使用git上传代码到github远程仓库

    一.新建代码库 注册好github登录后,首先先在网页上新建代码库. 点击右上角"+"→New repository 进入如下页面:按照要求填写完成后,点击按钮创建代码库创建成功. ...

  5. git创建/合并分支/删除分支/将修改后的内容同步到GitHub远程仓库

    1.创建分支并切换到刚创建的分支(这里创建新的分支来修改README.md的内容然后将创建的分支与master分支合并,最后删除创建的分支) $ git checkout -b 分支名 Switche ...

  6. 【Git笔记1】本地项目与GitHub远程仓库互联

    秋招面试的时候,面试官就问了我:你会Git吗?我迟疑看着他,他微笑着说,入职前要抓紧时间好好学习一下. 由于地理位置优势先来公司熟悉下环境,咨询算法组组长入职前可以做些什么准备?组长说,Git要好好学 ...

  7. 使用git命令提交代码到Github远程仓库的方法

    首先新建一个github地址仓库名字随便取: 文件然后记下github仓库的地址,在桌面新建刚刚创建创库的名字为文件夹,添加一些项目文件后使用这个命令初始化仓库:git init 如下图: 2.使用这 ...

  8. 使用git上传代码到github远程仓库

    一.新建代码库 注册好github登录后,首先先在网页上新建代码库. 点击右上角"+"→New repository 进入如下页面:按照要求填写完成后,点击按钮创建代码库创建成功. ...

  9. 使用Git推送代码到GitHub远程仓库

    文章目录 1.创建git仓库 2.将代码纳入版本控制 3.添加代码到仓库中(本地仓库) 4.推送代码到GitHub远程仓库 5.克隆仓库 1.创建git仓库 语法:git init name 参数说明 ...

最新文章

  1. Java 学习笔记 ------第二章 从JDK到IDE
  2. PHP在Windows环境下获取GMT时间
  3. 简洁明了——STL容器库之set头文件常用函数集合
  4. 使用shell脚本调用mysql数据库存储过程,并设置定时任务
  5. 以图换字的几种方法及优劣分析
  6. Android APK系列4-------Android编译APK后的系统结构
  7. vue中Component错误
  8. python有效的变量名有哪些_python变量名有哪些
  9. HackTheBox::Blunder
  10. python使用mechanize模拟登陆新浪邮箱
  11. 服务器挂微信显示登陆异常怎么办,解决微信提示“该微信账号因登录环境异常,已被限制登录。”的方法...
  12. elasticsearch7.x catAPI之indices
  13. 【即时通讯软件系统——开题报告 分享(仅供参考呀)】
  14. 华为煤矿军团首登央视 | 发布会金句爆棚
  15. 无人机倾斜摄影测量技术有哪些特点?
  16. mysql 分区个数限制_mysql8 参考手册-分区的限制
  17. 串级PID及角度过零处理
  18. python soup歌词_python学习之爬虫(一) ——————爬取网易云歌词
  19. 【web安全原理分析】-XEE漏洞入门
  20. 流动的数据——使用 RxJS 构造复杂单页应用的数据逻辑

热门文章

  1. 【CCF】NOI笔试题库
  2. 新概念英语(第二册)复习(原文及全文翻译)——Lesson 41 - Lesson 50
  3. 在GNS3模拟NAT的仿真实验
  4. 最早截止时间优先即EDF算法
  5. 文件包含getshell
  6. 无法停止通用卷设备的解决方法
  7. python爬取顶点小说简单版
  8. 通关GO语言19 性能优化:Go 语言如何进行代码检查和优化?
  9. 保养腰椎,不妨做做腰肌操。
  10. 我们需要什么?——IT公司面试官经验谈