All About Git: Power Up Your Programming with Version Control

10 min read––– views

Gource SwiftGrade

The above gif is a time-lapse of the git history for MyGrades, a grades app I built. By the end of this article, you'll be able to recognize all the concepts and structures it visualizes.

I presented a workshop on this topic twice at a Google DSC Event and at the hackathon I hosted, Hackapalooza!

All About Git YouTube

What is Git?

Git is one of the most commonly used version control systems.

Version control allows you to understand the history of a file and how it has progressed, such as Google Docs’ “Revision History.” It tracks changes you make to files and keeps a record of your work. It also lets you revert to earlier versions of your code if the need arises.

Git drastically improves collaboration, allowing multiple people to work in synchronization on the same source code.

Common misconception: GitHub is NOT Git. GitHub hosts projects that use git.


Outline

In this article, we'll

  1. Install and setup git on your computer
  2. Create a GitHub account to store your work for free
  3. Learn and utilize several essential git commands
  4. Create, initialize, and push a repository
  5. Learn about git structure, branches, and merging
  6. Learn about a few useful situational commands
  7. Learn about collaborating using git and GitHub
  8. Review and discuss additional ways to learn git

Gitting Started

Installing Git

Git comes installed by default on many systems. If you don’t already have it:

Windows:

  1. Go to the official Git site git-scm.com/downloads.
  2. Click the OS link.
  3. Double-click to extract & launch the installer.
  4. Go through the installer steps; the default is usually fine. Make sure you add git to your path.

MacOS: Open a terminal and run brew install git

Linux: You either already have Git, or you can figure it out yourself ;)

Setting Up GitHub

Go to the GitHub sign-up page github.com/join.

Create a unique username and password, and you’re set!

*I Recommend using 2Factor Authentication for additional security.


Git the Basics

Save checkpoints of your code and share them with others.

This fundamental function is what all of Git revolves around. Below is a depiction of a simple git tree:

Git Structure

Creating A New Repository

To create a new repository (a central location in which data is stored and managed) via the command line, run git init.

You can then add and commit files to the remote repository. (A commit, or "revision", is a named checkpoint that represents an individual set of changes to your code), which is like saving your changes with a unique ID (the "SHA" or "hash") that allows you to keep a record of what changes were made when and by who).

Imagine a commit as "save as" for a snapshot of your code.

CommandDescription
git initInitialize a local Git repository
git add .Add all files in the working directory to the staging area
git commit -m "[commit message]"Commit your changes
git commit --amendAllows you to edit a commit
git commit --fixup [commitID]Combine new changes with an existing commit
Best Commit Practices

Pushing and Pulling

Before uploading your code, you must first add the remote origin - where your code will be stored. Here, this means Github.

Pushing a branch sends your local changes to the remote repository, while pulling them grabs the code on the remote repository and synchronizes it with your local computer.

CommandDescription
git remote add origin git@github.com:[username]/[repository-name].gitAdd upstream repo to publish commits at (the remote repo)
git push -u origin masterPush your changes to remote repository
git pullSynchronize local repo with remote repo
git fetchChecks to see if there are any changes on the remote repo (does not pull changes)
git remote set-url origin ssh://git@github.com/[username]/[repository-name].gitSet a repo's origin branch to SSH

Gitting Existing Projects

You can clone (copy to your local machine) repositories with the above commands.

You can also visit the Github Website to clone or fork (create a copy of the original repository that remains on your GitHub account) existing repositories using their interface.

CommandDescription
git clone ssh://git@github.com/[username]/[repository-name].gitCreate a local copy of a remote repo using SSH
git clone https://github.com/[username]/[repository-name].gitCreate a local copy of a remote repo using HTTPS

Git Gud

Branching Out

When you create a branch, you are making a named checkpoint no longer in sync with the main history, which lets you make changes and test code without breaking things.

Branches are commonly used when testing new features or collaborating with others.

CommandDescription
git branch -aList all branches (local and remote)
git checkout -b [branch name]Create a new local branch and switch to it
git branch -d [branch name]Delete a local branch
git checkout [branch name]Switch to a branch
Branches Visualized

Merging and Stashing

A common scenario is the following: My friend and I make changes to a repository. When I git pull his changes, I end up with merge conflicts.

In other words, my local code and the code in the remote repository are incompatible, and I will need to go through and decide which version of the code (mine or his) to keep.

Git offers a few helpful commands for this issue: git merge (which does the above automatically and recursively), and git stash (which stores my local changes for later so I can merge successfully).

CommandDescription
git merge [branch name]Merge a branch into the active branch
git merge [source branch] [target branch]Merge a branch into a target branch
git merge --abortAbort the current conflict resolution process, and attempt to reconstruct the pre-merge state
git stashStash changes in a dirty working directory
git stash clearRemove all stashed entries
Merging

The Nitty Gitty

Comparison, Status, & Inspection

CommandDescription
git statusSee details about the current branch
git showShows changes in committed files
git log (--summary) (--oneline)View changes in commit history
git diff [source branch] [target branch>]Preview changes before merging

These commands allow us to quickly view details such as:

  • Is the current branch is up to date?
  • Is there is anything to commit, push or pull?
  • Are there are files staged, unstaged or untracked?
  • Are there are files created, modified or deleted?
  • What is the commit history?

Rebasing & Cherrypicking

Situations can arise where more powerful git commands are necessary. Through rebasing - reapplying commits on top of another base tip (similar to a merge, but without the merge commit) - and cherrypicking - applying changes from specific commit(s) to a different point in history, you can resolve more complex issues.

CommandDescription
git rebase [branch]Reapply commits on top of another base tip
git rebase -i [commitID]Reapply all commits from [commitID] forward
git cherry-pick [commitID]Apply the changes introduced by some existing commits
Rebasing and Cherrypicking

This article is a fantastic Introduction to Git Rebase if you're looking for a more in-depth discussion of that powerful command.

Gitting Dangerous

In some cases, extreme action is the easiest way to resolve git issues.

🚨 Changing your history may cause undesired side effects. You may lose data. Many of these commands cannot be undone. If you change your remote history, don't say I didn't warn you.

CommandDescription
git clean -fRemoves and deletes untracked files from the working tree
git reset [commitID]Reverts all commits after specified commit, while keeping local changes
git reset --hard [commitID]Reverts all history and changes back to the given commit
git reset HEAD~1Revert 1 commit (while keeping current local state)
git push origin [branch] --forceDeletes all your previous commits and pushes your current one

Collaborating with Git

One of the best parts of learning git is how it enables you to collaborate with other programmers. Here are a few concepts you'll encounter when using git and Github.

Pull Requests and Issues

A pull request is a place to compare and discuss differences introduced on a branch with reviews, comments, and integrated tests. It allows you to ensure the code is up to par before merging it with the main branch, and helps you collaborate with others.

You can create and fill in issues on your repositories to track progress and keep track of what you need to work on.

PRs and Issues

Using Git GUIs

Many programmers choose to use graphical user interfaces as ways to visualize their git history and to speed up their development process. It's also a way to avoid using the CLI, although I still recommend you start with the CLI to become more comfortable with many important git concepts.

Popular GUIs include:

Git GUIs

In Review

Git is a powerful programming utility you can take advantage of to keep track of your work and code with others.

The most useful git commands are:

CommandDescription
git statusGives us all the necessary information about the current branch
git cloneMakes an identical copy of a repository and saves it to your computer
git commitThe most-used command. Sets a checkpoint in your code history. Make sure you git add files first
git branchCreates a local checkpoint no longer in sync with the main history. Allows collaboration. Use git checkout to switch to a branch
git pushUploads your local changes to the remote repository
git pullUpdates your local codebase with any changes from the remote repository

With these commands are your newfound knowledge of git, you're ready to go code some awesome projects with version control!

Next Steps

If you're a student, make sure you take advantage of the GitHub student developer pack. It's free if you have a GitHub account, and comes with a whole host of awesome perks including GitHub Pro, GitKraken Pro, DigitalOcean and Heroku Credits, and more.

You can bookmark a "Git Cheat Sheet" I created for a workshop in 2021 at github.com/GoldinGuy/UltimateGitResource.

If you're looking for more ways to practice git, I recommend Git-It - an app teaching git visually and interactively, and this tool that lets you Visualize Git Under the Hood.

Thanks for reading :)

Join my newsletter "Musings of a Student Developer"

Get my blog posts right in your inbox, plus a peek at what I'm working on! I love sharing tech articles, videos, and anything else a curious person may find fascinating.

- subscribers – 3 issues