- Git: Getting Started and Fundamental Commands
- Create a Github Account
- Install git
- Add global config settings
- git init: Initialize git in current folder
- Core Git Commands
- git --help: Print help pages for desired command
- git status: Check the status of all files in the local git-enabled directory
- git log: View commit history of the current local git-enabled directory
- git fetch, merge, pull: How to Fetch & Merge OR Pull From a Remote Branch
- git branch: How to Create a new branch
- git checkout: How to Move to Another Local Branch
- git diff: How to See the Changes You've Made
- git add: How to Add a file to staging area
- git commit: How to Commit all files currently in staging
- git reset: How to Uncommit Files You Just Committed
- git push: How to Push all commits on HEAD to remote branch
- Basic Workflow Summary
- Additional Resources
Make your GitHub username something professional, your username will show up on resumes, your LinkedIn, etc
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 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
Add a new SSH key to Github account
More about why we use ed25519
"Elliptic Curve" AKA "Twisted Edwards Curve" cryptography
$ 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
$ 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
# or with my preferred options:
$ git status -s -b # -s for concise, -b for branch information
# 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
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
❗️ Avoid working in
master
- you can safely work in a separate branch to avoid committing directly tomaster
, thereby raining havoc and hellfire upon your coworkers.
$ git branch BRANCH-NAME
$ git checkout BRANCH-NAME
$ git pull origin master
$ git branch -a
$ 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 BRANCH-NAME
$ git diff
$ git diff --staged # Only display changes for files that are currently staged for commit
# After you've made your changes...
$ git add testFile.js # adding a single file
# OR
$ git add --all # adding all files
$ git commit -m "MESSAGE DESCRIBING WHAT YOU\'VE DONE" # Don't forget to escape special characters: \' \" \` etc.
$ git reset HEAD~1
# Then make your changes and commit those files again
$ 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
-
Init repo
-
fetch
/merge
orpull
fromorigin:master
$ git pull origin master
- Create new local branch
$ git branch BRANCH-NAME
- Checkout new local branch
$ git checkout BRANCH-NAME
- Make your changes
// make changes to your code goes here
-
Save your files like you would normally (
Ctrl + S
orCmd + S
) -
Check the status of your working directory
$ git status
$ git diff
- Add changed files to staging
$ git add --all
or $ git add FILENAME.js
- Commit staged files
$ git commit -m "COMMIT MESSAGE"
- Push staged files to origin:BRANCH-NAME
$ git push origin BRANCH-NAME
Ctrl+Click
/Cmd+Click
the link in thepush
output message to create aPR
(Pull Request) directly on Github
- Git SCM Git Documentation
- Oh Shit Git Break glass in case of emergency
- error: gpg failed to sign the data fatal: failed to write commit object Common SSH key error, I usually have to open this page every ~6 months
- Learn the Basics of Git in Under 10 Minutes
- Walkthrough of Basic Git Commands
- Merging Local Git Branches