/git-workflow

Learning Github / Git workflow

MIT LicenseMIT

Git & GitHub

Learning Git / GitHub workflow

What is Git?

Git is a version control system, its a way for us to track changes in our code locally, manage a project locally and many other things. Git and GitHub are in no way connected to one another rather they are two separate tools. Git is a way to track, save and manage changes within your code and GitHub is a place where you can safely store your code as a backup. Git is a CLI tool while GitHub is a web application / collaborative space for developers.

What is GitHub?

GitHub is a place where you are able to store, share and collaborate with others on project and code. GitHub is free to use and is  one of many places where code can be stored online for free. We use GitHub to  store code as a backup, over time you will have many projects and code bases that will end up taking lots of space on your machine, its also a place where you can backup your code. Should your computer hard drive or SSD ever go corrupt your code is safe in GitHub.

NOTE: Other than GitHub there are other platforms like it such as GitLab, BitBucket ect. All of them server the same purpose with slight variations in what the platform offers as extra features. Through out our journey however we will stick to using GitHub, but feel free to use whatever you want!

Terminology

  • repository → The "directory" or folder that contains the code, but is tracked by Git
  • remote → A repository on GitHub (or on another machine / server)
  • branch → A split from the current branch, allows for new features without altering the functioning code in a code base
  • un-tracked → New file / directory that is not tracked by Git
  • tracked → Changes are being tracked by Git
  • staged → The file(s) is added but the changes have not been committed (saved) yet
  • commit → All changes are saved and a 'snapshot' is made of the current state of the code (on your local machine)
  • push → Push the changes (commits) to the origin (local repository) or remote repository
  • pull → Pull changes from a remote to your local repository (will pull the changes to the matching branch; main (remote) → main (local))

Setting up git for the first time

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

the above commands need to be ran once in order to use git properly. Be sure to replace "John Doe" with your name and the email "johndoe@example.com" with your email

Common Git commands

git clone <link>

clone / download a repo (project) onto your computer, in the current directory / folder you are in

EX: git clone [https://github.com/Machine-Academy/git-workflow.git](https://github.com/Machine-Academy/git-workflow.git)

git add .

add all changed files and folders, in the current folder you are in, into the staging area

ex: git add .

NOTE: . (dot) represents the current directory / folder that you are in.

git add fileName [file2, file3 ....]

add individual file(s) to the staging area, to be committed

git commit -m "commit message"

# Optionally for Git clout
git commit -m "commit type: general message

details about the commit here"

When you commit code, you need to create a message, these messages are very useful for your self and others to understand changes that were made in the code at the specific point in time. This is really useful for when you want to rollback changes you made in your project

git push origin <branch>

Push code back up to GitHub in the specified branch

ex: git push origin main OR git push origin dev

git checkout -b <branch name>

create and move into a new branch

ex: git checkout -b testing (create a new branch called testing an move into it)

git checkout <existing branch>

Change your working branch, to a already existing branch.

ex: git checkout dev (change to the existing branch named "dev") OR git checkout main (change to the existing branch named "main")

git log

Show the commit history of the repo (project)

git pull

pull new code down from remote repository (GitHub repo) into the current branch you are in.

man git

A Visual Aid

Visual of Git flow

Or another example with a cloud based service such as GitHub:

Visual of Git flow including a remote cloud repo

Videos

Web Resources