Docs
Dev
Git

Git cheat sheet

Create Repo

  • git init
  • git commit -am "first commit" | git add . && git commit -m "first commit"
  • git branch -M main
  • git remote add origin / git remote add upstream https://github.com/sampolgar/.../.git
  • git pull main --allow-unrelated-histories

commands

useful

  • git status
  • git remote -v
  • git rm -r --cached .
  • git add . && git commit -m "" && git push origin main
  • git commit -am "committing and pushing" && git push origin main

Fork and Branch

  1. Fork, clone the forked repo
  2. git remote -v should be the forked Repo
  3. git remote add upstream .... add the original repo
  4. git fetch upstream fetch the original repo | git pull upstream main | git push origin main
  5. git checkout -b new-branch-name create a new branch
  6. git commit -am"commit and add" && git push origin new-branch-name

branching

  • git branch | git branch -r
  • git branch new-feature
  • git checkout main
  • git checkout -b new-feature
  • git branch -d branch-name delete local branch, branch must be comitted
  • or git branch -D branch-name delete branch, doesn't need commit. Ensure I'm on the branch

Global Setup

  • git config --global init.default Branch main
  • git config --global user.email "sampolgar@gmail.com"
  • ls -la | see in the file structure
  • rm -rf .git/ | delete the git init file

Revert or Reset

  • git log --oneline --graph --author="sampolgar@gmail.com" | easily see the commit history
  • reset undoes commits, revert undoes commits and creates a new commit
  • git reset --hard HEAD~5 || git reset --hard hashofmerge_commit | reset the last commit, or
  • git reset --soft HEAD~1 || git reset --soft hasofmerge_commit | reset the last commit, but keep the changes
  • git revert HEAD || git revert HEAD~2 || git revert HEAD~4..HEAD~1 || git revert hasofmerge_commit -m parentNumber | revert

Revert a commit https://stackoverflow.com/questions/1895059/revert-to-a-commit-by-a-sha-hash-in-git/1895095#1895095 (opens in a new tab) Revert changes, git push origin -f closes a PR and reverts any changes => you can edit them and push a new PR https://stackoverflow.com/questions/32212783/how-to-remove-a-file-from-a-git-commit-that-has-been-pushed (opens in a new tab)

Revert a pushed commit

Update or sync a forked Repo here (opens in a new tab)

  • git remote add upstream https://github.com/whoever/whatever.git | Add the remote, call it "upstream":
  • git fetch upstream | Fetch all the branches of that remote into remote-tracking branches
  • git checkout main | make sure I'm on main
  • git rebase upstream/main | Rewrite your master branch so that any commits of yours that aren't already in upstream/main are replayed on top of that other branch: If you don't want to rewrite the history of your main branch, (for example because other people may have cloned it) then you should replace the last command with git merge upstream/main. However, for making further pull requests that are as clean as possible, it's probably better to rebase.
  • git push -f origin main | push the changes to my forked repo, use -f the first time after rebase

sam can you revert your last commit in the PR?

https://stackoverflow.com/questions/36168839/how-to-remove-commits-from-a-pull-request (opens in a new tab)

Removing commits that you want to delete

  • git checkout my-pull-request-branch
  • git rebase -i HEAD~n
  • pick commits to keep, drop commits to discard
  • git push --force

If you drop all commits in the PR it will close the PR

  • git rebase -i HEAD~n
  • swap the commit you want to remove to the bottom (most recent) position
  • git reset HEAD^ --soft
  • git push -f

this should remove commit from remote and keep commit on local

Unstash a few files

  • git stash pop | pop everything
  • git add | add what you want to keep
  • git stash --keep-index | stash the rest

NVM

  • source $(brew --prefix nvm)/nvm.sh
  • nvm use 18.15
  • nvm alias default 20