git reset的用法

Posted -beyond

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git reset的用法相关的知识,希望对你有一定的参考价值。

git reset三个选项

  --mix,--hard,--soft

 

数据

  针对每个选项都是操作这个文件。

[[email protected] demo]# git init
Initialized empty Git repository in /root/demo/.git/
[[email protected] demo]# echo one >> a.txt
[[email protected] demo]# git add a.txt
[[email protected] demo]# git commit -m "first commit"
[master (root-commit) b7ee3a2] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt
[[email protected] demo]# echo two >> a.txt
[[email protected] demo]# git commit -am "second commit"
[master 92ae9c4] second commit
 1 file changed, 1 insertion(+)
[[email protected] demo]# echo three >> a.txt
[[email protected] demo]# git commit -am "third commit"
[master 0985eec] third commit
 1 file changed, 1 insertion(+)
[[email protected] demo]# echo four >> a.txt
[[email protected] demo]# git commit -am "four commit"
[master 5bd480c] four commit
 1 file changed, 1 insertion(+)
[[email protected] demo]# git show-branch --more=4    #查看四次提交记录
[master] four commit
[master^] third commit
[master~2] second commit
[master~3] first commit

  

git reset --mix

  在省略reset选项的时候,默认的就是使用--mix

[[email protected] demo]# git reset HEAD~2
Unstaged changes after reset:
M       a.txt
[[email protected] demo]# 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:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] demo]# cat a.txt
one
two
three
four
[[email protected] demo]#  git diff
diff --git a/a.txt b/a.txt
index 69f75fc..a4c0ca1 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,4 @@
 one
 two
+three
+four

  从运行结果可以看出来,--mix有以下特点:

  技术分享图片

  假设使用reset命令从版本D回到版本B,那么HEAD就会指向B。同时,从版本B到版本D之间做的文件修改并不会丢失,会保留版本D相对于B的diff。

 

git reset --hard

  

以上是关于git reset的用法的主要内容,如果未能解决你的问题,请参考以下文章

git reset总结

Git 学习深入理解git reset 命令

git reset 版本回退的三种用法总结

git reset --soft,--hard的区别

git reset本地常见操作

会用git的重要性,记工作中使用git reset 代码丢失的教训