Git reset not working

老子叫甜甜 提交于 2019-12-12 08:52:56

问题


I made a commit, pulled and merged some changes, and then made a second commit. When I wanted to go back to the first commit, I ran the command

git reset --hard <sha hash>

While the response was "HEAD is now at <sha hash>", my code looks just as it did before I ran that command. Usually, it changes to what I had before, but it looks like something isn't working correctly. Do I need to run a different command to unmerge before resetting head?


Extra info

When I run git status it says:

app/assets/images/.DS_Store.orig is untracked

and I can add it.

According to git reflog, I pulled before I made the commit hash1 (which I consider "before merge"). There is an sha hash2 for the pull (which git log did not show). When I dig hash1 and hash2, I see the changes I made and could reconstruct my original code from this. Still, this seems very strange. If I try to git reset to either of them, I cannot get my code from before the merge.


回答1:


I am sure the sha1 value you put in 'git reset --hard ' is the HEAD. If you want to reset the HEAD to the first commit,you should run command like this:

git reset --hard <first commit's sha1 value>

or:

git reset --hard <HEAD~1>  

this site maybe help you to know how 'git reset' work:git-reset




回答2:


I do not use the command line for reset since gitk (branch viewer) can do this. Sometimes reset does not work. I then delete all directories, leaving only .git folder. It often happens that some working files cannot be deleted. This is the problem why git cannot reset. You must close the programs the keep the files.




回答3:


The:

git reset --hard <sha-hash>

won't work when:

  • you're trying to reset untracked files (they're not part of your git repository),
  • some attributes are overridden by your git normalization file (.gitattributes),
  • you're using wrong hash or the hash is after your changes.

Maybe the solution would be to go back what you have on remote branch by:

git reset origin/master --hard

and rebase hash on top of it or git cherry-pick the commits which you really need.

If above doesn't help, you should also:

  • make sure your testing it on the right branch (git branch -a), not on detached one,
  • check for git reflog for more details about your history,
  • run git blame some/file to show at what revision your line of code was modified, this helps you to track the problem at which commit your code looks like you would not expect,
  • in case you don't like specific commit which was made before, you can revert it.



回答4:


I think you should enter the first 7 characters of SHA of the second commit, not the first one. For example, you have 2 commits:

commit 212323b232g2h3jj232j323kkk23 Author: Date: ...11:40 PM ...

commit abcdefb232g2h3kk239j183kkk24 Author: Date: ...10:00 PM ...

IF you want to reset the first commit you should enter: git reset abcdefb



来源:https://stackoverflow.com/questions/15540862/git-reset-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!