007 Add Source Control - CarrieKroutil/Reactivities GitHub Wiki
Check Git
- Open a new terminal in VSCode.
- Check the current state in the terminal:
git status. - If the result is
fatal: not a git repository (or any of the parent directories): .git- Then, perform the "Setup & Configure Git" steps below.
- To show the associated remote repositories and their stored name, like
origin:git remote -v
Setup & Configure Git
- Download and install Git - https://git-scm.com/.
- Use OS package manager. E.g. for macOS:
brew install git.
- Use OS package manager. E.g. for macOS:
- Check success:
git --version. - Inform git of the user to perform tasks:
git config --global user.name "NameHere"git config --global user.email "[email protected]"- Note: Use
globalto set the username and e-mail for every repository on your computer. Omit if just want to set it for the current repo.
- Note: Use
git config --global init.defaultBranch mainto set value away from possible default of master
Add Code to Local Repo
- Initialize a git repository with:
git init. - 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.
- The above command and others are visible by:
- Exclude things that should not be visible in a public repository like
appsettings.json. - Stage changes:
git add . - Commit changes to local repository:
git commit -m "message here"| Learn more here.
Create GitHub Repository
- Create a new repository: https://github.com/new.
- Run a command in the terminal to create a main branch:
git branch -M main - Add remote origin:
git remote add origin {https://github.com/url_to_new_repo_here}.git - 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:
- Stage changes:
git add .- Check what is included now:
git status
- Check what is included now:
- Commit changes to the local repository:
git commit -m "message here" - Push local repo changes to GitHub repo:
git push -u origin main
Alt, Pull Down Repo
- In terminal, open parent folder you want repo to be created in. E.g. \userName\code
- Run
git clone (paste url)
Git Branches & FAQs
-
git branchShows all your origin branches aka cloned branches -
git branch newbranchCreates a new branch -
git checkout -b newbranchCreates a new branch and switches to that branch immediately.- This is the same as
git branch newbranchfollowed bygit checkout newbranch.
- This is the same as
-
git checkout newbranchSwitches to said branch -
git pullUpdates origin branch with remote branch when no conflictsgit pull --rebaseInstead of a merge commit, this re-applies your local commits on top of the updated remote branch. Cleaner history.git fetch originthengit reset --hard origin/main # or your branch nameIf You Want to Discard Local Changes (Caution!)- This deletes all local changes—only use if you're sure you don't need them.
-
What is the difference between
git switchandgit checkout?git switchis a command introduced in Git version 2.23 to simplify the process of switching between branches. It separates the concerns ofgit checkoutinto two distinct commands:git switchfor changing branches andgit restorefor discarding changes in the working directory.- Remember to use
git switchfor changing branches andgit checkoutfor discarding changes in your working directory.