Git学习第一天
1、初始化设置
$ git config --global user.name "Your Name" $ git config --global user.email "email@example.com"
2.初始化仓库
$ git init
3.把文件添加到版本库
$ git add readme.txt
用命令
git commit
告诉git,把文件提交到仓库
$ git commit -m "wrote a readme file" [master (root-commit) ac45a5b] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt
-m "xxx" 后面输入的是本次提交的说明
4.查看仓库当前状态
$ 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")
通过
git status
命令查看仓库已经被修改过,但还没有准备提交,通过git diff
命令看看文件具体修改的信息
5.用git log
查看仓库的更改纪录
$ git log commit a418c239b0a71da59b249c2f7b32c2290f381af2 Author: anzhihe <anzhihe1218@gmail.com> Date: Sun May 29 18:31:11 2016 +0800 hehe commit 2784b3674f515f1ef48d595c14639bf0f819a4f7 Author: anzhihe <anzhihe1218@gmail.com> Date: Sun May 29 18:22:22 2016 +0800 add distributed commit ac45a5bb9fc9b8f47d6fc75c2bad941236b697ff Author: anzhihe <anzhihe1218@gmail.com> Date: Sun May 29 17:55:33 2016 +0800 wrote a readme file
加上
--pretty=oneline
参数,让更改纪录单行显示
$ git log --pretty=oneline a418c239b0a71da59b249c2f7b32c2290f381af2 hehe 2784b3674f515f1ef48d595c14639bf0f819a4f7 add distributed ac45a5bb9fc9b8f47d6fc75c2bad941236b697ff wrote a readme file
6.用git reset
命令回退到指定版本
$ git reset --hard HEAD^ HEAD is now at 2784b36 add distributed
看看readme.txt的内容是不是版本
add distributed:
$ cat readme.txt Git is a version control system. Git is free software Git is a distributed version control system
现在用git log看看版本库的状态
$ git log --pretty=oneline 2784b3674f515f1ef48d595c14639bf0f819a4f7 add distributed ac45a5bb9fc9b8f47d6fc75c2bad941236b697ff wrote a readme file
已经没有hehe那个版本,现在想把它找回来怎么办?
用
git reset --hard a418c
指定回滚的ID号就可以把它找回来
$ git reset --hard a418c HEAD is now at a418c23 hehe $ git log --pretty=oneline a418c239b0a71da59b249c2f7b32c2290f381af2 hehe 2784b3674f515f1ef48d595c14639bf0f819a4f7 add distributed ac45a5bb9fc9b8f47d6fc75c2bad941236b697ff wrote a readme file
HEAD是git内部指向当前版本的指针,当回退的时候他能很快的定位到你想要回退的版本
$ git reflog a418c23 HEAD@{0}: reset: moving to a418c 2784b36 HEAD@{1}: reset: moving to HEAD^ a418c23 HEAD@{2}: commit: hehe 2784b36 HEAD@{3}: commit: add distributed ac45a5b HEAD@{4}: commit (initial): wrote a readme file
现在我们可以开始愉快的git之旅啦 !
小结
git init
初始化仓库,git add <file>
添加文件到仓库,git commit -m "xxxx"
提交工作中我们可以用
git status
命令随时查看工作区的状态具体修改了哪些内容可用
git diff
查看HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id。
穿梭前,用
git log
可以查看提交历史,以便确定要回退到哪个版本。要重返未来,用
git reflog
查看命令历史,以便确定要回到未来的哪个版本。
整理自廖雪峰Git教程: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000