We went through git
in class today. We covered basic commands and terminology. I'll review some of those commands in this post as well as some extra commands that you might need when working on projects.
How do I connect git to GitHub?
-
Make a new directory somewhere locally
mkdir new-project
-
cd
into that directory and run agit init
-
Add files and make code changes, these can be very basic for now
-
Add and commit these changes
git add . # or git add <filename> git commit -m "message" # careful that you use only single or double quotes
-
Change the default branch to
main
withgit branch -M main
-
Login to GitHub, create a new repo with the same name as your local project
-
Add the remote with
git remote add origin <remote-name>
-
Push to GitHub with
git push -u origin main
-
From here on out you can make changes, commit and push as your please
How do I use
git diff
in VSCode?
I find the output of git diff
difficult to sometimes parse therefore I opt to use the GUI equivalent of the command in VSCode. To access this command:
-
Open your project in VSCode
-
Navigate to View (it's in the menu bar) and click on SCM
-
Click on the file you wish to see the
git diff
of
How do I delete files from a git repository?
You might accidentally add a file to your source control (like a big video or image file) and push it to GitHub. To remove it from both git and GitHub you'll need to:
-
Delete the file from git
git rm --cached <filename>
-
Do a force push to GitHub
git push origin main --force
How do I revert back to a previous commit?
There are many different approaches to revert back to previous commits. The approach that I like to take is as follows:
-
Run a
git log
and copy the commit id of the commit you wish to roll back to, for example the commit id for this commitcommit 5792c92bbb0c026f46c2c6e8a993ec5c603f1165 Author: Harrison Malone <[email protected]> Date: Tue Feb 9 11:05:34 2021 +1100 once again changed style and added privacy policy
is <code style="word-break: break-word;">5792c92bbb0c026f46c2c6e8a993ec5c603f1165</code>.
-
Checkout to that commit and make a new branch
git checkout -b <branch-name> <commit-id>
-
In this branch you can do whatever it is you need to do, change your code, more commits etc
-
When you're happy with your code in this branch you can do a couple of things, if you wish to add the branch code back to main you could
git checkout main git merge <branch-name>
-
You may get some conflicts when you run this command, resolve these conflicts in VSCode by clicking on Accept Incoming Change
-
You'll then need to add and commit the conflict resolution to the main branch
Why did we do the SSH config?
We followed the GitHub SSH guide to ensure that we never have to enter our password over and over when connecting to GitHub. You'll be doing this potentially hundreds of times a day as a developer so this saves us a lot of time.
How do I exit out of vim?
vim is a command line text editor that ships default with macOS and some Linux distributions. We aren't going to teach you how to use vim but you might inadvertently find yourself in vim. For example if you try to make a commit without the -m and ""
git commit
you'll be thrown into vim. To exit out you vim you need to type :q
.