Interfacing with 2FA Protected GitHub Account Repos via Git on Ubuntu

The goal:

Follow this guide to enable command line git access to your two-factor authentication (2fa) protected GitHub account repositories!

Difficulty:

If you have some experience using the bash shell and/or navigating the Linux filesystem then this shouldn’t be too difficult. If you have no experience with either of those things or want a refresher then please follow the links embedded in this guide to read tutorials on relevant topics or programs.

Why do this?

  • Enabling two-factor authentication (2fa) for your GitHub account helps protect against security breaches / unauthorized access to your account. Generally you should use 2fa for every app or website or login provider that supports 2fa; note that 2fa isn’t perfect and won’t make you immune to security breach / unauthorized account access risks, but it helps.
  • Accessing a 2fa protected GitHub account via git in the terminal requires a few more steps than accessing a non-protected account.
  • Some gui applications for git such as GitHub Desktop or SourceTree (to name two such applications I’ve personally used before; there are other gui applications for git out there) make it easy to access a 2fa protected GitHub account, but they aren’t available on Linux. Plus, git doesn’t have too steep of a learning curve and offers great productivity and version-control benefits to even novice users, so learning how to use command line git is quite helpful for programmers, writers, and generally anyone who benefits from using professional version control systems. More on git here.

Who is this guide for?

Anyone interested in learning how to use git on Linux (in this case, the Ubuntu Linux distribution and GNU Bash) via command line (aka the terminal) to interface/interact with your GitHub repositories. Git isn’t just for programmers! I think it’s one of the best tools available for writers as well, plus anyone more generally who needs or wants the great convenience and quality of life improvement that comes from using a great version control system. More on using git for writing here and here.

Initial Steps:

  1. This guide assumes that you have a GitHub account, if you don’t have one, you can create one by going here.
  2. Enable 2fa for your GitHub account.
  3. Configuring GitHub account’s implementation of 2fa.
  4. If you want to access GitHub via the command line and connect to your repos via HTTPS, you need to create a personal access token that will be used instead of your password when authenticating via git.
    • Save this personal access token and keep it in a safe location because you only see it once!
  5. Create a new repo on GitHub.
  6. Make note of the name you chose for your repo and copy its URL. Usually a GitHub repo URL looks like: “https://github.com/*username*/*reponame*”

Onward to the Terminal!

  1. Open terminal and run the following command to check if git is installed on your system:
    ‘which git’

    • If you see some output such as “/usr/bin/git” then git is present though you may want to update it in case it’s an older version.
  2. If git isn’t installed on your system and you’re running Ubuntu Linux then open terminal and run the following commands to install git:
    ‘sudo apt-get update’
    ‘sudo apt install git’
  3. Configure git: Associate your GitHub username and email with your local git install via these terminal commands:
    ‘git config –global user.name yourusernamehere
    ‘git config –global user.email youremailaddresshere
  4. Clone your GitHub repo so that you have a local version of that repo on your computer.
    ‘git clone https://github.com/*username*/*reponame*’

    • Note: This will create a folder containing that repo within whatever your current working directory is. To check your current working directory enter this terminal command: ‘pwd’

Start Writing / Programming!

Whether you use a gui application or a terminal based editor is totally up to you and it doesn’t really matter so long as what you’re using helps you create whatever you want to create. Don’t be afraid to try many different applications / editors out as you craft things, some tools will work better for you than they work for others, each person is different. It’s fine to read recommendations and retrospectives on individuals’ and/or organizations’ experiences with various tools, in fact that can be quite helpful, but make sure you spend more time just trying things out and fooling around rather than trying to research the perfect tool for you to use when writing or programming (or for anything really): the reading and research can help point you in a good direction but you need hands on experiential data to truly know what works best for you, so go play with different tools. I’ll be using nano and staying in the terminal for the rest of this guide. If you aren’t familiar with nano and/or want a refresher on how to use it, here’s a tutorial on how to use nano.

  1. I cloned my GitHub repo while my current working directory was my user account’s home directory, thus the folder containing my repo is located at “/home/useraccountname/reponame“.
    • If you aren’t familiar with the structure of the Linux filesystem and/or navigating it, then I recommend checking out this guide and this guide.
  2. Navigate to your repo via terminal; in this case my repo is located directly in my home folder: ‘cd ~/reponame
  3. Create a file, any file! e.g. ‘nano helloworld’
  4. Now tell git what you’ve done so that git can create an index readable by GitHub containing the changes you made: ‘git add helloworld’
    • Note that you need to specify the path to your file when using the git add command. I didn’t have to because my working directory at time of entering that command was the directory containing the helloworld file I created.
  5. Once you’ve made all the changes you wanted and added them now you need to commit them to finalize those changes prior to uploading them to your GitHub repo; make sure to to leave a concise comment describing the changes you made. ‘git commit -m “created helloworld file and said hello”‘
  6. Push your local git repo changes to your corresponding GitHub repo online by running this command: ‘git push origin master’
    • If you’re curious about why that command pushed to the “master” branch, then I recommend reading this documentation on how git branching works.
  7. Congratulations! Refresh your GitHub repo webpage and you should see the changes you made and pushed appear on that page.

Git is incredibly powerful, but getting started is relatively easy and even using git at a novice level provides immediate benefits to your workflow!

Sources other than the ones linked above that I referenced while writing this post: