1、你的head已经跑到 origin/master 前面了,想回到和远程库版本一致
Setting your branch to exactly match the remote branch can be done in two steps:
git fetch origin
git reset --hard origin/master ##将会保持到和远程库中版本一致
注意该命令需要提前保存copy 出来 所有修改之前的文件,否则之前保存的文件都将不存在.
如果因为某些原因你发现自己处在一个混乱的状态中然后只是想要重来一次,也可以运行 git reset --hard HEAD
回到之前的状态或其他你想要恢复的状态。 请牢记这会将清除工作目录中的所有内容,所以确保你不需要保存这里的任意改动。
2、另外如果你想让自己回到某一个commit 也可以使用git rest --hard
if your want drop any change from the head and not commit the change,you can use :
git reset --hard head ##remember every changes will not saved
Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand.
Say you have this, where C is your HEAD and (F) is the state of your files.
(F)
A-B-C
↑
master
You want to nuke commit C and never see it again. You do this:
git reset --hard HEAD~1
The result is:
(F)
A-B
↑
master
Now B is the HEAD. Because you used --hard
, your files are reset to their state at commit B.
3、撤销add
Undo add
$ edit (1) $ git add frotz.c filfre.c $ mailx (2) $ git reset (3) $ git pull git://info.example.com/ nitfol (4)
-
You are happily working on something, and find the changes in these files are in good order. You do not want to see them when you run "git diff", because you plan to work on other files and changes with these files are distracting.
-
Somebody asks you to pull, and the changes sounds worthy of merging.
-
However, you already dirtied the index (i.e. your index does not match the HEAD commit). But you know the pull you are going to make does not affect frotz.c or filfre.c, so you revert the index changes for these two files. Your changes in working tree remain there.
-
Then you can pull and merge, leaving frotz.c and filfre.c changes still in the working tree.
4、撤销一次commit and redo commit 并且保留原来的修改文件
Undo a commit and redo
$ git commit ... $ git reset --soft HEAD^ (1) $ edit (2) $ git commit -a -c ORIG_HEAD (3)
-
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".
-
Make corrections to working tree files.
-
"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
git commit 后面加上如下参数的作用
-C <commit>
--reuse-message=<commit>
Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.
-c <commit>
--reedit-message=<commit>
Like -C, but with -c the editor is invoked, so that the user can further edit the commit message.
参考博文https://git-scm.com/docs/git-reset
来源:oschina
链接:https://my.oschina.net/u/2308739/blog/711049