If you ran the command itself by mistake, in emacs editing mode you can use up-arrow to move to the last command, and edit it to a new command undoing the mistake. But note that not all commands can be cleanly undone, perhaps installing those packages installed others as dependencies or made other changes.
Depending on the exact commands (here package installation), there might be logs of what was done somewhere; using the logs you shold be able to figre out what was done (and thus how to undo it).
Get confortable editing the command line and rummaging in the history bash keeps. It is a work (and ocassionally life-) saver. And go into the habit of reading commands twice (thinking carefully what you want to do in between) before pressing ENTER if there is the slightest possibility of something destructive.