git是一个流行的分布式版本控制工具,在本地存放版本库,在断网时可以将代码提交到本地,待有网时再把代码提交到远程库,而集中式的svn共用一个版本库,必须在网络连通的情况下才能工作。我使用的windows平台,安装过程略去。
- git的基本操作
mkdir mygit
cd mygit
初始化一个仓库:
$ git init
Initialized empty Git repository in D:/mygit/.git/
可以看到生成一个隐藏的.git目录,仓库就建好了。接下来在工作空间里增加一个readme.txt文件,add到版本库里:
$ git add readme.txt
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme.txt
提交一下,-m 后面是对本次操作的注释:
$ git commit -m "wrote a readme file"
[master (root-commit) 748b8cf] wrote a readme file
Committer: zdh <zhang@i11.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
使用git status 查看当前git本地工作区、暂存区中文件的修改状态
$ git status
On branch master
nothing to commit, working tree clean
为了知道这个过程发生了什么,先来了解一下git的原理:
git add将工作区的修改添加到暂存区,查看上面add命令的输出:
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
说明我们可以做的操作,要么commit到master,要么可使用 "git rm <file>" 来删除add到暂存区的文件,撤销掉add操作。git认为文件的新建,更新,删除都是一种修改。commit命令作用是将修改提交到master分支上,可以add多次,commit 一次,每次commit都会将暂存区所有的修改提交。
- 版本管理
$ git log
commit 127e1c9c59ca798ace9ca5954f935276039e91fe (HEAD -> master)
Author: zdh <zhangdonghui@ins111.com>
Date: Sun Mar 25 15:09:50 2018 +0800
455
commit d7283083464bfa82450e9ed1261f221d9b466459
Author: zdh <zhang@in11.com>
Date: Sun Mar 25 15:08:18 2018 +0800
2333
commit 748b8cf26e00a8294ad6ba26cf20b032c17de12b
Author: zdh <zhangdonghui@in111.com>
Date: Sun Mar 25 12:19:56 2018 +0800
来查看提交历史,commit后面的一串数字用来唯一标识此次提交,head是一个指向当前版本的指针,回退版本时可以使用commit id 的前几位来区分不同的版本:
$ git reset --hard d7283
HEAD is now at d728308 2333
此时本地文件已经回退到上一个版本了。
- 撤销修改
- 对本地文件修改的撤销
在工作区删除本地文件:
$ rm readme.txt
使用git status 查看当前git本地工作区、暂存区中文件的修改状态:
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme.txt
提示我们工作区的修改没有存储到暂存区,红色表示修改未进行add,我们可以做两件事,使用:
$ git rm readme.txt或 $ git add readme.txt
将修改add到暂存区,或者是使用下面的命令撤销掉工作区的修改,其实是用暂存区的文件进行覆盖:
$ git checkout readme.txt
此时,本地又有了该文件。使用git status 查看当前git本地工作区、暂存区中文件的修改状态:
On branch master
nothing to commit, working tree clean
工作区恢复到了删除前的状态。
2.对add/rm 操作的撤销
在暂存区将文件删除:
$ git rm readme.txt
rm 'readme.txt'
该命令执行后,同时也将本地文件删除了,使用git status 查看当前git本地工作区、暂存区中文件的修改状态:
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: readme.txt
注意到颜色为绿色,表明该修改已经被add到暂存区了,根据提示我们使用 “git reset head <file>”撤销此次修改:
$ git reset head readme.txt
$ git status
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: readme.txt
与一开始只删除工作区文件的输出相同,说明暂存区的rm操作撤销成功。
小结:对add/rm操作的撤销使用 git reset head <file>,对工作区的修改撤销使用git checkout <file>,对 commit过的东东撤销只好回退版本了。