不推荐使用YUM安装版本过低,Centos上是git-daemon.x86_64 1.7.1-9.el6_9
源码安装git
解决依赖关系
yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
下载源码包
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.21.0.tar.gz
tar xf git-2.21.0.tar.gz
cd git-2.21.0
make prefix=/usr/local/git all
make prefix=/usr/local/git install
rm -rf /usr/bin/git
ln -s /usr/local/git/bin/git /usr/bin/git
[root@tom01 git-2.21.0]# git --version
git version 2.21.0
创建git仓库
mkdir /git
[root@tom01 /]# cd /git/
[root@tom01 git]# git init
Reinitialized existing Git repository in /git/.git/
[root@tom01 git]# git config --global user.name “test”
[root@tom01 git]# git config --global user.email “xxxxxxx@qq.com”
git config --global color.ui true #语法高亮
[root@tom01 git]# git config --list
user.name=test
user.email=xxxxx@qq.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
Git四个区域
远程仓库
本地仓库
暂存区域
工作目录
四种状态
常用命令
git add 加入暂存区(索引区)
git status 查看状态
git status -s 状态概览
git diff 尚未暂存的文件
git diff --staged 暂存区文件
git commit 提交更新
git log 查看提交过的历史记录
git log --oneline 单行显示提交历史记录
git log --oneline --decorate 查看当前指针的位置
git
git reset --hard 唯一hash值 回滚到制定位置
git rm 从版本库中移除
git rm --cached 从暂存区中移除
git mv 相当于 mv git rm git add 三个命令
创建完版本库我们在里面创建文件测试
[root@tom01 git]# echo “git test” > index.html
[root@tom01 git]# git status
On branch master
No commits yet
Untracked files:
(use “git add …” to include in what will be committed)
index.html
nothing added to commit but untracked files present (use “git add” to track)
注:状态未被追踪
加入暂存区域
[root@tom01 git]# git add index.html
[root@tom01 git]# git status
On branch master
No commits yet
Changes to be committed:
(use “git rm --cached …” to unstage)
new file: index.html
状态:未被提交 在暂存区
提交到本地库
[root@tom01 git]# git commit -m “first commit”
[master (root-commit) aca1e1c] first commit
1 file changed, 1 insertion(+)
create mode 100644 index.html
注:-m 写一个注释
查看库状态
[root@tom01 git]# git status
On branch master
nothing to commit, working tree clean
所有文件都被推到本地仓库
查看提交记录
[root@tom01 git]# git log
commit aca1e1cbc1abbc6767a0c4c1271b09acdd4cf8f0 (HEAD -> master)
Author: test 67782688@qq.com
Date: Sun Mar 17 10:39:51 2019 +0800
first commit
实验 从暂存区删除不想提交的文件
[root@tom01 git]# echo “pay test” > pay.html
[root@tom01 git]# echo “news test” > news.html
[root@tom01 git]# git add pay.html
[root@tom01 git]# git add news.html
[root@tom01 git]# git status
On branch master
Changes to be committed:
(use “git reset HEAD …” to unstage)
new file: news.html
new file: pay.html
现在不想提交pay文件 想从暂存区移除
[root@tom01 git]# git rm --cached pay.html
rm ‘pay.html’
[root@tom01 git]# git status
On branch master
Changes to be committed:
(use “git reset HEAD …” to unstage)
new file: news.html
Untracked files:
(use “git add …” to include in what will be committed)
pay.html
分支管理
创建分支
[root@tom01 git]# git branch testing
查看现在所在分支
[root@tom01 git]# git status
On branch master
切换分支(切换指针) checkout
[root@tom01 git]# git checkout testing
Switched to branch ‘testing’
查看现在所在分支
[root@tom01 git]# git status
On branch testing
列出所有分支
[root@tom01 git]# git branch
master
- testing
代码融合(合并) merge
在新分支上创建新的代码文件
[root@tom01 git]# echo “testing us” > testing.html
[root@tom01 git]# git add testing.html
[root@tom01 git]# git commit -m “first testing”
[testing 66c7a15] first testing
1 file changed, 1 insertion(+)
create mode 100644 testing.html
现在要把新开发的代码合并到master分支上
先切换到master
[root@tom01 git]# git checkout master
Switched to branch ‘master’
合并文件
[root@tom01 git]# git merge testing
Merge made by the ‘recursive’ strategy.
testing.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 testing.html
查看哪些分支被合并 只显示被融合的分支
[root@tom01 git]# git branch --merged
- master
testing
哪些分支没被合并
[root@tom01 git]# git branch --no-merged
Git管理
撤销对文件的修改
例:
在master分支上修改index.html文件
先确定一下现在状态和内容
[root@tom01 git]# git status
On branch master
nothing to commit, working tree clean
[root@tom01 git]# cat index.html
git test
修改文件
[root@tom01 git]# echo “second ----” >> index.html
查看现有状态和内容
[root@tom01 git]# git status
On branch master
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout – …” to discard changes in working directory)
modified: index.html
[root@tom01 git]# cat index.html
git test
second ----
现在发现修改的有问题 不想提交了
[root@tom01 git]# git checkout – index.html
[root@tom01 git]# git status
On branch master
nothing to commit, working tree clean
[root@tom01 git]# cat index.html
git test
数据回滚 reset
–soft 工作目录和缓存区都不会被改变
–minxed 默认选项 缓存区和你指定的提交同步,但工作目录不收影响
–hard 工作目录和缓存区目录都同步到你指定的提交
使用场景
git reset 提交层面 版本回滚,在私有分支上舍弃一些没有提交的更改
git reset 文件层面 将文件从缓存区移除
git checkout 提交层面 切换分支或查看旧版本
git checkout 文件层面 舍弃工作目录中的更改
git reflog 命令分析你所有分支的头指针的日志来查找出你在重写历史上可能丢失的提交