Git and GitHub

Some of the material below comes from notes on An Intro to Git and GitHub for Beginners (Tutorial) by HubSpot. Another excellent source of information is Git Essentials by François Dupire.

Git Command — Quick Reference

Command Description
brew install commit Installs git (in a Mac Terminal window) using homebrew (package installer).
git branch --move [existing_branch_name] [new_branch_name] renames a branch. Often used to rename the default primary branch from "master" to "main" as follows: git branch --move master main
git branch shows a list of available branches with an * next to the current working branch.
git status lists files that have not been staged and committed.
git ls-tree --full-tree -r main shows a full list of all committed subdirectories and files.
git log lists past commits by date and their purpose. You can review the commits starting with the most recent at the top with: git log --reverse
git config shows your git username.
git config shows your git email address.

Summary of the Relation of Git to GitHub

"Git and GitHub are not the same thing. Git is an open-source, version control tool created in 2005 by developers working on the Linux operating system; GitHub [a cloud-based repository] is a company founded in 2008 that makes tools which integrate with git. You do not need GitHub to use git, but you cannot use GitHub without using git. There are many other alternatives to GitHub, such as GitLab, BitBucket, and "host-your-own" solutions such as gogs and gittea. All of these are referred to in git-speak as "remotes," and all are completely optional. You do not need to use a remote to use git, but it will make sharing your code with others easier." — An Intro to Git and GitHub for Beginners

Installation of Git and Creating an Account and Repository at GitHub

The best installation process for git depends on your operating system and other considerations. A good source for installation information is git's own website: 1.5 Getting Started — Installing Git. To find out if git is already installed on your computer, go to a command line tool and type:

git --version

The typical response on a computer which has git installed will be similar to the following:

git version 2.42.0

Create a Local Git Repository and Commit a New File


1. In Terminal, create or navigate to the directory you want to turn into a local git repository. (In my case, I created a directory at: /Users/[user_name]/Documents/webdev

2. Type the command: git init

You'll get a response similar to: "Initialized empty Git repository in /Users/[user_name]/Documents/webdev/.git/". If you look in the repository directory, you'll see a new folder named .git which contains several more folders and files that get uses to track filewa.

3. Add a file to the repository directory.

(Git will register that the file exits in the repo, but won't track its changes until you tell it so.)

4. In Terminal, type git status.

Git will respond with a message that includes "No commits yet" and "Untracked files:..."

Now a few concepts and terminology before the next step:

You can save the state of one or more files in what is known as a "commit," but first you have to put those files into the "staging environment" (formerly known as the "index").

5. Type git add [file_name]. For example, type "git add svg_test.html".

6. Type git status and you'll now see a message the includes "No commits yet" and "Changes to be committed... new file: [file_name]".

7. Type git commit -m "Your comment". You'll get a response that includes:

"1 file changed, 16 insertions(+)
create mode 10064 [file_name]"

The comment you add should contain a description of the new feature, bug fix, or other change the commit includes. It can be extremely helpful to other programmers (or yourself) who are trying to figure out why a change was made in future years.

Create a Repository Branch

There are times when you want to develop new pieces to a program without immediately interfering with the main program development. You can create a branch, track those changes separately, and later merge the new module back into the main program tracking. To create the branch, do the following:

1. Type git checkout -b [new_branch_name]

You'll get the response: "Switched to a new branch '[new_branch_name]'

2. Type git branch and you'll get a message confirming that the new branch was created similar to:

* [new_branch_name]

The asterisk (*) indicates that you're in that branch.

Note that adding files to staging and committing changes happens separately within each branch. If two files with the same name exists in both the main and a separate branch, the changes that committed in one branch will not be committed in the other.

Once a file is committed, it will not be listed in response to the git status command. The git status command only shows files that are new, changed, or staged for committing. [To see files that have been committed, you can use the git show command, but this command goes further than necessary if you just want to see a list of the files that are being tracked. It gives detailed information about the changes in the files committed, and even shows the content of the file. [That information ends with an "(END)" line (that I don't know how to get past, exit, or escape from yet.]

Rename the Main Branch

Many people find naming the primary branch "master" offensive, so it's a good practice (and consistent with GitHub's naming convention) to rename the primary branch "main." (You could also name it "primary," but that would be inconsistent with GitHub's default.)

To change the name, type: git branch --move master main (The same syntax applies to changing the name of any branch.) To verify the name has changed, again type: git branch and you'll see the list of your branches with main added. (An asterisk appears next to the current branch (the branch where the "add" and "commit" commands will be executed.)

Navigating from One Branch to Another

To move from one branch to another, simply type git switch [destination_branch_name] and type git branch to confirm the asterisk has moved to the branch where you want to work.

Connecting Git to GitHub


1. If you are not logged into GitHub yet, log in and a screen similar to the following will appear:

2. Click on the repository name you want to target with git. (My public GitHub repository is onareach/dev, so I click on that repo name on the left side of the window to open the following screen:

3. Click on the green "<> Code" button to open the following window, and note the URL (used in the next step):

4. Go to Terminal. Register the GitHub repo with git with the following command:

git remote add origin[github_username]/[repo_name].git

(In my case, the command was git remote add origin

The response in Terminal is "ev.git".

5. Confirm the connection to GitHub by typing the command: git remote -v

If the connection is established, you'll get a response similar to this one:

origin (fetch)
origin (push)

Getting a Personal Access Token (PAT)

Soon, we'll push a local git repository to a GitHub repository, but to do that, we need a GitHub personal access token (PAT). Here's how you get the PAT:

1. In the upper right corner of the GitHub window, click on your profile photo or avatar, and a flyout menu will appear:

2. Click on "Settings" (next to the "gear" icon) near the bottom of the menu and the following screen appears:

3. At the bottom of the menu on the left side of the window (scroll way down), you'll see the "<> Developer settings" link:

4. Click on that link, and the following screen will appear:

5. Expand the "Personal access tokens" link and select "Tokens (classic)" to get the following screen:

6. Click on the button (in the upper right corner) labeled "Generate new token"...

...and select "Generate new token (classic)" to open the following screen:

7. You need to type a short explanation for the purpose of the token. (I entered "uploading filing from git to GitHub".) I left the Expiration at the default of "30 days", clicked all of the boxes under "Select scopes" and clicked the green button labeled "Generate token". This opened the following screen:

(For security reasons, I have replaced the token shown above.)

8. Save the PAT in a secure location.