Git 版本创建和回退
#是否安装好了git
git
#显示安装成功的内容
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
如果没有安装的话,看一下这个链接。
https://www.jianshu.com/p/7edb6b838a2e
电脑是Windows的话,可以看一下这个链接。
https://www.jianshu.com/p/414ccd423efc
#git是管理某个目录的代码,首先要进行初始化。
git init
#用下面的命令,你就会发现,又一个.git文件,是个隐藏文件
ls -all
创建版本分为两步,两个核心命令
git add <可以多个文件和目录>
git commit -m '说明信息,最好是有意义的信息,如版本的描述信息'
git add code.txt code2.txt
git commit -m 'version1'
#那么怎么知道提交还是没提交上去呢,可以用下面的命令
git log
#然后你就会得到下面的东西, 英文状态下,按Q退出log
commit 2c7861b05d6313b7f8dffa2efc4b6b470e7c526b (HEAD -> master)
Author: BossLee <lihaoyuan@192-168-1-2.tpgi.com.au>
Date: Sun Sep 23 23:48:14 2018 +1000
version4
#commit 后面的就是版本序列号,之后回退版本的时候会使用到。version4就是添加的版本信息。
版本回退
git reset --hard HEAD^
HEAD^ 和 HEAD~1都可以代表上一个版本。
HEAD~100代表前一百个版
git reset --head <版本号>
head是个指针,只想当前的版本
#查看操作记录
git reflog
#查看当前工作状态
git status
显示内容包括,未跟踪的文件,在工作区但是没有add的,跟踪的文件,add了,改变了,但是没有commit
#丢弃工作区的修改
git checkout -- code.txt
#取消暂存区的修改,回退到在工作区的状态
git reset HEAD code.txt
工作区
当前目录,你可以编辑文件,添加文件,删除文件都是工作区的。
版本库分为暂存区,和库
只add不commit就是在暂存区里面
commit之后就是在库里面,里面存放着你对之前版本的修改而不是复制粘贴。
重点:只对add到暂存区的文件来创建版本。
对比文件的不同
- 工作区的文件和版本库里面的文件进行对比
# head 和code.txt也就是当前版本和更改过后的文件比较
git diff head -- code.txt
#出现如下内容,-号代表head,+号代表更改后的文件,可以看出,更改后的文件比当前版本文件多来一句话
diff --git a/code.txt b/code.txt
index ca63af7..b7513d6 100644
--- a/code.txt
+++ b/code.txt
@@ -1,3 +1,4 @@
+sdf
this is test txt.
this is second test sentence
dsfsf
- 对比两个版本之间有什么不同
git diff 1 2 -- 文件名
git diff head head^ -- code.text
git diff head^ head -- code.text
1代表---
2代表+++
删除文件
删除文件之后想撤销这个改动,如下
rm code2.txt
git checkout -- code2.txt
#将删除提交到暂存区
git rm code2.txt
git commit -m 'delete code2.txt'
#当版本信息很多的时候,你只想显示简短的信息
git log -- pretty=oneline
#如下
43ba027ae0d5cf8c0a8865a850a10264608ff2dd (HEAD -> master) delete code2.txt
2c7861b05d6313b7f8dffa2efc4b6b470e7c526b version4
d38d87826688c34cb118cdd3bd56fa84b0c48d51 verson3
617654d03a77786b0b986b8570c434cd302042d5 version2
7fd03a1b158681b32e393107a35c2e9dd08374dc version1