Imagine you are designing a logo.
You start out with the main image file you are designing.
Let's say you reach a point in your design where
you want to try out something new
without impacting the old file.
So you save a copy.
And another copy
when you make a new change.
And another copy.
This can quickly become a tangle.
Image credit: Peter Raymond
Number of changes: 3
Number of files: 4
Number of changes: 3
Number of files: 1
Change 1 - Add fuzzy eyes
Change 2 - Make logo green and adjust feet
Change 3 - Make logo turquoise
When you are working on a project (a site, application, logo, book), it's messy and hard to keep track of multiple files and multiple folders. Especially if you are making copies of the same files with small tweaks.
Version control monitors the changes you make in a project and keeps record of your changes.
That way, instead of saving multiple file copies, you instead are saving your changes and making record of why you made those changes.
Okay, I might see why this is important. How do we proceed?
Choose the appropriate installation for your computer.
Using Git on the command line
in the long-term will be easier for you.
The command line is a friendly* and useful tool for a developer.
But for many it's a bit* scary.
Some of its names: Git Shell/ Git Bash/ Command Line
* = The command line's friendliness may be questionable, but it doesn't have to be scary.
Don't worry, my coding happy friend. Let's introduce some command line concepts that will be of help.
Change directory - useful for moving from folder to folder.
List - lets you see the contents of a folder.
Makes a directory/ new folder.
Image Source: Tomb Raider Chronicles
ls- Find out where you are.
This lists the files in your current folder.
// user (Your current location) ls
cd- Change directories.
This lets you move from folder to folder.
Move to your Documents folder.
// user cd Documents // user/Documents
Jump multiple folders at once.
// user/Documents cd Folder/SubFolder // user/Documents/Folder/SubFolder
Takes you up one level/ out one level.
Can be combined with "
/" to move quickly.
// user/Documents/Folder/SubFolder cd .. // user/Documents/Folder
Takes you back to the main directory or root.
(Likely your username's main folder).
// user/Documents/Folder/SubFolder cd ~ // user/
If you aren't comfortable with the command line, there are great tools to help you get started using Git.
Here are a few GUIs (graphical user interfaces) to get you started:
Using these tools will help you understand how Git works and its main commands. Learning how Git works will allow you to apply it to the command line in the future.
Same language, different tools.
Now that you know how to get started and have some command line tools under your belt, let's dive into using Git.
Image Source: Tomb Raider Chronicles
Before you make your first commit (we'll get there), you will need to let Git know who you are. This is a bit like your fingerprint.
git config --global user.name "Mopsie Pie" git config --global user.email "firstname.lastname@example.org"
--global applies to all projects. If you want to create settings just for one project, don't include
Initializes a new Git project
This is you saying:
"Hey Git, I want you to keep track of my files in this folder."
This command asks Git what is currently happening with your files/repository.
It tells you what files are new or modified. With
git status you know which branch you are currently working on.
Use often. Use frequently.
A commit is a change you record in your Git history.
It's a savepoint in your code base.
git commit -m "Add polka dots"
Allows you to add a message. If you don't use the -m, another screen will pop up.
Don't do these things: bad commit messages.
(But do enjoy the humor.)
Here's a good guide for commits.
Commits are changes that get recorded into your code base. What if you don't want to make a commit, but just want to save where you are?
Like a checkpoint,
git stash is useful. It saves your unfinished changes for you to return to later.
This is especially useful when switching between branches when you have unfinished work on each.
As you make changes, it will help to know what changes you have made in the past.
Use your up and down keys to navigate through your commit history.
q to exit.
When using command line Git, sometimes you will find yourself entering a new screen where you makes changes or comments.
git log, use
Search for an answer online if you get stuck! There are lots of useful resources out there!
Imagine you are working on a project. You reach a point where you need to work on a new feature or idea, but you want don't want to impact where you currently are at with your files.
A branch takes a snapshot of your files at a specific point and splits those new files off. You are free to morph those files as you like.
Making a new change? Thinking of trying a new approach? Make a new branch first.
Think of it as a savepoint before you head down a new path.
Image Credit: Tomb Raider Chronicles
Create a new branch
git branch awesome-new-feature
Create a new branch and hop over to it immediately
git checkout -b newbranchofjoy
List your current branches
git branch master *newbranchofjoy awesome-new-feature
Jump to a different branch.
git checkout mybranch
With your local branches, you will need to merge branches. This means that you bring the file changes of two separate branches together.
git checkout branch-changes-will-be-applied-to git status git merge branch-where-I-made-changes
git checkout master git status git merge awesome-new-feature
Up to this point, we have looked at how Git might be helpful for a single user. What happens when multiple people are working on the same project and wish to collaborate?
Can you imagine how complicated it might be for a team of people to work on a project together and manually make choices about which files to use? Imagine hundreds or thousands of files. It would be a tangle.
Image Source: Walt Eis
Version control and good workflow (set guidelines for how people should integrate their files) helps make collaborating easier.
Because Git keeps track of which branch created which branch, timestamps, who made what changes, and the specific changes made to each file, it has a good memory of the history of your code.
It can make intelligent decisions about how to integrate files. Human eyes make the choices it cannot make.
Collaboration in Git involves a remote repository. This remote repository is stored somewhere on a server. Users push (or send over) their changes to this remote repository. And they pull from it to request the most recent changes.
If you want to copy a preexisting project, use
This command takes the files from a project stored elsewhere. You create a new copy of those files on your computer with this command. Once you have a copy, you can work locally, as you have already learned to do, and make changes to those files.
git clone https://somegitsite.com/username/repo.git
The folder on your computer that stores the project you are working on is your local repository (repo). It can have many branches. These files and branches are yours to control and no one sees them until you share them.
The same project might exist on another computer, generally a server, somewhere out in the cloud. Repos out in the cloud are remote repositories. Remote repositories can also have many remote branches.
A good workflow with multiple users makes good use of integrating user/local changes into remote branches.
To find out what remote repositories you project knows about or to add a new remote, you would use
A common way to get files from a remote branch is to use pull. This allows you to update the files on your own computer, pulling in the changes that have been made to the remote branch or repo.
If several people are working on a project, you can pull in the new changes and work from the most recent branch of a project.
The opposite of a pull is push. When you are ready to integrate your changes with a remote repository, it's time to push them there.
git push [remote-repo] [branch-you-are-pushing-to]
git push origin master