Skip to content

Instantly share code, notes, and snippets.

@connoro7
Created March 23, 2023 21:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save connoro7/0027a4ec63b57ac281f48c0a9a6266f7 to your computer and use it in GitHub Desktop.
Save connoro7/0027a4ec63b57ac281f48c0a9a6266f7 to your computer and use it in GitHub Desktop.
Git Fundamentals

Git: Getting Started and Fundamental Commands

Create a Github Account

https://github.com/join

Make your GitHub username something professional, your username will show up on resumes, your LinkedIn, etc

Install git

Open your terminal and type:

$ git -v

If git is not found, go here: https://git-scm.com/downloads

or for Windows: https://gitforwindows.org/

or for Linux: sudo apt install git-all

Add global config settings

Add your username to your global git config settings

$ git config —global -e 
# If you're unfamiliar with command line editors:
# type :q to quit without saving changes
# type :w to save changes
# type :wq to save and quit
# In case of emergency, ping someone in Slack ;) CLI editors are notoriously difficult to start using

If you aren't familiar with CLI editors, you can also add your credentials directly from the command line:

$ git config --global user.name "YOUR_USERNAME"
$ git config --global user.email "email@server.com"
$ git config --global --list # To check the info you just provided

Setting up SSH instead of HTTPS

About SSH w/ Git

Check for existing SSH keys

Generate new SSH key

Add a new SSH key to Github account

Verify SSH connection

More about why we use ed25519 "Elliptic Curve" AKA "Twisted Edwards Curve" cryptography

git init: Initialize git in current folder

$ git init # creates a hidden folder in the current directory called `.git`

For this case, we'll be using the following aliases:

REMOTE=origin
REPO=https://github.com/connoro7/ai-image-generator.git

or if you're using SSH: REPO=git@github.com:connoro7/ai-image-generator.git

$ git remote add $REMOTE $REPO
$ git remote -v
$ git fetch --all --prune -v
$ git pull $REMOTE master

Core Git Commands

git --help: Print help pages for desired command

$ git COMMAND -h
# OR 
$ git COMMAND --help

Example:

$ git log -h
# outputs:
usage: git log [<options>] [<revision-range>] [[--] <path>...]
  or: git show [<options>] <object>...

   -q, --quiet           suppress diff output
   --source              show source
   --use-mailmap         use mail map file
   --mailmap             alias of --use-mailmap
   --clear-decorations   clear all previously-defined decoration filters
   --decorate-refs <pattern>
                         only decorate refs that match <pattern>
   --decorate-refs-exclude <pattern>
                         do not decorate refs that match <pattern>
   --decorate[=...]      decorate options
   -L <range:file>       trace the evolution of line range <start>,<end> or function :<funcname> in <file>

git status: Check the status of all files in the local git-enabled directory

$ git status
# or with my preferred options:
$ git status -s -b   # -s for concise, -b for branch information

git log: View commit history of the current local git-enabled directory

# Try all of these out! Each set of options has their own uses and niche
$ git log # basic logging
$ git log --decorate --abbrev-commit --pretty=oneline -10 # basic logging, but more readable
$ git log --decorate --abbrev-commit --graph # tree-graph logging
$ git log --decorate --abbrev-commit --graph --pretty=oneline # tree-graph logging, but more readable

git fetch, merge, pull: How to Fetch & Merge OR Pull From a Remote Branch

In short fetch + merge === pull

In this case, we'll be fetching & merging / pulling from our remote master branch to our local working branch BRANCH-NAME

# To fetch, you can use:
$ git fetch --all --prune -v
# Normally when fetch, I just use:
$ git fetch origin master:master -v
# To merge changes from origin:master to your current branch BRANCH-NAME:
$ git checkout BRANCH-NAME
$ git merge master

# Usually I'll just use pull to do all of that work in one motion:
$ git checkout BRANCH-NAME
$ git pull origin master

git branch: How to Create a new branch

❗️ Avoid working in master - you can safely work in a separate branch to avoid committing directly to master, thereby raining havoc and hellfire upon your coworkers.

$ git branch BRANCH-NAME
$ git checkout BRANCH-NAME
$ git pull origin master

How to List all current branches (local and remote)

$ git branch -a

How to Delete a Local Branch

$ git branch -d BRANCH-NAME # Deletes a local branch. Safe to use, will not delete a branch with uncommitted changes
$ git branch -D BRANCH-NAME # Force-deletes a local branch. Unsafe to use, will delete a branch with uncommitted changes

git checkout: How to Move to Another Local Branch

$ git checkout BRANCH-NAME

git diff: How to See the Changes You've Made

$ git diff
$ git diff --staged # Only display changes for files that are currently staged for commit

git add: How to Add a file to staging area

# After you've made your changes...
$ git add testFile.js # adding a single file
# OR
$ git add --all # adding all files

git commit: How to Commit all files currently in staging

$ git commit -m "MESSAGE DESCRIBING WHAT YOU\'VE DONE" # Don't forget to escape special characters: \' \" \` etc. 

git reset: How to Uncommit Files You Just Committed

$ git reset HEAD~1
# Then make your changes and commit those files again

git push: How to Push all commits on HEAD to remote branch

$ git push origin BRANCH-NAME
# Click the link in the output text that pops up to create a PR (Pull Request)

Please do not push directly to master with git push origin master, this will break things Pushing to BRANCH-NAME allows us to keep PRs siloed and makes code reviews

Basic Workflow Summary

  1. Init repo

  2. fetch/merge or pull from origin:master

$ git pull origin master

  1. Create new local branch

$ git branch BRANCH-NAME

  1. Checkout new local branch

$ git checkout BRANCH-NAME

  1. Make your changes
// make changes to your code goes here
  1. Save your files like you would normally (Ctrl + S or Cmd + S)

  2. Check the status of your working directory

$ git status $ git diff

  1. Add changed files to staging

$ git add --all or $ git add FILENAME.js

  1. Commit staged files

$ git commit -m "COMMIT MESSAGE"

  1. Push staged files to origin:BRANCH-NAME

$ git push origin BRANCH-NAME

  1. Ctrl+Click/Cmd+Click the link in the push output message to create a PR (Pull Request) directly on Github

Additional Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment