All About Git: Power Up Your Programming With Version Control
10 min read • ––– views
The below 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!
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
- Install and setup git on your computer
- Create a GitHub account to store your work for free
- Learn and utilize several essential git commands
- Create, initialize, and push a repository
- Learn about git structure, branches, and merging
- Learn about a few useful situational commands
- Learn about collaborating using git and GitHub
- 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:
- Go to the official Git site git-scm.com/downloads.
- Click the OS link.
- Double-click to extract & launch the installer.
- 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:
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.
Command | Description |
---|---|
git init | Initialize 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 --amend | Allows you to edit a commit |
git commit --fixup [commitID] | Combine new changes with an existing commit |
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.
Command | Description |
---|---|
git remote add origin git@github.com:[username]/[repository-name].git | Add upstream repo to publish commits at (the remote repo) |
git push -u origin master | Push your changes to remote repository |
git pull | Synchronize local repo with remote repo |
git fetch | Checks 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].git | Set 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.
Command | Description |
---|---|
git clone ssh://git@github.com/[username]/[repository-name].git | Create a local copy of a remote repo using SSH |
git clone https://github.com/[username]/[repository-name].git | Create 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.
Command | Description |
---|---|
git branch -a | List 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 |
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).
Command | Description |
---|---|
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 --abort | Abort the current conflict resolution process, and attempt to reconstruct the pre-merge state |
git stash | Stash changes in a dirty working directory |
git stash clear | Remove all stashed entries |
The Nitty Gitty
Comparison, Status, & Inspection
Command | Description |
---|---|
git status | See details about the current branch |
git show | Shows 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.
Command | Description |
---|---|
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 |
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.
Command | Description |
---|---|
git clean -f | Removes 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~1 | Revert 1 commit (while keeping current local state) |
git push origin [branch] --force | Deletes 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.
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:
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:
Command | Description |
---|---|
git status | Gives us all the necessary information about the current branch |
git clone | Makes an identical copy of a repository and saves it to your computer |
git commit | The most-used command. Sets a checkpoint in your code history. Make sure you git add files first |
git branch | Creates a local checkpoint no longer in sync with the main history. Allows collaboration. Use git checkout to switch to a branch |
git push | Uploads your local changes to the remote repository |
git pull | Updates 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 PrintInPublic
Get my blog posts right in your inbox, plus a peek at what I'm working on! I love sharing tech articles and videos a curious person may find fascinating.
🎉 - subscribers