Week 7 - Getting familiar with Git and using VSCode for Git

Liaw Bei Le · May 3, 2021

GIT

Popular GIT sites: GitHub, GitLab, BitBucket

I am using GitHub. It’s quite easy to create an account!

Repository (Repo)
Git system stores and saves the files to a directory called repository, a.k.a. repo.

It is used to save the information about the project.

Branches:
You have a master / main branch, then there can be more branches from this branch when you or others make changes (e.g. comments, change variable names).

The branch can have more branches, the codes of the branches could be merged back to the main branch / other branches.

Overtime, there is different versions of codes which GitHub tracks.

Merge branches: Go to ‘Compare & Pull request’ button in GitHub to merge branches. Only the owner can merge pull request or give comments on pull request.

Using VSCode for GitHub

Local: Your computer
Remote: GitHub
You push from local to remote.
You pull from remote to local.
Sometimes, you need to pull changes from remote into local first, git pull origin master, its good practce to pull, to prevent conflicts of versions.

In the terminal of VSCode:

Initialize, Add to repo, Pull, Stage, Commit, Push to GIT

1) git init

  • Initialise py file in VSCode into git files

2) ls
to see list of files

3) git remote add origin https://(insert the origin’s code link which is from green ‘Code’ button in Github)

4) git pull origin branch_name
(e.g. git pull origin main)

  • Get code from the specified branch

5) git status
check/ see which file was modified

6) git add file_name
(e.g. git add README.md)
(e.g. git add .\calculator.py .\main.py)
(e.g. git add .\insertfilenamehere)

  • File changed but need to STAGE for commit
  • If file name has U icon beside it in VSCode, means untracked, need to add to stage for commit. When added, icon changes to A

7) git commit -m ‘Added a line / insert some comment’
m means message

8) git push origin branch_name
(e.g. git push origin master)

  • Push the modified code for the specified branch

9) git clone https://(insert the repo’s code link which is from green ‘Code’ button in Github)
takes a git repo and copies it into your local

10) git pull origin branch_name
(e.g. git pull origin master)

  • Pull the code for the specified branch from GIT to local
  • Should frequently pull from git into local first before making changes to files and pushing them, in case of file changes in git

11) git rm file_name
(e.g. git rm ._posts\2019-02-13)

  • Delete the file (or in this case, the post)

Steps to push from local to remote without a remote repo created yet:

After creating your local folder and files, in VSCode terminal:

  • git init
  • git add (your untracked local files)
  • git commit -m “first commit”
  • git branch -M main

Then, create a new repo in GitHub, don’t select README for now

  • git remote add origin https://github.com/liawbeile/repo_name.git
  • git push -u origin main

Cheatsheets

GIT cheatsheet

Twitter, Facebook