Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverting the last action on git?

Tags:

git

github

I am just new to git and I pushed a file to the repository but it just overwrites and deleted every work on our groups' repository, how do I undo this. as fast as possible without anyone noticing.

When I try git revert it gives bad object.

step by step I used

clone <repsurl>

in the git bash than I add somefile to local directory and used the command

git add <somfile>

git commit -m "somecomment"

and finnaly

git push

I think my mistake was not using

git add .

any way at the end Only my files were in the rep.

like image 500
physshyp Avatar asked Feb 06 '26 03:02

physshyp


1 Answers

When you do git push, you don't overwrite files, you just add a version of the files. A picture of that version is a commit which is identified by a commit hash.

I highly recommend you read https://git-scm.com/docs. It's short, and it will answer a lot of questions for you.

So, to answer your question: how can I undo this? The question should be: how can I get the previous files back? As always, in git, there are many ways to achieve something. I won't post a git tutorial here, but I'll try to provide you with some basic commands which would allow you to do what you need. However, if you do not understand them fully, I insist, you should read the docs.

I will assume your working area is clean, that is, everything has been committed and pushed to your remote. Local and remote repositories are fully synced.

When executing git log, you can see all the commits you have done on your branch (and parent branches). Some useful variant: git push -10 --oneline. This shows the last 10 commits in a short version (one line per commit). For each commit, you will see the commit hash (the identifier of your commit). To check out to that commit, you can do:

git checkout <commit_hash>

After that, the version of your files are those corresponding to that commit. But, note that you are not on a branch anymore. If you want to stay on your branch, but reset your files to the version of a given commit, you can do:

git reset --hard <commit_hash>

Sometimes, it is useful to do a soft reset:

git reset --soft <commit_hash>

This resets your last commit to the one identified by <commit_hash>, but does not change the version of your files. Instead, those changes are in the staged area.

There are also some useful variants like:

git reset --soft HEAD~1

This last command does a soft reset to your previous commit (the commit before the one HEAD is pointing to).

like image 139
mfnx Avatar answered Feb 09 '26 07:02

mfnx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!