Git Cheatsheet

Git is a distributed version control system that is used to manage and track changes to source code and other files. It was created in 2005 and has since become one of the most widely used version control systems in the world.

Git allows developers to work on a shared codebase simultaneously, keeping track of changes made by each contributor and allowing for easy collaboration. It uses a distributed architecture, which means that each developer has a local copy of the repository on their computer and can make changes independently. These changes can then be shared with other team members through a central repository or peer-to-peer sharing.

By storing versions of files and directories as snapshots, it allows developers to track changes made over time and revert to previous versions if necessary. It also supports branching and merging, which allows developers to work on multiple versions of a codebase simultaneously and merge changes back together as needed.

Git is highly customizable and can be integrated with various tools and services, such as GitHub, Bitbucket, and GitLab. It is used by software development teams of all sizes, from small startups to large enterprises, and is an essential tool for modern software development.

Git concepts and workflow

Source of Truth: orginal repository from where a project is launched.

Git workflow

Git terminology

  • Working tree: set of nested directories and files that contain the project
  • Repository: directory located at the top of a working tree (contains history and metadata)
  • Hash: represents contents of a file/object as a fixed number of digits
  • Object: Blob (ordinary file), tree (directory with names, hashes and permissions), commit (specific working tree version), tag (name of the commit)
  • Commit: committing the changes to the working tree so that others can eventually see them
  • Branch: named series of linked commits
  • Remote: named reference to another Git repository
  • Commands: Git operations are performed by using commands with the following structure <command> <subcommand> [options]

Git commands

Git configuration

# Define a global username in order to make commits
git config --global user.name "<username>"
# Define a global email in order to make commits
git config --global user.email "<user-email-address>"

# Display git configuration options
git config --list

Working with repositories

# Create a local repository that will be represented by the directory named `.git`:
git [options] init REPO_DIRECTORY

Note: When working in a remote repository, use:

git clone [options] REMORE_REPO_URL TARGET_DIRECTORY
# Download the latest, current assets from a remote repository to the local repository:
git pull [options]

# Download the latest code and assets from a remote repository to the local repository 
# without overwriting existing local code and assets in the current branch:
git fetch [options]

# Display the Git log file (history of all transactions in the repository):
git log [options]

Note: --oneline option to show abbreviated format.

Working with branches

# Show all branches in the local repository, flagging the current branch that is checked out from the local repository:
git branch

# Show all branches in the remote repository:
git branch -r

# Show all branches in the local and remote repository:
git branch -a

# Create a new branch in the local repository:
git branch NEW_BRANCH_NAME EXISTING_BRANCH_NAME

Note: If EXISTING_BRANCH_NAME is not given, the branch will be derived from the current working branch.

# Change to another branch in the local or remote repository:
git checkout BRANCH_NAME

Working with content

# Report the status of the current filesystem associated with the local repository.
git status [options] DIRECTORY_OR_FILE

# Add local content from the current branch to the staging environment:
git add [options] DIRECTORY_OR_FILE

Note: Unstage changes:

git restore --staged FILE
# Commit content from the staging environment to the local repository:
git commit [options] DIRECTORY_OR_FILE

# Commit content with a message:
git commit -m "This is my commit message."

# Commit only specific content:
git commit FILE1 FILE2 DIR3

# Upload content from the local repository to the remote repository:
git push [options] REMOTE_REPOSITORY

# Roll back a file to its previous state:
git restore [options] FILE

# Roll back a file to a particular state based on a specific context with the repository:
git clean [options] FILE

# Revert the file system associated with a local `.git` repository to a previous state:
git revert [options] COMMIT_UUID

Merging branches

# Merge files and directories from the SOURCE_BRANCH into the TARGET_BRANCH:
git merge [options] TARGET_BRANCH SOURCE_BRANCH

Note: --no-edit option to skip writing a merge message (highly discouraged).

# Merge one repository onto another while also transferring the commits from the SOURCE_BRANCH onto the TARGET_BRANCH:
git rebase [options] OTHER_BRANCH

# Invoke an editing tool to resolve merge conflicts between files:
git mergetool TOOL

Note: Register a merge editor using the following command:

git config --global merge.tool vimdiff

Keep track of changes

# Display a list of recent commits on a file by committer along with changes in the file:
git blame [options] FILE

# Tag a repository:
git tag [options] TAG_NAME

Note: -n option to show message associated with each tag.