Git & GitHub Guide#
1. Installation & Initial Configuration#
1.1. Installation#
MacOS (Requires Homebrew)#
Linux (Debian/Ubuntu)#
Verify Version#
1.2. Global Configuration#
# Set Identity
git config --global user.name "Marc Ouellet"
git config --global user.email "marc.ouellet@gmail.com"
# Set Default Branch to match GitHub (main)
git config --global init.defaultBranch main
# Verify Settings
git config --global user.name
git config --global user.email
# Common Alias
git config --global alias.s "status"
2. Local Workflow#
2.1. Initialize & Stage#
Start Git in current folder#
Add filter for sensitive files#
Add files to staging#
git add . # All files
git add <filename> # Individual file
0.1. Commits & Version Control#
Create a version#
git commit -m 'Initial commit'
Amend the last commit#
git commit -m 'New message' --amend
Reset staging area (unstage)#
Discard changes in working directory#
History#
# List last 5 commits
git log --oneline -n 5
0.2. Restoring Versions#
# Checkout specific commit or file from commit
git checkout <commit-hash>
git checkout <commit-hash> <filename>
1. Branches & Merging#
1.1. Visualization#
# Pro-level branch graph
git log --oneline --graph --all --decorate
1.2. Branching Workflow#
Create and switch#
git branch FEATUREBRANCHNAME
git checkout FEATUREBRANCHNAME
Merge into main#
git checkout main
git merge FEATUREBRANCHNAME -m 'Merge feature'
2. GitHub Integration#
2.1. SSH Authentication#
# 1. Generate Key
ssh-keygen -t ed25519
# 2. Copy Public Key to GitHub (Settings > SSH Keys)
cat ~/.ssh/id_ed25519.pub
# 3. Test Connection
ssh -T git@github.com
2.2. Remote Repository Management#
# Link local to remote
git remote add origin git@github.com:marcoue/Scripts.git
git branch -M main
Pushing & Pulling#
git push -u origin main # First push
git push origin main # Subsequent pushes
git pull origin main # Pull updates
Cloning#
git clone git@github.com:marcoue/inuvik.git
Verify Remote#
## Check which remote servers (like GitHub, GitLab, or a private server) your local repository is connected to
git remote -v
#similar to git status but to check the remote vs the local version for safety
git fetch
## Save pull - Does a merge
git pull
## Full pull - Overwrites all local files
git reset --hard origin/main
## Check which remote servers (like GitHub, GitLab, or a private server) your local repository is connected to
git remote -v
2.3. Remote Branching & PRs#
Push specific branch#
git push origin FEATUREBRANCHNAME
Sync after GitHub merge#
git fetch
git checkout main
git pull origin main
3. Advanced & Recovery#
3.1. Reposition Head (Force push)#
git push origin HEAD --force
3.2. Hard Reset to previous version#