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中,设置 ...
随机推荐
- Unity ContextMenu特性
有时候我们需要在编辑器下,频繁的做一些操作,比如说在不同的位置创建物体,一个个的修改坐标显然有点繁琐 这时候ContextMenu就派上用处了 例:利用 LineRenderer 画圆,我们不可能一个 ...
- 仿分词统计的MapReduce 程序。
HDFS 数据格式 : 举例单条数据:02-26 08:01:56 [qtp512249001-42] INFO async-statistics - class com.spring.aop.S ...
- Android导入工程提示Invalid project description
在eclipse里导入的时候报错,提示 Invalid project description. 解决的方法: 在eclipse的workspace中,找到.metadata目录,依次打开------ ...
- PHP Apache shutdown unexpectedly启动错误解释及解决的方法
在学PHP的时候,偶然发现XAMPP窗体Apache的启动出现错误,出现下面的错误提示: 9:52:41 [Apache] Attempting to start Apache app... 9:5 ...
- 15.Intellij中配置jdk/tomcat/maven
转自:https://blog.csdn.net/u010414666/article/details/44465905 继上一篇安装好了Intellij之后,我们可以对Intellij,做一些简单的 ...
- 实现图片懒加载(lazyload)
对页面加载速度影响最大的就是图片,一张普通的图片可以达到几M的大小,而代码也许就只有几十KB.当页面图片很多时,页面的加载速度缓慢,几S钟内页面没有加载完成,也许会失去很多的用户. 所以,对于图片过多 ...
- C# DevExpress GridControl使用方法
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...
- Windows 一键关闭UAC、防火墙、IE配置脚本
有时候,在环境需求下,需要关闭windows防火墙,UAC,以及IE选项配置. 对不懂电脑来说是比较麻烦的,老是得教他们,关键还记不住…… so,以下脚本就可以解决这个问题 注:脚本 需要右键 以管理 ...
- linux下安装配置rabbitMQ
1.安装Erlang 由于RabbitMQ依赖Erlang, 所以需要先安装Erlang Erlang的安装方式大概有两种: 1.从Erlang Solution安装(推荐) # 添加erlang s ...
- tcpg通信
1.客户端 from socket import * def main(): # 创建套接字 tcp_socket = socket(AF_INET,SOCK_STREAM) # 链接服务端 ip = ...