PERSONAL ACCESS TOKENS ON GIT/GITHUB: A TALE OF A LESSON, MISTAKE, AND FUTURE QUEST
Tags: github, vscode, git, codenewbie
Published: 2021-08-20 04:42:45

Using Git/GitHub has always been a huge hole in my technical knowledge. Sure, I can add files to the staging area and make a basic commit, but anything beyond that has been beyond my comprehension.  I'd like to walk through a couple things I've learned recently, a big mistake I made, and highlight a concept that is still eluding me.  

##REPLACING GITHUB'S DEPRECATED PASSWORD AUTHENTICATION WITH PERSONAL ACCESS TOKEN AUTHENTICATION

A couple days ago, I sat down and did some work on a project.  When I got done making my changes, I made my commit.  When I went to push to my remote repository, I got a message in my command line that GitHub had removed password authentication and replaced it with the use of personal access tokens (read their statement about the change [here](https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/)).  So, let's walk through how to set up a personal access token on GitHub, remove the original remote, and how to add a new one with your token.  

1. Go to your GitHub account, click on your profile picture icon in the top right corner, scroll down and click on *Settings*.

2. On the left hand side of the screen, there is a menu bar.  Scroll down near the bottom of the menu, and click on *Developer Settings*.

3.  Once again, on the left side of the screen, there is a menu bar.  Click on *Personal access tokens*, and then click on *Generate new token*.

4. When the menu to create a token comes up, set your expiration date, set you permissions, and generate your token.

5. Copy your token down and **KEEP IT HANDY**! As the prompt says, you will not be able to see this again (I deleted the one I made for this example, so don't go trying to use it to add emojis to my codebases!).

Now, go back to your terminal.  First thing we need to do is remove the remote so we can add it back using the token.  In the terminal, run `git remote remove origin`.

Now, run 
```
git remote add origin https://<GITHUB_ACCESS_TOKEN>@github.com/<GITHUB_USERNAME>/<REPOSITORY_NAME>.git
```
and replace the `<PLACEHOLDERS>` with their respective value.  Then you should be free to run `git push origin main` with no problems.  And if you go back to the screen we saw in **Step 3**, you should see your new token paired with the repository, along with the expiration date and permissions.

##MY EMBARRASSING MISTAKE

So after I figure out how to do this, I go to repeat the process in other repositories.  Since I'm lazy, I scroll up to run the command with the access token I had just run.  I see some warning message in my terminal that I don't understand, so I brilliantly decide to use the `--force` flag and override and push to that remote.  After doing this a few times, I go to check something on my GitHub repo homepage, and I noticed I wasn't seeing any of the commits I had just made.  After some backtracking, I realize what happened - when I scrolled up to the *remote add* command with my token, I forgot to replace the `<REPOSITORY_NAME>` placeholder with the current repository name that I actually wanted to push to.  I go to the previous repository homepage on GitHub, and sure enough, there were all my commits.  I had basically just overwritten the entire codebase.

After a literal facepalm and a litany of swearing, I came up with a solution: go back to my local repo, and redo the push I had done earlier with the force flag.  I'm pretty sure this was not the proper way to do it, but it did fix the problem.  My GitHub remote repository reflected my local repo.  

So the lesson I learned is:  be very careful when re-using commands to modify them properly!

##NEXT GIT CONCEPT I NEED TO TACKLE

Later that day, I was going through some old projects, and needed to make a GitHub repo to push to for a local repo.  When I made the repo, I initialized it with a README.md, since I know I need to get better at project readme's.  **Note**: there is not a readme in my local repo.  When I go to set the GitHub repo as a remote, I can't push to it because of the file that is in the remote but not the local repo.  

I wound up using my newly signature `--force` flag trick and just making my README locally, but I know there is a more conventional way to do this.  Clone the repo? Use `git pull` or `git fetch`?
Let me know in the comments what you think!

If you liked this article, please give me a virtual like, a follow, share online, and if you **REALLY** liked it, feel free to 


[Buy Me A Coffee!](https://www.buymeacoffee.com/jeremydmarx)