007 Add Source Control - CarrieKroutil/Reactivities GitHub Wiki

Check Git

  1. Open a new terminal in VSCode.
  2. Check the current state in the terminal: git status.
  3. If the result is fatal: not a git repository (or any of the parent directories): .git
    • Then, perform the "Setup & Configure Git" steps below.
  4. To show the associated remote repositories and their stored name, like origin: git remote -v

Setup & Configure Git

  1. Download and install Git - https://git-scm.com/.
    • Use OS package manager. E.g. for macOS: brew install git.
  2. Check success: git --version.
  3. Inform git of the user to perform tasks:
    • git config --global user.name "NameHere"
    • git config --global user.email "[email protected]"
      • Note: Use global to set the username and e-mail for every repository on your computer. Omit if just want to set it for the current repo.

Add Code to Local Repo

  1. Initialize a git repository with: git init.
  2. Add a gitignore for dotnet specifically to exclude files from being checked in: dotnet new gitignore.
    • The above command and others are visible by: dotnet new -l.
  3. Exclude things that should not be visible in a public repository like appsettings.json.
  4. Stage changes: git add .
  5. Commit changes to local repository: git commit -m "message here" | Learn more here.

Create GitHub Repository

  1. Create a new repository: https://github.com/new.
  2. Run a command in the terminal to create a main branch: git branch -M main
  3. Add remote origin: git remote add origin {https://github.com/url_to_new_repo_here}.git
  4. Push local repo to GitHub repo just created: git push -u origin main
    • Should prompt to open external URL to authenticate with GitHub on the first time only.

Add File - Commit Flow Steps

For more practice, add a README.md file at the root.

Commit to repo:

  1. Stage changes: git add .
    • Check what is included now: git status
  2. Commit changes to the local repository: git commit -m "message here"
  3. Push local repo changes to GitHub repo: git push -u origin main

Alt, Pull Down Repo

  1. In terminal, open parent folder you want repo to be created in. E.g. \userName\code
  2. Run git clone (paste url)

Git Branches & FAQs

  • git branch Shows all your origin branches aka cloned branches

  • git branch newbranch Creates a new branch

  • git checkout -b newbranch Creates a new branch and switches to that branch immediately.

    • This is the same as git branch newbranch followed by git checkout newbranch.
  • git checkout newbranch Switches to said branch

  • git pull Updates origin branch with remote branch

  • What is the difference between git switch and git checkout?

    • git switch is a command introduced in Git version 2.23 to simplify the process of switching between branches. It separates the concerns of git checkout into two distinct commands: git switch for changing branches and git restore for discarding changes in the working directory.
    • Remember to use git switch for changing branches and git checkout for discarding changes in your working directory.