Skip to content

Git and GitHub Basics

Installation

Depending on your operating system, follow the instructions below to install Git. - macOS: Install Git using Homebrew if you haven't already. Then run:

brew install git
- Windows: Download and install Git from the official website. During installation, you can choose to use Git from the Command Prompt or Git Bash. - Linux: Install Git using your package manager. For example, on Ubuntu/Debian:
sudo apt-get install git

Git is a version control system a tool that helps you keep track of changes in your code (or any files, really). It lets you save "snapshots" of your project as you go, so you can: - Go back to earlier versions if something breaks. - See what changed and when. - Work together with others without overwriting each other’s work.

The Git Lifecycle

The git lifecycle starts in your working directory. This is where your files live (your project folder).

  1. You make edits to the files in this folder as normal.
  2. Staging Area – When you’re ready to save some of your changes, you stage them.
    • Command: git add <filename>
    • This tells Git, "I want to include this file in my next snapshot."
  3. Repository (Local) – When your changes are staged, you commit them.
    • Command: git commit -m "Your message here"
    • This creates a permanent snapshot of the staged files in your local Git history.
  4. Remote Repository (e.g., GitHub) – Finally, you push your commits to a shared online location (GitHub), so others can see and use them.
    • Command: git push
  5. When working with others, you’ll also want to pull new changes from the remote repo each time you open your project, so your local copy stays up to date:
    • Command: git pull

Summary of Common Commands

Action Command Description
Create a repo git init Start tracking files in a folder
Check status git status See what’s changed or staged
Stage changes git add <file> Mark files for the next commit
Commit git commit -m "message" Save a snapshot
Push git push Send your commits to GitHub
Pull git pull Get updates from GitHub

Once you understand this cycle - edit, add, commit, push/pull - you understand the core of Git.

Other features of Git

git is an extremely powerful tool with many more features beyond the basics covered here. Some additional features include branching and merging, resolving conflicts, viewing history, and reverting changes. As you become more comfortable with Git, exploring these advanced topics will help you leverage its full potential for version control and collaboration.

For a more comprehensive guide, refer to the Pro Git book.

For a starting point of my commonly used git commands, try out these shell aliases

alias ga='git add'
alias gl='git log'
alias glp='git log -p'
alias glog="git log --graph --pretty=format:'%Cred%h%Creset %an: %s - %Creset %C(yellow)%d%Creset %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"
alias glg='git log --oneline --graph --all --decorate --abbrev-commit'
alias gdf='git log --name-only --oneline'
alias gpu='git push origin HEAD'
alias gd='git diff'
alias gdc='git diff --cached'
alias gc='git commit'
alias gco='git checkout'
alias gb='git branch'
alias gs='git status'
alias gss='git status -sb'
alias gp='git pull'
alias gpr='git pull --rebase'
alias git-sync='git pull --rebase && git push'
alias gt='git log --no-walk --tags --pretty="%h %d %s" --decorate=full'
# show git branches by date modified
alias gbl="git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'"