博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
git基本操作
阅读量:7086 次
发布时间:2019-06-28

本文共 8787 字,大约阅读时间需要 29 分钟。

温馨提示

此博客用于记录git常用的命令参数使用方法

遗忘或想不起来了可以来看一眼

所以写的并不详细、不适合初学者学习

环境说明

[root@Check1 ~]# cat /etc/redhat-release CentOS release 6.10 (Final)[root@Check1 ~]# uname -aLinux Check1.net 2.6.32-754.2.1.el6.x86_64 #1 SMP Fri Jul 13 12:50:12 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

git的安装

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

创建版本仓库

[root@Check1 ~]# mkdir -p /usr/local/git/test[root@Check1 ~]# cd /usr/local/git/test/[root@Check1 test]# git init Initialized empty Git repository in /usr/local/git/test/.git/

创建版本库后会在目录内生成一个隐藏目录.git  初始化一个空版本

[root@Check1 test]# ls -al总用量 12drwxr-xr-x 3 root root 4096 12月 16 20:51 .drwxr-xr-x 3 root root 4096 12月 16 20:51 ..drwxr-xr-x 7 root root 4096 12月 16 20:51 .git[root@Check1 test]# cd .git/[root@Check1 .git]# lsbranches config description HEAD hooks info objects refs

版本创建

[root@Check1 test]# pwd/usr/local/git/test[root@Check1 test]# echo "version 1.0" >> code.txt   编辑一个文件或目录,在里面添加内容[root@Check1 test]# git add code.txt   将此文件添加到缓存区[root@Check1 test]# git commit -m "版本1.0"   提交此文件创建版本记录 -m "版本说明信息"[master (root-commit) 0367a6a] 版本1.01 files changed, 1 insertions(+), 0 deletions(-)create mode 100644 code.txt

再创建几个版本

[root@Check1 test]# echo "version 2.0" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本2.0"[master c94bcab] 版本2.01 files changed, 1 insertions(+), 0 deletions(-)[root@Check1 test]# echo "version 3.0" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本3.0"[master 9f98991] 版本3.01 files changed, 1 insertions(+), 0 deletions(-)

在git中创建版本记录只是记录一个修改后面的版本是依赖于前面的版本的

查看版本记录

[root@Check1 test]# git logcommit 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5Author: wcy 
Date: Sun Dec 16 21:02:31 2018 +0800版本3.0commit c94bcab611218e72f9d806ee347dcfb1d12e73a3Author: wcy
Date: Sun Dec 16 21:01:58 2018 +0800版本2.0commit 0367a6a3bd901309e282dc630e6562b7f270e39bAuthor: wcy
Date: Sun Dec 16 20:55:10 2018 +0800版本1.0

版本回退

根据HEAD指针回退

HEAD指向当前版本

[root@Check1 test]# git reset --hard HEAD^   回退至上一个版本HEAD is now at c94bcab 版本2.0

HEAD^指的是上一个版本 HEAD^^指的是上两个版本

上100个版本可以用 HEAD~100
上200个版本可以用 HEAD~200表示

根据版本序列号回退

[root@Check1 test]# git reset --hard 9f9899114fHEAD is now at 9f98991 版本3.0

版本序列号在上面git log中查看git commit对应的字符串就是  回退的时候不用填写全部版本序列号,前几位即可

如果Git的版本号通过git log查看不到了可以通过git reflog查看操作记录

比如回退到版本3.0

[root@Check1 test]# git reset --hard HEAD^HEAD is now at c94bcab 版本2.0[root@Check1 test]# git logcommit c94bcab611218e72f9d806ee347dcfb1d12e73a3Author: wcy 
Date: Sun Dec 16 21:01:58 2018 +0800版本2.0commit 0367a6a3bd901309e282dc630e6562b7f270e39bAuthor: wcy
Date: Sun Dec 16 20:55:10 2018 +0800版本1.0

查看操作记录

[root@Check1 test]# git reflogc94bcab HEAD@{
0}: HEAD^: updating HEAD9f98991 HEAD@{
1}: 9f9899114f: updating HEADc94bcab HEAD@{
2}: HEAD^: updating HEAD9f98991 HEAD@{
3}: commit: 版本3.0c94bcab HEAD@{
4}: commit: 版本2.00367a6a HEAD@{
5}: commit (initial): 版本1.0[root@Check1 test]# git reset --hard 9f98991 回退到版本3.0HEAD is now at 9f98991 版本3.0

可以看到上面版本3.0 最前面的就是之前版本序列号的前几位,然后再跟进这个序列号进行其他操作

9f98991 HEAD@{
3}: commit: 版本3.0 序列号即:9f98991

工作区、暂存区和版本库

git是管理版本的修改

git commit时只是会把暂存区的修改提交到版本记录中(也就是创建一个版本记录)

撤销修改

丢弃工作区的改动

在工作区修改 code.txt  使用git status查看一下状态

[root@Check1 test]# echo "version 4.0" >> code.txt [root@Check1 test]# git status# On branch master# Changed but not updated:# (use "git add 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: code.txt#no changes added to commit (use "git add" and/or "git commit -a")

可以看到已修改文件 code.txt

可以操作的选项是

1、git add 提交至暂存区

2、git checkout -- 删除工作区的修改

[root@Check1 test]# git checkout -- code.txt [root@Check1 test]# cat code.txt version 1.0version 2.0version 3.0

删除工作区的修改后 发现刚才添加的version 4.0 已经没有了

已经添加至暂存区未commit

[root@Check1 test]# echo "version 4.0" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git status# On branch master# Changes to be committed:# (use "git reset HEAD 
..." to unstage)## modified: code.txt#

把暂存区的文件拉回工作区

[root@Check1 test]# git reset HEAD code.txt Unstaged changes after reset:M    code.txt

再把文件从工作区中checkout出去 丢弃工作区的改动

[root@Check1 test]# git status# On branch master# Changed but not updated:# (use "git add 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: code.txt#no changes added to commit (use "git add" and/or "git commit -a")[root@Check1 test]# git checkout -- code.txt [root@Check1 test]# git status# On branch masternothing to commit (working directory clean)

已经commit

[root@Check1 test]# echo "version 4.0" >> code.txt [root@Check1 test]# cat code.txt version 1.0version 2.0version 3.0version 4.0[root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本4.0"[master d61d2f2] 版本4.01 files changed, 1 insertions(+), 0 deletions(-)

进行版本回退

[root@Check1 test]# git log --pretty=onelined61d2f2cfdf2e2feb0dec63227315fcb7c004ff9 版本4.09f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.00367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0[root@Check1 test]# git reset --hard 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5HEAD is now at 9f98991 版本3.0[root@Check1 test]# cat code.txt version 1.0version 2.0version 3.0

对比文件的不同

对比工作区和版本库的某个文件

[root@Check1 test]# git diff HEAD^ -- code.txt diff --git a/code.txt b/code.txtindex d1f9bad..69afb9a 100644--- a/code.txt+++ b/code.txt@@ -1,2 +1,3 @@version 1.0version 2.0+version 3.0

---为三个版本 HEAD^

+++为工作区的版本

结果为+version 3.0 也就是工作区比上个版本多这一行

对比两个版本库中文件

[root@Check1 test]# git diff HEAD~1 HEAD~2 -- code.txt diff --git a/code.txt b/code.txtindex d1f9bad..7c8de03 100644--- a/code.txt+++ b/code.txt@@ -1,2 +1 @@version 1.0-version 2.0

由此可以看出---为前者 +++为后者

可看出 上个版本比上上个版本多一行 version 2.0

删除文件

新建一个测试文件code2.txt

[root@Check1 test]# echo "version 1.0" >> code2.txt[root@Check1 test]# git status# On branch master# Untracked files:# (use "git add 
..." to include in what will be committed)## code2.txtnothing added to commit but untracked files present (use "git add" to track)[root@Check1 test]# git add code2.txt [root@Check1 test]# git commit -m "new code"[master e39c2ef] new code1 files changed, 1 insertions(+), 0 deletions(-)create mode 100644 code2.txt

rm直接删除工作区文件

[root@Check1 test]# lscode2.txt code.txt[root@Check1 test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y[root@Check1 test]# git status# On branch master# Changed but not updated:# (use "git add/rm 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## deleted: code2.txt#no changes added to commit (use "git add" and/or "git commit -a")

删除后可以通过git checkout -- 撤回删除

[root@Check1 test]# git checkout -- code2.txt[root@Check1 test]# lscode2.txt code.txt

git rm删除暂存区文件

[root@Check1 test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y[root@Check1 test]# git status# On branch master# Changed but not updated:# (use "git add/rm 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## deleted: code2.txt#no changes added to commit (use "git add" and/or "git commit -a")[root@Check1 test]# git rm code2.txtrm 'code2.txt'[root@Check1 test]# git status# On branch master# Changes to be committed:# (use "git reset HEAD
..." to unstage)## deleted: code2.txt#

删除后可以通过

1、git reset HEAD <file>...

2、git checkout -- <file>...

进行恢复

[root@Check1 test]# git reset HEAD code2.txtUnstaged changes after reset:M    code2.txt[root@Check1 test]# git checkout -- code2.txt[root@Check1 test]# lscode2.txt code.txt

git commit提交删除

[root@Check1 test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y[root@Check1 test]# git rm code2.txtrm 'code2.txt'[root@Check1 test]# git commit -m "DEL code2.txt"[master c88df5f] DEL code2.txt1 files changed, 0 insertions(+), 1 deletions(-)delete mode 100644 code2.txt

恢复只能版本回退

[root@Check1 test]# git reflogc88df5f HEAD@{
0}: commit: DEL code2.txte39c2ef HEAD@{
1}: commit: new code9f98991 HEAD@{
2}: 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5: updating HEADd61d2f2 HEAD@{
3}: commit: 版本4.09f98991 HEAD@{
4}: 9f98991: updating HEADc94bcab HEAD@{
5}: HEAD^: updating HEAD9f98991 HEAD@{
6}: 9f9899114f: updating HEADc94bcab HEAD@{
7}: HEAD^: updating HEAD9f98991 HEAD@{
8}: commit: 版本3.0c94bcab HEAD@{
9}: commit: 版本2.00367a6a HEAD@{
10}: commit (initial): 版本1.0[root@Check1 test]# git reset --hard e39c2efHEAD is now at e39c2ef new code[root@Check1 test]# lscode2.txt code.txt

 

转载于:https://www.cnblogs.com/LuckWJL/p/10128886.html

你可能感兴趣的文章
ambari与ClouderaManager
查看>>
cdn加速
查看>>
为什么学习Linux系统?
查看>>
Windows Server2012上使用Nginx做文件服务器
查看>>
linux下的apache配置
查看>>
app测试和web端测试的区别
查看>>
一瓶汽水1元,两瓶汽水可换一瓶,现有20元,最多可喝多少瓶汽水
查看>>
图文教程自动登录expect脚本实例
查看>>
2019 第四周 开发笔记
查看>>
CORBA分布式实现
查看>>
第五天的学习
查看>>
微信小程序小技巧系列《二》show内容展示,上传文件编码问题
查看>>
Rancher Kubernetes Engine(RKE)正式发布:闪电般的Kubernetes安
查看>>
$.get()请求返回一个html页面,获取该页面特定id的元素
查看>>
Linux中的加解密实现
查看>>
Java开发GUI之CardLayout卡片布局
查看>>
将自己本地的项目托管到github上
查看>>
yum仓库搭建
查看>>
this怎么用(基础)
查看>>
安装server2008后,提示“Windows无法验证此文件的数字签名”
查看>>