Is there a possibility to revert a committed file in Git? I've pushed a commit to GitHub and then I realized that there's a file which I didn't want to be pushed (I haven't finished the changes).
If you have a commit that has been pushed into the remote branch, you need to revert it. Reverting means undoing the changes by creating a new commit. If you added a line, this revert commit will remove the line.
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.
update: added safer method
check out the previous (unchanged) state of your file; notice the double dash
git checkout HEAD^ -- /path/to/file
commit it:
git commit -am "revert changes on this file, not finished with it yet"
push it, no force needed:
git push
get back to your unfinished work, again do (3 times arrow up):
git checkout HEAD^ -- /path/to/file
To modify the last commit of the repository HEAD, obfuscating your accidentally pushed work, while potentially running into a conflict with your colleague who may have pulled it already, and who will grow grey hair and lose lots of time trying to reconcile his local branch head with the central one:
To remove file change from last commit:
to revert the file to the state before the last commit, do:
git checkout HEAD^ /path/to/file
to update the last commit with the reverted file, do:
git commit --amend
to push the updated commit to the repo, do:
git push -f
Really, consider using the preferred method mentioned before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With