git- 仓库创建、修改、提交、撤销
1.仓库创建
zhangshuli@zhangshuli-MS-:~$ mkdir myGit
zhangshuli@zhangshuli-MS-:~$ cd myGit/
zhangshuli@zhangshuli-MS-:~/myGit$ git init
Initialized empty Git repository in /home/zhangshuli/myGit/.git/
2.更改添加
//创建一个新文件
zhangshuli@zhangshuli-MS-:~/myGit$ touch bbb.txt
//查看当前目录
zhangshuli@zhangshuli-MS-:~/myGit$ ls
aaa.txt bbb.txt
//查看仓库状态
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bbb.txt //更改,还没有跟仓库扯上关系
nothing added to commit but untracked files present (use "git add" to track)
//提交到缓存去(工作目录树)
zhangshuli@zhangshuli-MS-7817:~/myGit$ git add .
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: aaa.txt
#
zhangshuli@zhangshuli-MS-:~/myGit$
3.仓库修改还原
zhangshuli@zhangshuli-MS-:~/myGit$ ls
aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git br -av
* master 9a1e055 aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-:~/myGit$ touch bbb.txt ccc.txt
zhangshuli@zhangshuli-MS-:~/myGit$ ls
aaa.txt bbb.txt ccc.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bbb.txt
# ccc.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-:~/myGit$ git add bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# ccc.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git co ccc.txt
error: pathspec 'ccc.txt' did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-:~/myGit$ git checkout ccc.txt
error: pathspec 'ccc.txt' did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-:~/myGit$ git clear -df .
git: 'clear' is not a git command. See 'git --help'. Did you mean this?
clean
zhangshuli@zhangshuli-MS-:~/myGit$ git clean -df
Removing ccc.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
zhangshuli@zhangshuli-MS-:~/myGit$ zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean -df aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: aaa.txt
#
zhangshuli@zhangshuli-MS-7817:~/myGit$ vim aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git co aaa.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt bbb.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ touch ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ ls
aaa.txt bbb.txt ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$ git checkout ccc.txt
error: pathspec 'ccc.txt' did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git co ccc.txt
error: pathspec 'ccc.txt' did not match any file(s) known to git.
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean diff .
fatal: clean.requireForce defaults to true and neither -n nor -f given; refusing to clean
zhangshuli@zhangshuli-MS-7817:~/myGit$ git clean -f .
Removing ccc.txt
zhangshuli@zhangshuli-MS-7817:~/myGit$
从上面我们可以得到如下结论
1)git co = git checkout
2) git co 跟git clean 都是针对目录树的操作,对缓存区没有影响
3) git co 是针对版本库或者缓存区已经存在的项目进行的操作,它是去除两者的差异,相当于还原
4) git clean 针对的是目录树存在而版本库或者缓存区不存在的项目
5)两者的区别,其实就是,一个git co分析同一个文件内容差异;git clean分析文件目录差异
另外,cc = git co . | git clean -f .
4.版本库的撤消跟修改
zhangshuli@zhangshuli-MS-:~/myGit$ ls
aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git br -av
* master 9a1e055 aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-:~/myGit$ ls
aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ touch bbb.txt zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-:~/myGit$ git add .
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
zhangshuli@zhangshuli-MS-:~/myGit$ git reset .
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-:~/myGit$ git add .
zhangshuli@zhangshuli-MS-:~/myGit$ git commit -m "bbb.txt" -m "add"
[master 2f6533f] bbb.txt
files changed, insertions(+), deletions(-)
create mode bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-:~/myGit$ git br -av
* master 2f6533f bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git log
commit 2f6533fe371f8b5a9dfa9c502bc20e22aaaa6177
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + bbb.txt add commit 9a1e05516a0436f46b73c9553795ae22acfb2eee
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$
对于本地目录树跟缓冲区之间的操作,主要就是有两种
1)目录树->缓冲区:git add
2)缓冲区->目录树:git reset
3)看如下的例子
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-:~/myGit$ vim bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git add .
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
zhangshuli@zhangshuli-MS-:~/myGit$ vim bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: bbb.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: bbb.txt
#
zhangshuli@zhangshuli-MS-:~/myGit$ git reset bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bbb.txt
nothing added to commit but untracked files present (use "git add" to track)
zhangshuli@zhangshuli-MS-:~/myGit$ git clean -f .
Removing bbb.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-:~/myGit$
通过上面的操作,我们还可以再次加深下理解
1)git add :本质上是把一次修改先放到缓存区,如果你目录树中再次对它进行修改并提交,这个会覆盖之前的状态
2)git reset:是把缓存区的内容放回到目录树,但它不是覆盖,而是保持目录树中的当前状态
3)如果你删除了一个缓存区存在的文件,那么你要么可以使用git add . -A把缓存区的状态覆盖掉,要么可以git reset .这时候git reset 文件名 报错,因为找不到这个文件了
4)git commit -m "one" -m "two" ...其实就是注释的换行且中间间隔一行
5)git commit --amend 是在当前的节点上追加内容(当然也可以通过它来给注释重命名).理解错误,看如下实例
zhangshuli@zhangshuli-MS-:~/myGit$ git log
commit aec98e99d63338313b35f6f62a44de1e9aff7095
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my second commit commit e63204faffe5e482c18f188ca1c690d961924846
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git commit --amend
[master ef2db10] my second commit
files changed, insertions(+), deletions(-)
zhangshuli@zhangshuli-MS-:~/myGit$ git log
commit ef2db102fc369c5a20e4b9521f0acb6532a75255
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my second commit commit e63204faffe5e482c18f188ca1c690d961924846
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$
你会发现git commit 之后,节点序号改变了,所以,它不是在原来节点上的增加,而是把他们两个合在一块形成了一次新的节点
6)git commit "file" 默认的是全部提交
6)git log -2 表示的是显示log的最近两次提交
5.仓库状态跳转修改
1)get reset
zhangshuli@zhangshuli-MS-:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my second commit commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my frist commit
zhangshuli@zhangshuli-MS-:~/myGit$ git reset 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Unstaged changes after reset:
M aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: aaa.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
zhangshuli@zhangshuli-MS-:~/myGit$ vim aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$
它的意思是,从当前节点到指定节点,比较两者的差异,差异存在目录树
它类似于你在指定节点的基础上做了某些修改,现在是修改之后的样子
2)git reset --soft
zhangshuli@zhangshuli-MS-:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my second commit commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my frist commit
zhangshuli@zhangshuli-MS-:~/myGit$ git reset --soft 4c7cede87be37783ca4528fbdcd79bc08e3870fe
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: aaa.txt
#
zhangshuli@zhangshuli-MS-:~/myGit$ git diff --cahced .
error: invalid option: --cahced
zhangshuli@zhangshuli-MS-:~/myGit$ git diff --cached .
diff --git a/aaa.txt b/aaa.txt
index 3e66ffe..b973e7f
--- a/aaa.txt
+++ b/aaa.txt
@@ - +, @@
-my frist commit
+my second commit
+
zhangshuli@zhangshuli-MS-:~/myGit$ vim aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$
它跟不加参数的区别就是:它把差异放在了缓存区,相当于进行了一次commit
3)git reset --hard
zhangshuli@zhangshuli-MS-:~/myGit$ git log
commit f73968df521e48d23289c5563f7ac7fbc5937b57
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my second commit commit 4c7cede87be37783ca4528fbdcd79bc08e3870fe
Author: zhangshuli <zhangshuli@vanzotec.cn>
Date: Fri Feb :: + my frist commit
zhangshuli@zhangshuli-MS-:~/myGit$ git reset --hard 4c7cede87be37783ca4528fbdcd79bc08e3870fe
HEAD is now at 4c7cede my frist commit
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
nothing to commit (working directory clean)
zhangshuli@zhangshuli-MS-:~/myGit$ vim aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$
它比较暴力,是直接将当前状态,强制转化为了节点状态,不可恢复,除非你记住了转换之前的节点
上面说的是往前节点的跳转,如果往后其实也是一样的,如
git reset
zhangshuli@zhangshuli-MS-:~/myGit$ git reset f73968df521e48d23289c5563f7ac7fbc5937b57
Unstaged changes after reset:
M aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$ git st
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: aaa.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
zhangshuli@zhangshuli-MS-:~/myGit$ vim aaa.txt
zhangshuli@zhangshuli-MS-:~/myGit$
其实,它相当于,你在跳转节点的基础上(当时aaa.txt里面的值为second commit)当前aaa.txt的值是first commit。它相当于你把aaa.txt的值有原来的second commit ->first commit
从而我们可以知道,节点严格来说不分所谓的前后,因为它代表的仅仅是一个提交点,它是一种状态的id,通过它我们可以找到那个状态。当然,这样说是不严谨的,因为节点都是有父子节点关系的
git- 仓库创建、修改、提交、撤销的更多相关文章
- git常用命令速查:创建,修改提交,撤销,切换分支,合并分支,变基解决冲突
创建 $ git init #在当前目录下创建一个空的本地仓库 $ rm -rf .git #删除本地仓库 $ git add . #把当前目录下的所有文件添加到暂存区 $ git commi ...
- 2017-12-30-如何彻底清除现存GIT仓库的大量提交历史
layout: post title: 2017-12-30-如何彻底清除现存GIT仓库的大量提交历史 key: 20171230 tags: GIT 版本管理 问答 modify_date: 201 ...
- git仓库删除所有提交历史记录
stackoverflow原问题地址:http://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-g ...
- 【Git教程】如何清除git仓库的所有提交记录,成为一个新的干净仓库
一.引言 马三也算Github的忠实用户了,经常会把一些练手的项目传到Github上面进行备份.其中有一个名为ColaFramework的Unity框架项目,马三开发了一年多了,期间提交代码的时候在L ...
- Git仓库创建和文件提交
参考质料:廖雪峰的个人网站 Git 什么是Git:一个分布式版本管理系统: 作用:管理你的历史文件,文件修改历史,团队协作. Windows下安装Git: 到链接下载安装包,国内镜像 云盘 安装完成后 ...
- Git仓库创建---克隆仓库---初始提交代码
1.在Git上创建仓库,添加成员,默认分支是“master”,仓库路径假设为http://192.168.1.1/root/project.git 2.在sourceTree上,点击“克隆”,输入上面 ...
- Git应用详解第二讲:Git删除、修改、撤销操作
前言 前情提要:Git应用详解第一讲:Git分区,配置与日志 在第一讲中我们对Git进行了简单的入门介绍,相信聪明的你已经了解Git的基本使用了. 这一讲我们来进一步深入学习Git应用,着重介绍Git ...
- Git项目创建与提交
创建Git密钥: 1.生成密钥: 右键–>Git Bash Here:先输入ssh-keygen –t rsa –C "邮箱地址",注意ssh-keygen之间是没有空格的, ...
- git仓库创建及基本使用
创建git用户 useradd git passwd git 创建目录 mkdir /home/git/repos/app.git/ -p 初始化目录 cd /home/git/repos/app.g ...
- 合并两个git仓库并保留提交记录
case如下: 有2个git仓库:repo1.repo2: 想将repo1中的文件移入repo2: repo1的历史日志要保留: 1 2 # 1.将repo1作为远程仓库,添加到repo2中,设置 ...
随机推荐
- Python学习笔记(4)--数据结构之元组tuple
元组(tuple) 定义:tuple和list十分相似,但是tuple是不可变的,即不能修改tuple 初始化:(), ('a', ) , ('a', 'b') //当只有一个元素时,需加上逗号, ...
- Java基础学习总结(13)——流IO
一.JAVA流式输入/输出原理 流是用来读写数据的,java有一个类叫File,它封装的是文件的文件名,只是内存里面的一个对象,真正的文件是在硬盘上的一块空间,在这个文件里面存放着各种各样的数据,我们 ...
- Opencv Mat的三种常用类型简介
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/47683127 本文主要介绍Opencv ...
- 洛谷——P1970 花匠
https://www.luogu.org/problem/show?pid=1970 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走, ...
- Zookeeper简单概念介绍
过去,每个应用都是一个CPU.一个主机上的单一系统.然而今天,随着大数据和云计算时代的到来,不论什么相互独立的程序都可以运行在多个计算机上.然而面临的问题是,协调这些集群的系统比在单一主机上要复杂的多 ...
- centos7 keepalive双机热备~
简单实现Keepalive双击热备~ 摘要:准备两台虚拟机A:192.168.161.7 B:192.168.161.35 虚拟ip:192.168.161.10 keepalive进程 具体关于 ...
- codeforces Gym100589H Count Subarrays 树状数组/线段树+离散化
题意:给你一个数组,问你有多少子数组中的逆元数不小于K个,N<105 还在研究中
- request获取各种路径总结、页面跳转总结。
页面跳转总结 JSP中response.sendRedirect()与request.getRequestDispatcher().forward(request,response)这两个对象都可以使 ...
- 【Linux下权限控制之chmod与chown命令】
chmod 用于配置文件/目录权限 命名格式:chmod [选项] 文件/目录名 . 权限类别: r 读取 可用数字4表示 w 写入 可用数字2表示 x 执行 可用数字1表示 . 归属类别: u 属主 ...
- 实现IE下兼容CSS3的圆角效果
有些CSS3的牛逼的效果在IE下展示不出来是最烦人的啦,在项目中做的圆角效果到了IE下一堆方块....忒尴尬了...,找了个替代解决方案 1.首先下载一个js插件PIE.js百度一搜都是的,我也就不写 ...