git reset & checkout & revert

来源:互联网 发布:淘宝网假货多吗 编辑:程序博客网 时间:2024/04/28 22:23

git-reset - Reset current HEAD to the specified state

git reset [-q] [<tree-ish>] [--] <paths>…​git reset (--patch | -p) [<tree-ish>] [--] [<paths>…​]git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]

对于整体
git reset的三个参数–soft, –mixed, –hard,mixed是默认参数,三个参数的控制范围如下图所示

参数 工作区 暂存区 仓库 soft no no yes mixed no yes yes hard yes yes yes
git reset # 将仓库区缓存区恢复到上一次提交的状态,就是把暂存区的内容清空,工作区不受影响,用来清空暂存区git reset HEAD # 同 git resetgit reset --soft HEAD^ # 工作区和暂存区不变,仓库索引向前回退一次git reset HEAD^ # 工作区不变,暂存区和仓库索引都会回退一次git reset --hard HEAD^ # 工作区暂存区仓库索引都回退到一次

对于文件
对于文件了来说reset的作用和对于整体来说有很大的不一样

git reset -- filename # 将文件从缓存区中撤销,而不影响工作区的git reset HEAD filename #同上 用HEAD指向想恢复的版本reset ID file.txt # 同上 用具体的ID指向想恢复的版本

git checkout

git-checkout - Switch branches or restore working tree files
将HEAD移到一个新的分支,然后更新工作目录。

git checkout [-q] [-f] [-m] [<branch>]git checkout [-q] [-f] [-m] --detach [<branch>]git checkout [-q] [-f] [-m] [--detach] <commit>git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>…​git checkout [<tree-ish>] [--] <pathspec>…​git checkout (-p|--patch) [<tree-ish>] [--] [<paths>…​]

对于整体

git checkout branch # 将HEAD移动到branch,注意git reset branch则会将本仓库也指向branch,HEAD不变。

对于文件

git checkout file_name # 取出当前指针指向的file,更改工作区(reset 更改的是缓存区)git checkout  ID file_name # 取出对应提交id的file

git revert

git-revert - Revert some existing commits

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…​git revert --continuegit revert --quitgit revert --abort

git revert 会将现在的工作区和回退commit的之前的文件进行合并

参考文献:
https://git-scm.com/docs/git-reset
http://www.cnblogs.com/craftor/archive/2012/11/04/2754140.html
https://segmentfault.com/a/1190000006185954
http://www.cnblogs.com/houpeiyong/p/5890748.html
revert:http://blog.csdn.net/caz28/article/details/43602879

原创粉丝点击