1、下载安装git
2、每个机器都必须自报家门
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
3、建仓库
$ cd learngit
$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/
4、把文件添加到版本库(可重复操作这两步,像这样,你不断对文件进行修改,然后不断提交修改到版本库里)
$ git add .(.代表全部文件)
$ git commit -m "wrote a readme file"
[master (root-commit) eaadf4e] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
5、用git log
命令查看“显示从最近到最远的提交日志”,如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline
参数:
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
commit id(版本号) :动作
6、回退到上一个版本,
$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
7、指定回到未来的某个版本
$ git reset --hard 1094a(版本号没必要写全,前几位就可以了,Git会自动去找。)
HEAD is now at 83b0afe append GPL
在Git中,总是有后悔药可以吃的
8、后悔药: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
又到了小结时间。
场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout -- file
。场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令
git reset HEAD <file>
,就回到了场景1,第二步按场景1操作。场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。