Intro to Git

Setup

To install Git, run pacman -S git in an MSYS2 terminal. Set up the commit author information with the following commands.

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

The following configuration commands are also recommended.

This configures rebasing by default instead of merging when pulling a branch.

git config --global branch.autosetuprebase always

This enables 3-way diffs during merge conflicts. This gives context for both changes from a common base, which is much easier to resolve.

git config --global merge.conflictstyle diff3

This enforces fast-forward merges, effectively disallowing merge commits (they clutter up history).

git config --global merge.ff only

This disables auto-conversions of line endings to CRLF on Windows, which avoids entire files showing as conflicting during merge conflicts when the files have differing line endings between two branches.

git config --global core.autocrlf false

In general, Git tries to be helpful. If you don't know what to do after running a Git command or seeing something fail, read the text Git prints for possible hints or instructions.

Learn more about Git

  1. Learn Git - great resource to learn how to use Git
  2. Git lecture slides
  3. The Git Parable - an alternate explanation of Git terminology
  4. ugit: DIY Git in Python - implement Git in Python to learn more about how Git works on the inside
  5. PDF on Git internals for those curious about how Git stores files and revisions

References for Git

Git lecture notes

Introduction

How Git stores data

Repository management

Getting changes from others

Branches

Workflow

main is considered the stable branch which contains tested, working code. Work on features and bugfixes should occur on separate branches. Users should make as many commits on experimental branches as they feel is necessary. Before a branch is merged with main, intermediate commits are typically squashed to produce one or several feature, bugfix, or cleanup commits.

10 most commonly used Git commands

1. Cloning a remote repository

git clone <repository> [<directory>]

This downloads the repository at the given URL to the current directory. <directory> overrides the name of the repository's containing folder.

example: git clone https://github.com/frc3512/DriverStationDisplay

example: git clone git@github.com:frc3512/DriverStationDisplay

2. Adding files to the staging area

git add <file>

This adds new, untracked files as well as changes to already tracked ones.

example: git add Main.cpp

3. Removing files from the staging area

git rm [--cached] <file>

This deletes and stops files from being tracked. If the --cached option is provided, it only removes the specified files' changes from the staging area.

example: git rm UnneededFile.hpp
example: git rm --cached Main.cpp

4. Creating a commit in a local repository

git commit [-m "commit message"]

This commits changes currently in the staging area. If git commit is called without the -m option, an editor will be opened for entering a commit message.

example: git commit -m "Adding new feature x"

5. Pushing commits to a remote repository

git push [<branchname>]

This pushes commits from a branch to the corresponding branch on a remote repository. If <branchname> isn't specified, Git will use the currently checked out branch.

example: git push my-branch

6. Pulling commits from a remote repository

git pull --rebase [<refspec>]

This pulls the ref <refspec> (a branch, commmit, or tag) from the remote repository and rebases it into the current branch. If <refspec> isn't specified, Git will attempt to pull changes from a remote branch with the same name as the currently checked out branch.

example: git pull origin/main

7. Creating a branch

git branch <branchname>

This creates a new branch starting at the current commit. Omitting <branchname> shows a list of all branches and an asterisk beside the current one.

example: git branch new-feature

8. Deleting a branch

git branch -d <branchname>

This deletes the branch <branchname>. This command won't work if that branch is currently checked out.

example: git branch -d merged-feature

9. Switching to a branch

git switch <branchname>

This checks out <branchname>. The default branch when a new project is created is called main.

example: git switch other-branch

10. Merging a branch

git merge <branchname>

This merges commits from the specified branch into the branch currently checked out. In some cases, one may want to delete the branch after merging it (see 8).

example: git merge new-feature