GitHub Authentication and Basic Functionalities
2021-06-15

Recently, I share with colleagues my knowledge of Git/GitHub. I gave a presentation in a team meeting, which covers the authentication and the basic functionalities of GitHub.

The authentication setting of GitHub in Git is not as easy as that in the GitHub Desktop App. Most beginners are overwhelmed by large numbers of relevant posts. Therefore, I list the only necessary official references that you can follow step by step to set up the authentication. Moreover, from my perspectives, I list the basic functionalities of GitHub as well as the corresponding daily Git commands. Lastly, I share some rules of thumb for collaboration in GitHub.

Authentication

You must authenticate before you can access certain resources on GitHub. When you authenticate to GitHub, you supply or confirm credentials that are unique to you to prove that you are exactly who you declare to be. The most two popular ways to authenticate to GitHub are

  1. A Combination of Username and Personal Access Token, and
  2. The SSH Protocol

Username and Token

  1. Creating a personal access token: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token#creating-a-token

  2. Using a token on the command line: https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token#using-a-token-on-the-command-line

SSH

  1. Checking for existing SSH keys: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/checking-for-existing-ssh-keys
  2. Generating a new SSH key and adding it to the ssh-agent: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  3. Adding a new SSH key to your GitHub account: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account

With SSH keys, you can connect to GitHub without supplying your username and personal access token at each visit.

Basic Functionalities

Adding an existing project to GitHub

https://docs.github.com/en/github/importing-your-projects-to-github/importing-source-code-to-github/adding-an-existing-project-to-github-using-the-command-line

Cloning a repository

https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository-from-github/cloning-a-repository

Branch: Checkout/Branch

  1. Create a new branch using git branch <branch_name>.
  2. Switch to the new branch using git checkout <branch_name>.

Execute two command lines in one: git checkout -b <branch_name>

Adding a file to a repository

https://docs.github.com/en/github/managing-files-in-a-repository/managing-files-using-the-command-line/adding-a-file-to-a-repository-using-the-command-line

Remove files from the working directory

  1. Remove files (e.g. trash.txt) from local directory manually.
  2. git rm trash.txt in Git Bash.
  3. Commit the change using git commit -m "Remove trash.txt".

Pull and Push

  1. Check the target remote repository on GitHub using git remote -v.
  2. Pull the latest changes from GitHub with git pull.
  3. Push your work to GitHub with git push origin <branch_name>" where origin is referred to as the target remote repository.

Merge

Merge code changes in two different branches (target branch and to be merged branch)

  1. Checkout the target branch using git checkout <target_bread>.
  2. Merge the changes from to-be-merged-branch into the target branch using git merge <to_be_merged_branch>.

Revert and Reset

git revert <commit_ID> will undo the changes in a particular commit.

  • git reset

  • git commit --amend

Best Practices for Collaboration

  1. Always synchronize your branches before starting any work on your own.
  2. Make changes that are self-contained.
  3. Avoid having very large changes that modify a lot of different things. (Try to make changes as small as possible as long as they're self-contained.)
  4. When working on a big change, it makes sense to have a separate feature branch.
  5. Regularly merge changes made on the master branch back onto the feature branch.
  6. Have the latest version of the project in the master branch, and the stable version of the project on a separate branch.
  7. You shouldn't rebase changes that have been pushed to remote repos.
  8. Write commit messages.
  9. Only edit the same branch when necessary after submitting a pull request.