0

Note - I am new to understanding the git reset command, so I am not sure if it is the right command for what I want to do, which is very simple.

I wanted to undo the changes made in the last git push. So, I ran the following reset command to go back to the desired commit hash (one before the undesired commit hash)-

git reset <last desired commit hash> <file name>

After this, there were no changes to file as such (working directory). But, git status showed that the file had been modified.

 modified:   <filename>

On doing git stash now, the working directory file was modified. But, I do not see an option to push the changes to remote repository. Git pull also shows no changes.

Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • (1) It may not be the right command, at least, not as used. (2) Possible duplicates include http://stackoverflow.com/q/927358/1256452 and http://stackoverflow.com/q/2318777/1256452 (3) Changes aren't made in pushes; changes are made in the work-tree, and then committed as a new snapshot through `git add` and `git commit`. – torek Feb 08 '17 at 08:52
  • `git stash` will have stashed your changes, so they won't be available to push. Prior to the `git stash`, what were the results of the `diff` on the file? – Tom Feb 08 '17 at 09:17
  • duplicate of: https://stackoverflow.com/questions/1270514/undoing-a-git-push – ketan vijayvargiya Feb 08 '17 at 09:25
  • @thebluefox the `diff` on the working directory using a UI tool showed the desired version in the left hand side and the undesired version on right. I finally did the following after taking a fresh `pull` - git reset --hard git push -f This removed the undesired commit completely from the commit log. – Sandeepan Nath Feb 08 '17 at 10:00
  • @SandeepanNath you got the answer you expected?? :)) – Supun Wijerathne Feb 13 '17 at 05:44

1 Answers1

0

You can solve your doubts in your approach with this.

  • git reset <last desired commit hash> <file name>. This will restore your desired file(<file name>) into the version specified by <last desired commit>. And you will still be remained in the current branch.

  • If the file you specified had any difference between the version you had and the version you restored, soon after you executed previous command this kind of a text should appear. Unstaged changes after reset: M <file name>

  • You can verify it by the following command. git diff <file name>. Then it will show the change happened.

  • git stash will remove any un-staged commit. So obviously after git stashyou can't commit or push since there are no any changes. And git pull would not work either. Reason is your local branch will not go behind with commits by git reset. So it has all the commits in remote branch. So there's no new commits to be pulled by git pull.

  • You can try followings. Do git stash apply. So your file will again go to the state of last desired commit. Verify the changes by git diff <file name>. Then commit & push.

  • If your expected change was not there when you check the diff, the problem should be with some other thing, not version controlling. :))

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87