Git & GitHub

Git is a version control system (VCS) that tracks changes to projects.

GitHub is a web-based platform that uses Git and allows developers to store, share, and collaborate on code.

Git & GitHub Basics

  • git status - Shows the state of your working directory and staging area.
  • git diff - Shows the differences between two commits, branches, files, or the working directory and the staging area.
  • git log - Shows the history of commits in the current branch, along with their messages, authors, and dates.

Getting Information

git status # Shows the current state of the working directory and staging area (modified, staged, untracked files)

git diff file_name # Shows the changes in that file compared to the last committed version (or between working directory and staged version)

git log # Shows the commit history for the repository

Git & Local Repositories

  • git init - Creates or initializes a new local Git repository in the current directory.

Adding Git to a Local Repository

cd repository

git init

# Initialized empty Git repository in...

ls -a

# You should see the .git folder

Removing Git from a Local Repository

# If the repository is not connected to GitHub

cd new_repo

ls -a

# You should see the .git folder

rm -rf .git
# If the repository is connected to GitHub

cd new_repo

ls -a

# You should see the .git folder

git remote -v

# You should see a connection

rm -rf .git

git remote -v

# You should no longer see a connection
# You will need to delete the repository on GitHub.

Git & GitHub

  • git add - Adds changes in your working directory to the staging area, which is the temporary area where you can prepare your next commit.
  • git commit - Records the changes in the staging area as a new snapshot in the local repository, along with a message describing the changes.
  • git push - Uploads the local changes to the remote repository usually on a platform like GitHub or GitLab.
  • git clone - Copies an existing remote repository to your local machine.

Adding Repositories to GitHub

  1. Initialize Git in the local repository.
  2. Create an empty repository on GitHub with the exact same name as the local repository.
  3. Invite any partners to collaborate (if necessary).
cd repository

git add .
git commit -m "Initial commit"

git remote add origin git@github.com:grantdavis303/[name].git
git branch -M main
git push -u origin main

Forking and Cloning an Existing GitHub Repository

Forking a repository means creating your own copy on GitHub.

  1. Visit an existing repository.
  2. Fork the repository by clicking the Fork button near the top right side of the screen.
  3. Type in a name for the forked repository (most times it is the same name as the original), and click the green Create Fork button.

Cloning a repository means pulling down that repository to your own local machine so you can edit the files and push up any changes to the cloud.

  1. Visit an existing repository.
  2. Click the green <> Code button on the GitHub repository page. In the Local tab, click on SSH and copy the SSH code by clicking the two overlapping squares on the right hand side.
  3. Follow these steps in the terminal:
  4. git clone # Paste SSH code
    git remote -v
    
    origin git@github.com:grantdavis303/.....git (fetch)
    origin git@github.com:grantdavis303/.....git (fetch)

Branches

  • git checkout - Switches your working directory to a different branch or commit, discarding any uncommitted changes.
  • git branch - Lists, creates, renames, or deletes branches in your local repository. A branch is a pointer to a specific commit.
    • git branch - See current branch
    • git branch --all - See all branches
  • git pull - Downloads the latest commits from a remote repository and merges them with your local branch.
    • git pull --rebase - No merge commit. Moves your work to the top. Takes local commits and replays them on top of another branch.
    • git pull origin -branch-name - Immediately merges the specified branch into your current branch.
  • git merge - Creates merge commit. Combines histories. Combines the changes from one branch into another branch, creating a new commit so there are no conflicts.
  • git fetch --all - Contacts all configured remotes, downloads new commits, branches, and tags.

Checking Out Main & Branches

# Checking Out Main

git checkout main
git branch
# Checking Out a New Branch

git checkout -b [name of branch]
git branch
# Checking Out an Existing Branch (Locally)

git branch -a
git checkout [name of branch]
git branch
# Checking Out an Existing Branch (GitHub)

git branch -a
git fetch -a
git checkout [name of branch]
git pull

Updating Main & Branches

# Updating Main from GitHub

git checkout main
git pull
# Updating a Branch from Main

git pull origin main
# Updating a Branch from Another Branch

git pull origin [name of branch]

Fetching & Merging Remote Branches

git merge
git fetch --all
git pull origin SCRUM-10/new-feature
git fetch --all
git pull origin SCRUM-10/new-feature

Deleting Branches

# Deleting a Branch

git branch -D [name of branch]

Commits

Adding Commits

It is recommended to add commits on a feature basis, not a time basis. Add commits when a new piece of functionality has been added to the repository.

A good commit message is brief enough that it is easy to read, but detailed enough that it is easy to understand.

Tips for writing good commit messages:

  1. Avoid unnecessary capitalization
  2. Double check your spelling
  3. Don't end commit message summaries with punctuation
  4. Commit frequently to keep your messages succinct and to the point
# Adding Individual Files

git add [file name]
git commit -m "Adding a single file"
# Adding All Files

git add .
git commit -m "Adding all files"

Pushing Commits

# Pushing Commits on Main

git push origin main
# Pushing Commits on a Branch

git push origin [name of branch]

Reverting Commits

# Reverting to a Previous Commit (Soft)

git log

# Copy commit ID number

git reset --soft # Paste commit ID number
# Reverting to a Previous Commit (Hard)

git log

# Copy commit ID number

git reset --hard # Paste commit ID number

Pull Requests

Creating a Pull Request on GitHub

  1. Visit the repository page on GitHub.
  2. Click Compare & Pull Request.
  3. Change the title and add a description.
  4. Assign any reviewers to the Reviewers section.
  5. Assign yourself to the Assignees section.
  6. Click Create Pull Request.
  7. Navigate to the Files Changed tab and review all code, clicking the "Viewed" checkbox for each file.
git push origin [name of branch]

Merging a Pull Request on GitHub

  1. Visit the repository page on GitHub.
  2. Navigate to the Files Changed tab and review all code, clicking the "Viewed" checkbox for each file.
  3. Click Merge Pull Request.

.gitignore Files

Adding .gitignore to a Repository

touch .gitignore

Inside the .gitignore file, copy and paste:

# GitIgnore

# List all files and directories you wish to ignore.
# See more here: https://git-scm.com/docs/gitignore

/.env
/.env.local

/.DS_Store
**/.DS_Store

If there are already files in repository, get rid of them with:

git rm -r --cached '*.DS_Store'

git commit -m "Remove DS_Store Files"
git push origin main