Understanding the Rule of Three in Git
Before you jump into using Git, you’ll need to make sure you’re clear on the three major sections of a Git project, and the workflow between them.
- Your working directory
- Your staging area (aka the index)
- The Git repository
With most projects where you’re working with other developers, the third section is likely to have both a local repository and a remote repository.
This diagram highlights the workflow of Git:
The Git Cheat Sheet
What follows is a list of the commands that you can see after downloading our free Git cheat sheet.
NB: Where you see a parameter within square brackets, it means it is optional. If there are two or more parameters within square brackets, separated by a pipe, it means that they are synonymous. So: [x|y] means that x and y are the same command.
How to Set Up Git
For a quick start beginner’s guide to setting up a new Git project, you’ll need one of two commands. You’ll either want to set the current directory as a Git repository, or you’ll want to clone an existing repository from a local or remote source.
Initialises the current directory as a new Git repository.
Clones a local repository to the current directory.
Clones an existing repository from the hosted location to the current directory.
How to Configure Git
Next, you’ll want to set up and configure a few essential details. These Git configure shortcuts will help make your workflow clear and simple, adding your user details and setting preferences. There’s even a simple command to make reading lines in Git a lot easier.
git config --global user.name "<your-name>"
Sets the name for the user when making commits.
git config --global user.email "<your-email>"
Sets the email address for the user when making commits.
git config --global color.ui auto
Sets automatic colouring on command lines (and makes reviewing easier!)
git config --system core.editor <text-editor-path>
Sets your preferred text editor.
git config (--global | --system | --local) --edit
Opens one of Git’s configuration files: either the global, system, or local one.
How to Add Files to Git
With the essentials configured, you can start adding files. There are several different ways to add to Git, depending on if you want to add single or multiple files and what types of updates you want to include. Our Git cheat sheet explains the different options.
git add (<file-name> | <directory-name>)
Text that describes the sample code.
Adds all new, modified and deleted files in the current folder.
Adds all new, modified and deleted files in all directories in the repository.
Updates the staging area to match every directories modified and deleted files, but doesn’t add new files.
How to Commit & Tag in Git
Once you’ve added all the new files you need and made the necessary changes, you need to commit those changes from staging (aka the index) to your local repository.
Commits all staged content.
Stages any modified or delete files, and then commits all files that are staged.
Replaces the last commit with changes that you later supply.
Lists all the tags in the repository.
Tags the head commit with the supplied tag-name.
How to Connect, Push & Pull in Git
After you’ve committed to your local repo, you’ll need to push or pull commits, branches and tags to and from the remote repo. Make sure it’s connected first, and add an alias for it, to make future commands much easier.
git remote add <remote-alias> <url>
Adds an alias to the new remote repository at <url>
Pushes the current branch to the remote repository with all commits and objects.
git push --all <remote-alias>
Pushes all local branches to remote repository.
git fetch <remote-alias> <branch-name>
Fetches a specific branch, without changing your local files.
Fetches all branches, and merges changes into the local files.
Get the Full Git Cheat Sheet
To get all of these commands and more, just download and print off this cheat sheet, so that whenever you get stuck you can simply refer to the relevant command.
The Quickest & Easiest Solution
Do you need a Git GUI that's totally free for personal use? Vershd boosts your software development productivity by making Git simple with a unique UI/UX. It's a Git GUI that prevents errors and creates clarity. This Git client soars over your repository and then swoops down to quickly tackle branches, commits and files. It deals with Git, so you can concentrate on coding.
Get screenshots and a 60 second video of what Vershd, the effortless Git GUI
can do for you.