Git Started
Git is an open-source source tool designed to help manage source code. It is sometimes called source code management (SCM), version control system (VCS), or even revision control system (RCS). No matter what you call it, Git is a tool that provides a way to help keep track of changes to (predominantly) text-based files and their structures. Git works by taking a “snapshot” of all files and directories within a project directory. Git organizes these snapshots in what are known as repositories or “repos” for short. Repositories are essentially a record of all the different snapshots and how they relate to each other. Git allows users to freely edit projects and even create project “branches” (independent duplicates) for development and experimentation while keeping track of everything. Users can experiment on one branch, and if the code is good, it can be merged to the main branch. If the experiment is bad, no big deal, it can be left for later, or even deleted without affecting the main project branch. If a mistake is made, Git can be used to back track to a state that the code worked. This allows users to develop and evaluate project versions prior to changing the production code base.
Git provides a distributed framework which allows repositories to exist both locally on your computer, with duplicates synced up to a cloud version called a “remote repository”. Multiple users can access the remote, which allows more than one person to work on the same code base. Users clone the remote to a local directory and fork development branches that are then pushed back to the remote for potential merger. If it’s all done properly, this allows multiple people to work on the code without disturbing either the production code base or each user’s individual copy.
Organization
This guide is broken into three basic levels of Git complexity:
-
On the first level, we explore the basics of Git. This involves installing, configuring, and working with a single branch on a local machine. At this level we become familiar with the three stages that Git tracks and how to deal with various issues within these contexts. The first level of complexity is covered in the Git Basics section.
-
Stepping up in complexity we introduce branches. Branches allow us to work on multiple versions of the same project concurrently. Branches are useful for exploring various features that we may want to add, but can safely develop without disturbing the base, or “main” project code. When we’re happy with a feature, we can then merge that code into the main branch with confidence.
-
Finally, we introduce remote repositories. Remotes allow us to fulfil two incredibly valuable functions. Remotes allow us to back up our work remotely, thus preserving it from a local hardware malfunction. Remotes also allow us to work collaboratively by making our repos available to other users.
Installation
There are several ways to install Git on your system. For MacOS it may be easiest to simply use the Xcode Command Line Tools by running git
in terminal. Refer to the official Git docs for current recommendations.
$ git
If Git is not already installed, the terminal will provide prompts for installing it. It is then possible to execute a version check by running the following command.
$ git --version
Configuration
Once Git is downloaded and installed, it is possible to configure it for use. Generally speaking, these are things that are only set once per machine, as the settings will persist even if the Git version is updated. There are three tiers/places that Git stores configuration information. The three tiers are listed by their command line interface (CLI) modifier and where the configuration files are typically stored.
Settings
Check configuration settings using the git config --list
command.
-
System
These are configurations that will apply to every user of the particular machine by default. Each user can override system configurations with custom configurations.
- CLI Modifier:
--system
- Config file location:
/etc/gitconfig
(MacOS)
- CLI Modifier:
-
User
These are used to control configurations for a particular user login.
- CLI Modifier:
--global
- Config file location:
~/.gitconfig
as/Users/myuser/.gitconfig
(MacOS)
Terminal window # Set name and email$ git config --global user.name "Peter Fakemann"$ git config --global user.email pfakemann@gamil.com# Set default branch name to "main"$ git config --global init.defaultBranch main# Set text editor to add commit messages$ git config --global core.editor "/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl --new-window --wait"$ git config --global core.editor "subl -n -w"# My current setup for posterity$ git config --global core.editor "nvim -c 'set ft=gitcommit' -c 'set tw=0' -c 'set wrap' -c 'startinsert'"Note that the example for preferred editor includes two variations. Both options set the preferred editor to Sublime Text. The first option points to the application’s executable file (subl), and adds the “new window” and “wait” options that open a new Sublime Text window and waits to commit until the file is saved (cmd + s) and closed (cmd + w). The second option simply references the executable “subl” argument with “new window” and “wait” options, but uses the abbreviated forms as
-n
and-w
respectively. If your system doesn’t recognize the “subl” command you may need to create a symlink.Terminal window sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublThe first option avoids the symlink, but without it the “subl” command is not recognized in the terminal.
As of late 2023 Ive become a bit of a Neovim fanboy. As such Ive decided to edit my .gitconfig file to use Neovim. When I opened my
~/.gitconfig
file I found this, and stubbed in the line for Neovim. I’ll circle back to why this isn’t Bash some other time.[filter "lfs"]cess = git-lfs filter-processrequired = trueclean = git-lfs clean -- %fsmudge = git-lfs smudge -- %f[user]name = p5chmitzemail = petermschmitz@gmail.com[core]# editor = subl -n -weditor = nvim -c 'set ft=gitcommit' -c 'set tw=0' -c 'set wrap' -c 'startinsert'excludesfile = /Users/peterschmitz/.gitignore_global[init]defaultBranch = main - CLI Modifier:
-
Project
These are used for specific projects, as the name suggests.
- CLI Modifier: (no modifier)
- Config file location:
project_name/.git/config
(MacOS)
Preferences
In addition to setting tool configurations for things like name, default text editor and default branch names, it is also smart to add some quality-of-life issues like phrase auto-completion (or simply search for “completion” in the Git public directory), which makes typing commands much quicker and helps with spelling and formatting. Check out the official Git (for Bash) setup over on Git.