🫐 CommandsGit + GitHub

Git & GitHub

  • git init initializes a local git directory
  • rm -fr .git removes a local git directory
  • git status status of staging area
  • git log show log of changes
  • git diff filename.txt see recent changes made to code
  • git branch lists all branches
  • git checkout -b branchName creates a new branch and moves into it
  • git checkout branchName move into branch

Commit

  • git add <filename> add file to staging area
  • git add . add all files that aren’t currently staged
  • git reset removes all files from git status staging area
  • git commit -m “change message” commits files locally
  • git commit -m “title” -m “longer description” add a description of changes
  • git commit --amend -m "change message" amend a commit and change the commit message
  • git commit --amend --no-edit amend a commit without changing the commit message

How to write a good commit message

Local branches

  • git checkout main then git branch -D <branchname> delete local branch

Remote branches

Origins

  • git remote add origin <repository-url> add remote origin
  • git remote remove origin remove remote origin
  • git remote set-url origin url update remote origin
  • git remote -v view connected remote repositories

Pull and push

  • git pull origin main pull remote main branch into local branch
  • git push origin branchName push branch to the remote repo feature branch or
  • git push -u origin branchName push feature branch to the remote repo and set it as the upstream branch, then git push will push to that branch
  • git checkout -b new-branch origin/branchname checkout remote branch, good for testing PRs

Problems / Solutions

Wont push to a remote repo that has a README

git fetch --all
git rebase origin/main

Merge conflicts

git pull origin main
git checkout feature-branch
git rebase main
 
# fix conflict
git add .
git rebase --continue
# repeat until conflicts are resolved
 
git push origin feature-branch --force

Resources