git使用(1)
安装git(centos7)
1.查看是否存在低版本git
[root@baseserver learngit]# git --version ##查看版本
git version 2.9.5
[root@baseserver learngit]# yum remove git ##卸载git
2.安装依赖
[root@baseserver learngit]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
[root@baseserver learngit]# yum install gcc-c++ perl-ExtUtils-MakeMaker
3.下载安装git
[root@baseserver learngit]# cd /usr/src
[root@baseserver learngit]# wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
4.解压
[root@baseserver learngit]# tar xf git-2.9.5.tar.gz
5.编译
[root@baseserver learngit]# cd git-2.9.5
[root@baseserver learngit]# make configure
[root@baseserver learngit]# ./configure --prefix=/usr/git ##配置目录
[root@baseserver learngit]# make profix=/usr/git
[root@baseserver learngit]# make install
6.加入环境变量
[root@baseserver learngit]# echo 'export PATH=$PATH:/usr/git/bin' >> /etc/profile
[root@baseserver learngit]# source /etc/profile
7.检查版本
[root@baseserver learngit]# git --version
git version 2.9.5
创建版本库
1.初始化仓库
[root@baseserver git2]# mkdir gittest ##创建空文件夹
[root@baseserver git2]# cd gittest/ ##进入该文件夹
[root@baseserver gittest]# pwd ##查看当前路径
/root/git2/gittest
[root@baseserver gittest]# git init ##初始化仓库
初始化空的 Git 仓库于 /root/git2/gittest/.git/
2.创建文件并添加到仓库,如将test.txt添加到版本库
[root@baseserver gittest]# vi test.txt ##创建test.txt(输入内容后保存即可)
[root@baseserver gittest]# git status ##查看仓库状态
位于分支 master
初始提交
未跟踪的文件:
(使用 "git add <文件>..." 以包含要提交的内容)
test.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@baseserver gittest]# git add test.txt ##添加文件
[root@baseserver gittest]# git status ##查看仓库状态
位于分支 master
初始提交
要提交的变更:
(使用 "git rm --cached <文件>..." 以取消暂存)
新文件: test.txt
[root@baseserver gittest]# git commit -m "添加test.txt" ##提交并添加注释
[master(根提交) d6c1695] 添加test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
提交文件
1.修改test.txt文件,查看仓库状态,提示未加入提交的修改
[root@baseserver gittest]# vi test.txt
[root@baseserver gittest]# git status
位于分支 master
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git checkout -- <文件>..." 丢弃工作区的改动)
修改: test.txt
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
2.查看文件差异
[root@baseserver gittest]# git diff test.txt
diff --git a/test.txt b/test.txt
index b7a82be..a7caebf 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
this is a text txt.
+this is a update.
3.添加(不论是新建还是修改,都需要添加)
[root@baseserver gittest]# git add test.txt
4.查看仓库状态,提示未提交的变更
[root@baseserver gittest]# git status ##未提交但已经加入提交,此时不再提示需要加入提交
位于分支 master
要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)
修改: test.txt
5.提交
[root@baseserver gittest]# git commit -m "修改test.txt"
[master 42508c1] 修改test.txt
1 file changed, 1 insertion(+)
6.查看仓库状态,此时无任何需要提交
[root@baseserver gittest]# git status
位于分支 master
nothing to commit, working tree clean
版本回退
1.查看提交记录
[root@baseserver gittest]# git log
commit f8f70ba9ac5b2413532daf6da57baa97f93fd01e
Author: xxxxxx <xxxxxx@gitee.com>
Date: Thu Dec 26 10:26:46 2024 +0800
another update
commit 42508c153ce102b5449be6cd91006ca40f8a38ac
Author: xxxxxx <xxxxxx@gitee.com>
Date: Thu Dec 26 10:21:09 2024 +0800
修改test.txt
commit d6c1695cf53ad4ff447872b2d9d78867b6806244
Author: xxxxxx <xxxxxx@gitee.com>
Date: Thu Dec 26 09:45:15 2024 +0800
添加test.txt
2.回退到上一个版本,HEAD表示当前提交,HEAD^表示上一个,HEAD^^表示上两个,
[root@baseserver gittest]# git reset --hard HEAD^
HEAD 现在位于 42508c1 修改test.txt
[root@baseserver gittest]# git log
commit 42508c153ce102b5449be6cd91006ca40f8a38ac
Author: xxxxxx <xxxxxx@gitee.com>
Date: Thu Dec 26 10:21:09 2024 +0800
修改test.txt
commit d6c1695cf53ad4ff447872b2d9d78867b6806244
Author: xxxxxx <xxxxxx@gitee.com>
Date: Thu Dec 26 09:45:15 2024 +0800
添加test.txt
3.回退(跳转)到指定提交记录(跳转指定提交,回退以后也可以跳转至当前提交以后的提交)
[root@baseserver gittest]# git reset --hard f8f70ba9ac5b2413532daf6da57baa97f93fd01e
4.使用HEAD^时,如果要回退至前100个提交,不可能写100个^,使用~100来表示
[root@baseserver gittest]# git reset --hard HEAD~2
HEAD 现在位于 d6c1695 添加test.txt
[root@baseserver gittest]# git log
commit d6c1695cf53ad4ff447872b2d9d78867b6806244
Author: hxxxxxx <xxxxxx@gitee.com>
Date: Thu Dec 26 09:45:15 2024 +0800
添加test.txt
5.查看历史操作记录,可以看到HEAD移动过程(HEAD指向当前版本)
root@baseserver gittest]# git reflog
f8f70ba HEAD@{0}: reset: moving to f8f70ba9ac5b2413532daf6da57baa97f93fd01e
d6c1695 HEAD@{1}: reset: moving to HEAD~2
f8f70ba HEAD@{2}: reset: moving to f8f70ba9ac5b2413532daf6da57baa97f93fd01e
d6c1695 HEAD@{3}: reset: moving to HEAD^^
f8f70ba HEAD@{4}: reset: moving to f8f70ba9ac5b2413532daf6da57baa97f93fd01e
42508c1 HEAD@{5}: reset: moving to HEAD^
f8f70ba HEAD@{6}: commit: another update
42508c1 HEAD@{7}: commit: 修改test.txt
d6c1695 HEAD@{8}: commit (initial): 添加test.txt
查看修改对比
[root@baseserver gittest]# git diff test.txt ##查看当前修改与加入提交(未提交)的差异
diff --git a/test.txt b/test.txt
index e6ddb59..ec7a68b 100644
--- a/test.txt
+++ b/test.txt
@@ -2,3 +2,5 @@ this is a text txt.
this is a update.
this is another update.
this is next update.
+this is next another update.
+this is next next another update.
[root@baseserver gittest]# git add test.txt ##加入提交
[root@baseserver gittest]# git diff test.txt ##加入提交后,未见差异(与加入提交比较)
[root@baseserver gittest]# git diff HEAD -- test.txt ##与已提交比较,可见差异
diff --git a/test.txt b/test.txt
index e6ddb59..ec7a68b 100644
--- a/test.txt
+++ b/test.txt
@@ -2,3 +2,5 @@ this is a text txt.
this is a update.
this is another update.
this is next update.
+this is next another update.
+this is next next another update.
[root@baseserver gittest]# git commit -m "update next" ##提交
[master a91f086] update next
1 file changed, 2 insertions(+)
[root@baseserver gittest]# git diff test.txt ##查看差异(与已加入提交比对,未见差异)
[root@baseserver gittest]# git diff HEAD -- test.txt ##查看差异(与已提交比对,未见差异)
浙公网安备 33010602011771号