创建版本库
$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit
pwd命令用于显示当前目录。在我的Mac上,这个仓库位于/Users/michael/learngit。
初始化仓库 git init
$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/
添加文件到仓库 git add
$ git add readme.txt
提交 git commit
$ git commit -m "wrote a readme file"
[master (root-commit) cb926e7] wrote a readme file
1 file changed, 2 insertions(+)
create mode 100644 readme.txt
-m
后面输入的是本次提交的说明
查看当前仓库状态 git status
$ 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 diff
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
Git is free software.
查看版本历史 git log
$ git log
commit 3628164fb26d48395383f8f31179f24e0882e1e0
Author: Michael Liao <askxuefeng@gmail.com>
Date: Tue Aug 20 15:11:49 2013 +0800
append GPL
commit ea34578d5496d7dd233c827ed32a8cd576c5ee85
Author: Michael Liao <askxuefeng@gmail.com>
Date: Tue Aug 20 14:53:12 2013 +0800
add distributed
commit cb926e7ea50ad11b8f9e909c05226233bf755030
Author: Michael Liao <askxuefeng@gmail.com>
Date: Mon Aug 19 17:51:55 2013 +0800
wrote a readme file
简化显示历史版本信息git log --pretty=oneline
$ git log --pretty=oneline
3628164fb26d48395383f8f31179f24e0882e1e0 append GPL
ea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributed
cb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file
回退版本git reset
$ git reset --hard HEAD^
HEAD is now at ea34578 add distributed
参数 | 含义 |
---|---|
HEAD | 当前版本 |
HEAD^ | 上一版本 |
HEAD^^ | 上上一个版本 |
HEAD~100 | 往上100个版本 |
回退到某一版本
$ git reset --hard 3628164
HEAD is now at 3628164 append GPL
查看git历史指令 git reflog
$ git reflog
ea34578 HEAD@{0}: reset: moving to HEAD^
3628164 HEAD@{1}: commit: append GPL
ea34578 HEAD@{2}: commit: add distributed
cb926e7 HEAD@{3}: commit (initial): wrote a readme file
撤销修改git checkout -- file
$ git checkout -- readme.txt
撤销有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
删除文件git rm file
$ rm test.txt
创建+切换分支git checkout -b name
$ git checkout -b dev
Switched to a new branch 'dev'
查看所有分支 git branch
$ git branch
* dev
master
切换分支 git checkout name
$ git checkout master
Switched to branch 'master'
合并分支 git merge name
$ git merge dev
Updating d17efd8..fec145a
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
删除分支 git branch -d name
$ git branch -d dev
Deleted branch dev (was fec145a).
强行丢弃没有合并的分支git branch -D name
$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 756d4af).
忽略已跟踪文件的改动
git update-index --assume-unchanged name