Restoring old git push

I made some changes to my code and would like to undo them and get back to a previous version of my code, that I pushed. Is that possible?

1 Like

Yes, that is one of the applications git (a version control system) was invented for.

If you want to only look at a previous version:
You can use git checkout [commit] to switch to the specified commit.
Git will inform you on the command line what you do, and what/how you should do things.
For now, you should probably not edit files there without knowing what you are doing.
Go back to the newest version using git switch -.
git checkout can do a lot more things as detailed here.

Reset the history as if the newest commits never happened:
You can use git reset --hard [commit] to erase all history since commit. Not recommended here.
(--soft will keep the changes as local modifications)
Reference

Undo commits but keep the history:
git revert [commit] allows you to undo the specified commit in a new commit.
Use this to undo changes that introduced bugs. You can also give multiple commits.
Reference

Getting the files from a previous version:
To get the files from a previous version as if you had manually changed them back, you can do git checkout [commit] . (with the dot).
This will overwrite the files in the current directory with the old versions.
You will the changes in git status.

The last one is what you probably want to do here.
There are a ton more commands with the same and with different approaches to achieve the task but this should give you an overview.

4 Likes

What you want to do it you want to reset the server state to a previous version is

git checkout [commit] .
git add -A
git commit -m "Revert last few commits"
git push
1 Like

Do I need to know the exact message I wrote in this commit to call it back? Can I get a list of the names of my commits or how can I identify the one I need?

You obtain a list of all commits together with messages, timestamp, person, … using git log.
The long hexadecimal number is called the identifier or hash of the commit and is used to uniquely select one commit.
In the commands, we mean such a hash when writing [commit].

You should know the commit to which you want to revert.
Either you entered a good commit message that you remember or you remember the timestamp. Copy the hash from the output of git log for this commit.

grafik

Am I rightfully concerned to push this?
I found the commit hash I wanted and used git checkout with the hash as described and then did git add -A. Should I just commit and push it now?

It seems like you cloned the project inside your project folder.
This results in nested repositories that are hard to deal with and not what you intended.
git add -A says: Add everything to the staging area (the things you will push).
As there was a nested repository in your folder, git added it.
You should probably remove the nested repository with the git rm --cached project2 command (or this repo if the nested one is the correct one).

grafik

So there are still some things from the debugging listed. As far as I remember we shouldnt push those but I cant see them in any folder. When I git add -A , I would also add them I assume. Should I still do that and push?

The best way is to ensure there is nothing important in the directory using
git status
Then retrieve the old version of your committed files:
git checkout [commit] .
Check that the changes are there using
git status
Maybe check that the files are looking like expected manually or using git diff
Add the files you want to add with
git add [files]
Commit with a rememberable message:
git commit -m "Reverted to ... due to ..."
Push the new files if you want to do so:
git push

Johannes suggested git add -A as this is faster and simply adds everything which in general does not matter.
But in your case, it might be better to only add the files you want to in order to make sure everything works as expected.

grafik

I feel like I did it the way you said. I didnt include the checkout since I didnt want to show the hash. It cant be pushed tho, at least I am doing something wrong and I am very sorry for taking so much time but I am very lost right now.

Your repository is currently in a detached head state.
Git tells you so with HEAD detached at [commit-hash].
You probably missed the dot after the git checkout and switched to the commit back in history (first case in my first answer). The problem there is that you can not change things there as you are in the past and you should not/cannot rewrite the past.

The git commit failed as there was nothing new in the old commit you were viewing.
In the current state git status should also tell you that only untracked files (namely .vscode) are changed.
You can warp back to the present with git switch -. Afterward, the green text should again say master and you can try again with:
git checkout e38d591 . (including the dot)

Note: Nobody without the permissions to the repository can do anything with the commit hash.

It worked, thanks a lot for helping out.

In future, when posting a screenshot of some error, please always include the full command you ran, and (if possible) the commands you ran before. Otherwise, we are just guessing at what might have gone wrong, which does not really help solving your problem.

In particular, the precise arguments of your git checkout would have been interesting.