This issue can be caused by multiple problems, but when I ran into this issue it was because my access token had expired but my SSH key was fine.
If you are having this issue and suspect that it may be caused by the token expiration, follow the steps below.
First, generate a new access token on github by following this very simple guide: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token and do not forget to copy the token because you cannot see it again.
Clone a repo to test pushes on and open that location in the terminal.
Run the command:
git remote -v
The output of this command will be the remote URL(s) associated with that particular repository. If there are entries there, you can remove them with the follow command:
git remote remove origin
Add a new remote URL built with the token, the username and the repository name by running the following command but substituting in your own values:
git remote add origin https://[PERSONALACCESSTOKEN]@github.com/[USERNAME]/[REPO].git
After you add that new remote URL you can try to push but you will get an error:
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
As described in the error, run the command:
git push --set-upstream origin main
and now the pushing will work. Happy coding!