Github Version Control

Github is a distributed Version Control System that helps developers manage different versions of project files. In a distributed system, you can copy a complete repository (ie git, project) with its entire history to any machine making it easy to work remotely on projects from any physical location. Learn how to use Github and check out this cheat sheet.

Developers utilize Git via declarations that are delivered via the console which grants access to a repository. You can edit your files, then run Git declarations to store your changes. Each time you complete a change to a project file, Git records a snapshot of their current contents. These snapshots are known as commits which are pushed back to the repository and stored.

  1. Creating a Repository
  2. Staging Changes
  3. Managing Commited Files
  4. Working with Remote Repositories

 Creating a Repository

There are two ways to create a repository: You can Clone a repo from another machine, or you can Initialize a brand new repo. The process begins in the local host root directory.

Cloning an existing machine is really simple using the command line:

git clone <https://name-of-the-repository-link>

Initializing a repo is a more delicate process, but the demonstration below will suffice.

[Step 1] Initialize Repo
git init
[Step 2] Stage Files
git add filename.ext
[Step 3] Commit File
git commit -m + "Description"
[Step 4] User Credentials
git config --global + user.email + 'your@email.address'
(One-Time Process)
git config --global + user.name + 'userName'
[Step 5] Retrieve Remote
git remote add origin + https://github.com/ + UserName/projectName.git

Learn more about GithubAUTH & Configurations.

 Staging Changes

At this point, you have a repository properly configured and you simply want to save your work. Before saving your work, you run [Step 1] to ascertain what files are ready to be versioned.

[Step 1] Check Repo Status
git status
[Step 2] Stage File
git add filename.ext
[Step 3] Commit Files
git commit -m "Description"
[Step 4] Push Files to Repo
git push -u origin master

Include emojis when writing commits!

After performing these four steps, you will be promted to enter your credentials unless you use advanced features that allow you to configure your dev environment.

  Managing Committed Files

Below are declarations that may prove useful only after becoming comfortable using the console.

View Comparisons
git diff(--staged)
Remove a File
git rm filename.ext
Remove Multiple Files
git rm file1.ext file2.ext file3.ext
Rename a File
git mv "oldFilename.ext newFilname.ext"
Unstage a File
git reset HEAD^ filename.ext
Discard Recent Changes
git --checkout filename.ext
Undo Commited Changes
git log + git revert SHA ID#
(Copy & Paste the SHA ID# >> The first 5 Numbers are Required!)

When using the git diff(--staged) command, files that have been modified will display - while files that have been removed will display +.

The git log command produces a list of Commits along with their Simple Hashing Algorithm (SHA) identification number, which is a checksum of all changes in that commit. The checksum is the result of combining all the changes in the commit and feeding them to an algorithm that generates these 40-character strings. When you need to select a commit from your history, you can use these SHA-1 checksums, or "SHAs" for short, to identify which commit you want.

Fixing errors or mistakes can be a pain in the arse so it benefits developers to explore further: perhaps understanding resets would be beneficial.

 Working with Remote Repos

Using github for independent projects is slightly different when working with others. For starters, a repository that you own is Local whilst a one owned by another developer is known as a Remote. Each repo is a full copy of its original, including version history.

While developers can make commits to their own repo independent of others, a dev must always pull commits other developers have made from a remote to a local in order to maintain the most up-to-date version.

Clone a Repo
git clone + https://github.com/user/repo + newlocalFolder
Add Remote
git remote add origin + https://github.ProjectURL.git
View Remote
git remote (Default Remote Name is 'origin')
Update Repo
git pull origin (Add Updated Files to Directory)

 Branching

Collaborators typically use a declared central repository and gain access via cloning into their local machine. This allows developers to work on different components that can be added to the central repo by pushing commits. Simply add the repo by name to the folder on your local machine. In this regard, it is a best practice to conduct work outside of the master branch: enter, branching. This is the act of creating a temporary branch when creating a new web app feature. Then adding that feature to the master branch only after the code has been vetted. Keep in mind

Branches, for all intents and purposes, are like alternate timelines. You will know what branch you are on by looking for the branch with the 'star' next to it: this is the branch that is checked-out. Temporary branches are stored locally, not in the repository: in fact, commit history is totally separate. Thus, when refactoring or making a substantial change to a project, there are not disruptions. When edits are complete and the code gets the OK, it will be merged into the master branch for deployment.

View All Branches
git branch (--list)
Create a Branch
git branch + branch_feature
Push to Remote Repo
git push -u + remote + branch_feature
Switch to Feature Branch
git checkout + branch_feature
Switch to Master Branch
git checkout + master
Update Master Branch
git fetch (origin)
Merge Feature Branch
git merge + branch_feature
Resolve Merge Conflicts
git merge + -abort
Delete Feature Branch
git branch -d + branch_feature

Sometimes the process of merging file contents doesn't go so smoothly. When you change the same part of the file in both branches you're trying to merge together, Git won't be able to merge the changes automatically due to a conflict that must be manually corrected. Remember, head is a marker that points to your currently checked out commit. So the head section is the code from the branch that you're merging into. This line of equal signs divides the changes from the two branches. And the ending conflict resolution marker will be labeled with the name of the branch you're merging in.

The illustrations below highlights (a) master branch before adding branches; (b)new branch pointed toward same commit as master branch; and (c) new branch that has been committed and pointed toward the commit.

Master Branch
Feature Branch
Committed Feature Branch

A Remote Branch is like a local branch, but it points to the commit that a branch in a remote repo is at. You can update a local branch with commits from a remote repository by merging a remote branch into a local branch. The process can get complicated quickly: using a Git Flow would be wise! Learn more about git branching with this interactive tool.

Git has the ability to Tag specific points in history as being important and developers typically use this functionality to mark release points. A Lightweight Tag is a branch that doesn't change as it points to a specific commit, but Annotated Tags are stored as full objects in the Git database.

Create Lightweight Tag
git tag
Create Annotated Tag
git tag + -a -m + "msg"
Share Tag
git push + origin + tag
Share Multiple Tags
git push + origin + --tags
Checkout Tags
git checkout -b + branch + tag

A Tracking Branch is a local branch with a direct relationship to a remote branch.

View List of Branches
git branch -a
Create Track Branch
git checkout -b + local-branch + origin + tracking-branch
Specifiy Branch to Track
git chekout --track + origin + tracking-branch

The advantage to establishing an association between a local tracking branch with a remote branch is that we can use a simple command when that tracking branch is checked out. This process is similar to fetch and merge commits from a remote repo. Just keep in mind that git pull should be used for commits when working independently, but git fetch and git merge should be used when working on remote repositories or merging different remote branches into your local branch. Use this interactive tool to learn git branching.