I have a git repository in which I have made quite a few changes, and pushed to a remote location.
I wish to reset the repository to the 3 commit from the first, push it to the remote branch. I’m considering doing the following :-
- git reset
- git push origin master
A couple of doubts regarding the same?
- Would this be the right way to do it?
- I have deleted certain files and added a few files after the 3rd commit(from the first). I wish to change all that back to the state of the 3rd commit(get back the deleted files, remove the newly added ones). How could I go about doing that?
Thanks.
[EDIT]
I’m a solo developer on this project at this point in time. Also there are quite a few commits made so Id rather not manually have to revert each one of them.
As you already sent your files to a remote repository, it is not a good idea to perform a
git resetbecause other developers should updated their local repositories and get the new updates. The best solution is to perform agit revert, a command that generates a new commit that undo what a previous commit did. For more information, take a look at this link.But if you were the only developer of this project, you can delete the commits. For this, you can use the
git resetcommand:where hash is the commit hash that you want to return. The option
--hardis to almost complete remove the commits after the commit hash you passed.As you were changing the project history, you can’t perform a simple
push. You will need the flag -f to force the update:However, some remote repositories have some hook that prohibits you to force update the history. You will need to delete this hook to allow you to perform a history update. If you can’t do this, you will need to use the
git revertcommand.