1、安装Git-2.16.2-64-bit.exe后,设置用户名,用户邮箱
#--global参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
2、创建版本库
命令:git init
版本库:版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
3、把文件添加到版本库
命令:git add 文件
解析:将文件添加到暂存区
命令:git commit -m "log description"
解析:将文件添加到本地库。
-m命令用于添加提交说明日志。
命令:git status
解析:查看当前的文件修改状态。
上面截图因为我们已经提交到本地仓库了,所以当前库中没有任何修改。
命令:git diff 文件
解析:查看指定文件差异。
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory) modified: readme.txt no changes added to commit (use "git add" and/or "git commit -a") lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git diff readme.txt
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory.
diff --git a/readme.txt b/readme.txt
index 9c69c9c..4b1c78e
--- a/readme.txt
+++ b/readme.txt
@@ -, +, @@
-GIt is a version control system.
+GIt is a distributed version control system.
Git is free software. lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$
4、版本回退
命令:git log [--pretty=oneline]
解析:显示从最近到最远的提交日志
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git log
commit adde96d057448c792343465df3aee108764defbd (HEAD -> master)
Author: lfy <@qq.com>
Date: Sat Aug :: + add GPL commit 31728b8d6ec5a40ed7eca72aca63533d8625b423
Author: lfy <@qq.com>
Date: Sat Aug :: + add distributed commit 586474b5bd16e377df3b657ffc1b35f663a69038
Author: lfy <@qq.com>
Date: Sat Aug :: + create readme.txt
在Git中,用HEAD
表示当前版本,上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100个版本写100个^
比较容易数不过来,所以写成HEAD~100。
现在,我们要把当前版本
append GPL
回退到上一个版本add distributed
,就可以使用git reset
命令
命令:git reset --hard commit_id
解析:
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git reset --hard HEAD^
HEAD is now at 31728b8 add distributed
再次查看log,发现我们最后一次提交的内容已经找不到了。
但是,只要当前窗口没有关闭(或者关闭了但你要记得commit id),还是可以再找回的,要输入之前查看的commit id。
(即adde96d057...)
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git log
commit 31728b8d6ec5a40ed7eca72aca63533d8625b423 (HEAD -> master)
Author: lfy <@qq.com>
Date: Sat Aug :: + add distributed commit 586474b5bd16e377df3b657ffc1b35f663a69038
Author: lfy <@qq.com>
Date: Sat Aug :: + create readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git reset --hard adde96
HEAD is now at adde96d add GPL lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ cat readme.txt
GIt is a distributed version control system.
Git is free software distributed under the GPL. lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git log
commit adde96d057448c792343465df3aee108764defbd (HEAD -> master)
Author: lfy <1220429263@qq.com>
Date: Sat Aug 24 21:08:03 2019 +0800 add GPL commit 31728b8d6ec5a40ed7eca72aca63533d8625b423
Author: lfy <@qq.com>
Date: Sat Aug :: + add distributed commit 586474b5bd16e377df3b657ffc1b35f663a69038
Author: lfy <@qq.com>
Date: Sat Aug :: + create readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$
命令:git reflog
解析:记录你的命令
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$ git reflog
adde96d (HEAD -> master) HEAD@{}: reset: moving to adde96
31728b8 HEAD@{}: reset: moving to HEAD^
adde96d (HEAD -> master) HEAD@{}: commit: add GPL
31728b8 HEAD@{}: commit: add distributed
586474b HEAD@{}: commit (initial): create readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master)
$
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)