将文件从暂存区回退到工作区
$ git status
查看状态
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
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: app/src/main/res/values/colors.xml
no changes added to commit (use "git add" and/or "git commit -a")
将app/src/main/res/values/colors.xml
文件加入到暂存区
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git add app/src/main/res/values/colors.xml
$ git status
查看状态 app/src/main/res/values/colors.xml
文件已经加入到暂存区中
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: app/src/main/res/values/colors.xml
$ git reset app/src/main/res/values/colors.xml
将暂存区中的 app/src/main/res/values/colors.xml
文件回退到工作区
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git reset app/src/main/res/values/colors.xml
Unstaged changes after reset:
M app/src/main/res/values/colors.xml
$ git status
查看状态 app/src/main/res/values/colors.xml
文件已经被移除暂存区
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
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: app/src/main/res/values/colors.xml
no changes added to commit (use "git add" and/or "git commit -a")
下面是介绍回退一次commit
如果我们在开发中忘记了如何使用reset 命令来回退我们的提交,我们可以使用$ git reset -h
来查看如何使用:
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git reset -h
usage: git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]
or: git reset [-q] [<tree-ish>] [--] <paths>...
or: EXPERIMENTAL: git reset [-q] [--stdin [-z]] [<tree-ish>]
or: git reset --patch [<tree-ish>] [--] [<paths>...]
-q, --quiet be quiet, only report errors
--mixed reset HEAD and index
--soft reset only HEAD
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
--keep reset HEAD but keep local changes
-p, --patch select hunks interactively
-N, --intent-to-add record only the fact that removed paths will be added later
-z EXPERIMENTAL: paths are separated with NUL character
--stdin EXPERIMENTAL: read paths from <stdin>
上面给出了reset很多组合命令,在这里我们不一一去介绍,只介绍最常用的几个,其他自己尝试吧
'$ git reset HEAD^ '
回退版本,一个^表示一个版本,可以多个,另外也可以使用 git reset HEAD~n这种形式。
例如:$ git reset HEAD^
等价于 $ git reset HEAD~1
说明: 如果HEAD指针指向的是master分支,那么HEAD还可以换成master,如果知道特定的commit-id,那么还可以直接使用 git reset commit-id 如果不加参数,实际上使用的是默认的参数mixed,
下面我们来看一个完整的回退操作过程:
- 首先我们用
$ git log -3
查看一下commit 的log 信息
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git log -3
commit f0f35510218e744485220b9e091d19a3ea93bb00
Author: CFOX <cfox.ma@gmail.com>
Date: Sat Aug 12 17:08:58 2017 +0800
comiit reset
commit 74fdb535821265d3ef7b3e53feb1e9a0f1506efc
Author: cfox <cfox.ma@gmail.com>
Date: Thu Aug 10 22:28:49 2017 +0000
push tow
Signed-off-by: cfox <cfox.ma@gmail.com>
commit f8d35036cc6fb64b85bdd937e3143a37a9181acc
Author: cfox <cfox.ma@gmail.com>
Date: Thu Aug 10 22:25:57 2017 +0000
push one
Signed-off-by: cfox <cfox.ma@gmail.com>
在这里我用到了 $ git log -3
命令查看commit 的log信息,后面的 -3
是指定我们要查看最近提交的几条记录,如果后面不加查看log条数即会查出全部的log 信息。
- 使用
$ git reset 74fdb535821265d3ef7b3e53feb1e9a0f1506efc
命令回退一条消息,在这里我使用的是 commit-id的方式。
下面是执行效果:
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git reset 74fdb535821265d3ef7b3e53feb1e9a0f1506efc
Unstaged changes after reset:
M app/src/main/res/values/colors.xml
从打印的信息中我们可以看到,我们回退成功了。
注意....注意...注意, 重要的事说三遍!!!
从log 中看到我们获取了最近三次的commit 信息,在这里其他信息不看,我们只看commit-id.
commit f0f35510218e744485220b9e091d19a3ea93bb00
commit 74fdb535821265d3ef7b3e53feb1e9a0f1506efc
commit f8d35036cc6fb64b85bdd937e3143a37a9181acc
$ git reset 74fdb535821265d3ef7b3e53feb1e9a0f1506efc
上面是我们最近三次的commit-id ,下面是我们的git 命令。会发现我使用的并不是最后一次commit-id(f0f35510218e744485220b9e091d19a3ea93bb00) 的值,而是使用的前一次的commit-id的值(74fdb535821265d3ef7b3e53feb1e9a0f1506efc),为什么呢?这样理解,我们要使用reset ,是要将我们的提交回退到,是回退到指定的那一步,在这里我们要回退一步,回退到第二次提交的地方。
补充说明:使用reset 回退最近的一次提交,下面三中方式是等价的
$ git reset HEAD^
$ git reset HEAD~1
$ git reset 倒数第二次的commit-id
reset 的三种参数
-
soft 参数:git reset --soft HEAD~1 意为将版本库软回退1个版本,所谓软回退表示将本地版本库的头指针全部重置到指定版本,且将这次提交之后的所有变更都移动到暂存区
-
默认的mixed参数:git reset HEAD~1 意为将版本库回退1个版本,将本地版本库的头指针全部重置到指定版本,且会重置暂存区,即这次提交之后的所有变更都移动到未暂存阶段
-
hard参数:git reset --hard HEAD~1 意为将版本库回退1个版本,但是不仅仅是将本地版本库的头指针全部重置到指定版本,也会重置暂存区,并且会将工作区代码也回退到这个版本
下面我们来看一下 soft 的操作过程, hard 的操作和soft 的基本相同,就不贴代码了, mixed 是默认的,在上面已经有示例了,这里就不在重复了。
1. 查看一下文件状态
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
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: app/src/main/res/values/colors.xml
no changes added to commit (use "git add" and/or "git commit -a")
2. 我们把这个文件加到暂存区中
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git add app/src/main/res/values/colors.xml
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: app/src/main/res/values/colors.xml
3. commit 提交文件
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git commit -m "cmiit soft test"
[dev d6b9977] cmiit soft test
1 file changed, 2 insertions(+), 2 deletions(-)
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch and 'origin/dev' have diverged,
and have 1 and 3 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
-
$ git reset --soft
回退一次提交
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git log
commit 9a0ecfd7530a4d75b4a35eedf67b431f3cf2f086
Author: CFOX <cfox.ma@gmail.com>
Date: Sun Aug 13 23:32:56 2017 +0800
commit soft test
commit 12319206b8552dd27dbf7cf0673c082ac33a1c49
Author: CFOX <cfox.ma@gmail.com>
Date: Sat Aug 12 18:31:48 2017 +0800
commit one
Signed-off-by: CFOX <cfox.ma@gmail.com>
commit e4f191e7f1cb8684b76d865495d04e1af0671f48
Author: machao <machao14@baidu.com>
Date: Thu Jun 1 19:23:09 2017 +0800
NDK Learn Demo
commit 39a3019352db4ed18d6b9961bb1af9176294e9e0
Author: machao <machao14@baidu.com>
Date: Thu Jun 1 17:24:57 2017 +0800
NDK Learn Demo
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git reset --soft 12319206b8552dd27dbf7cf0673c082ac33a1c49
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is behind 'origin/dev' by 3 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: app/src/main/res/values/colors.xml
到这里我们可以看到 app/src/main/res/values/colors.xml
文件又回到暂存区里了。
网友评论