Optional, but useful!
- Open Terminal and run:
git --version - If it returns something like
git version 2.37.1then you're good! - If not, install Git for Mac and restart VS Code
-
In the Terminal inside VS Code (or regular Terminal if you're in the project folder), run:
git initThis creates a
.gitfolder, which means your local Git repository is now active!
-
Create a
.gitignorefile in your project and add:# Ignore Python virtual environment my-venv-name/ # Ignore VS Code settings .vscode/ # Byte-compiled / cache files __pycache__/ *.pyc # Ignore system files .DS_StoreThis file tells GitHub what to ignore in your Project folder when commiting messages, perfoming merges, push/pull commands, etc. If you don't do this, you'll likely have thousands of commits that can overload these commands!
-
In Terminal, run the following:
git add . git commit -m "Initial commit"
- Now that Git is initialized, you'll see source control options in the VS Code sidebar:
- Click the Source Control icon* (third icon from the top or
⌃⇧G) - You'll see your project files listed as changes
- Type a message in the box (e.g., "Initial commit") and hit the checkmark ✔️ to commit
- Click the Source Control icon* (third icon from the top or
- Navigate to your project folder
- Run the following:
source my-venv-name/bin/activate
-
In Terminal, run the following:
git remote add origin https://github.com/you-username/your-repo-name.git git branch -M main git push -u origin main
You may be prompted to log into GitHub via your browser or VS Code. Do it... or else...
*Note: *Also, GitHub has required multifactor authetication. You may need to access GitHub first through SSH (secure shell protocol). Steps to do this are found here.
-
If you have a project folder, but it's not yet connected to GitHub do:
In Terminal, in VS Code, run the following:
git remote add origin htts://github.com/you-username/your-repo-name.git git pull origin mainMake sure your local branch tracks the remote branch:
git branch --set-upstream-to=origin/main main -
If you haven't set up the folder (AKA cloned the repository) do:
In Terminal, in VS Code, navigate to where you want the folder to go.
cd path/to/your/desired/folderClone your GitHub repo:
git clone https://github.com/your-username/your-repo-name.gitOpen the folder in VS Code:
cd your-repo-nameMake sure your local branch tracks the remote branch:
git branch --set-upstream-to=origin/main main
- Look it up, nerd (said lovingly). It's decently straigtforward: Go to GitHub
- 📝 Github began asking for a two-factor authentication, so you may need to link your GitHub with SSH instead of HTTPS if you have never used GitHub before. The steps for setting this up are found here - Generating a new SSH key
- See Option 6.1
-
Run these two commands (replacing the info with your own):
git config --global user.name "Firstname Lastname"git config --global user.email "your-email@example.com" -
⚠️ Note: Use the email associated with your GitHub account so GitHub links your commits to your profileFor example, if your GitHub email is
myemail@mail.com, run:git config --global user.name "myemail@mail.com"
-
In Terminal in your project folder, in the virtual environment, run:
git commit -m "Merging remote origin/main with local project" -
Then push everything from your local device (your computer) to your GitHub repo on the web:
git push -
If you want, you can pull everything from the web to your computer, just to be super certain everything is matching up:
git pull
You now have installed git for version control of your code, and have your own repository up and running with:
- git in VS Code
- VS Code synced with your external GitHub repository
- a .gitignore file that ensures the correct files get synced
- You can see changes by clicking on each file in the Source Control panel
- Use "..." > View > Commit History to see Git history
- Press
⌘ShiftPand typeGit:to see all available Git commands
If you need to reconcile divergent branches, you can do the following:
-
In terminal, run:
git statusIf you get the message "Your branch and 'origin/main' have diverged, ...", then you probably did a local commit before pulling the GitHub repo.
-
If you are willing to lose your local commits (changes, notes to self) then run:
git fetch origin git reset --hard origin/main
-
Run this to merge everything, and do a pull request:
git config pull.rebase false git pull -
If you want to set your git to merge discrepancies automatically in the future, run:
git config --global pull.rebase false
-
If you want your local commits to be on top of the remote commits (cleaner history) then run:
git config pull.rebase true git pull