安装完成之后,进行配置:
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
初始化一个Git仓库,使用git init命令。
添加文件到Git仓库,分两步:
使用命令git add filename,注意,可反复多次使用,添加多个文件;
git add . 或者git add *;
使用命令git commit -m “message”,完成。
在Git中我们用git log命令查看历史记录:
$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
如果嫌输出信息太多,看得眼花缭乱的,可以试试加上–pretty=oneline参数:
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
需要友情提示的是,你看到的一大串类似1094adb…的是commit id(版本号),和SVN不一样,Git的commit id不是1,2,3……递增的数字,而是一个SHA1计算出来的一个非常大的数字,用十六进制表示,而且你看到的commit id和我的肯定不一样,以你自己的为准。为什么commit id需要用这么一大串数字表示呢?因为Git是分布式的版本控制系统,后面我们还要研究多人在同一个版本库里工作,如果大家都用1,2,3……作为版本号,那肯定就冲突了。
在Git中,用HEAD表示当前版本,也就是最新的提交1094adb…(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD,上上一个版本就是HEAD,当然往上100个版本写100个比较容易数不过来,所以写成HEAD~100。
现在,我们要把当前版本append GPL回退到上一个版本add distributed,就可以使用git reset命令:
$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL
Git提供了一个命令git reflog用来记录你的每一次命令:
$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
回退撤销:
如果还没有add到暂存,我们用git status查看:
我们有git restore file进行回滚。
如果已经add到暂存区,我们有个git status查看:
我们用 git restore --staged file 撤销暂存区的提交,然后在利用git restore file进行回滚。
文件删除
加入文件已经commit 的前提:
或者直接用git rm
链接远程仓库
克隆
git clone https://github.com/fengmq/test.git