How to restore a modified file that was error from Git?

Asked

Viewed 286 times

0

Class, I committed (and then pushed) a file in git to where everything was fine. When I returned to continue the work, I made some changes that resulted in errors and bugs. This time I haven’t committed yet and I want to restore everything I did to the last commit because I got lost in the changes and I want to start from the last correct commit.

  • git reset --hard, maybe?

  • You want to discard the current changes and leave it the same in the last commit you pushed?

  • 1

    exact! I want to pick up where I left off in the last commit. what I did after was wrong and I can’t find the error.

  • Right! If any answer has solved your problem consider ticking as accepted.

2 answers

1


To rule out ALL what you did locally:

git reset --hard

To discard all changes to a specific file:

git checkout -- <arquivo>

To put in the pile everything you did and have possibility to recover later:

git stash

0

step 1: "hide" the modifications you haven’t committed yet.

git stash save <descrição>

step 2: reset the last commit of the branch taking its modifications to staging area.

git reset HEAD~

here (step 2) if you want, you can commit the modifications again or remove them at once with: git checkout -- < file>

step 3 (optional): recover the "hidden" modifications in step 1 and apply again under the code.

git stash list

list all stashs made

git stash apply stash@{numero-da-stash}

apply the selected stash under the code

when applying stash, conflicts may occur that must be resolved

WARNING

After you make the changes you want and make a next commit, when you try to push again, you will receive an error because the local tree will be different from the remote tree since you pushed and then reset the commit locally. To proceed with the push you can use:

git push --force

before giving this command make sure that your branch is organized with the commits you want, because this branch will replace the remote

Browser other questions tagged

You are not signed in. Login or sign up in order to post.