GIT Cheat Sheet: Your Ultimate Guide to Version Control

Welcome to the ultimate guide on the GIT cheat sheet! In today’s fast-paced software development world, having an effective version control system is a crucial aspect for managing projects efficiently. GIT, being a distributed version control system, has gained immense popularity because of its flexibility and powerful features. This cheat sheet serves as the go-to resource for mastering GIT whether you’re a beginner or an experienced developer. You can bookmark our GIT cheat sheet website page or you can do CNTRL + P to have GIT cheat sheet printable version which can be saved as PDF also.

GIT Cheat Sheet website

GIT Cheat Sheet website

What is GIT?

GIT is a distributed version control system designed to track changes in files and coordinate work among multiple developers. It was created by Linus Torvalds in 2005 and has since become the de facto standard for version control in software development. With GIT, developers can easily collaborate, manage changes, and track the history of their projects.

Installing GIT: A Step-by-Step Guide

To get started with GIT, you need to install it on your local machine. Follow these simple steps to install GIT:

  1. Visit the official GIT website at git-scm.com.
  2. Download the appropriate version of GIT for your operating system.
  3. Run the installer and follow the on-screen instructions.
  4. Once the installation is complete, open a terminal or command prompt and verify the installation by typing git --version.

GIT Cheat Sheet Basics

Initializing a Repository

To start using GIT in your project, you need to initialize a repository. Navigate to the root directory of your project using the terminal or command prompt and run the following command:

git init

This command initializes an empty GIT repository in the current directory. GIT will create a hidden .git folder that contains all the necessary files to track changes in your project.

Basic Commands

  • git status: Show the current status of the repository.
  • git add <file>: Add a file to the staging area.
  • git add . or git add --all: Add all modified and new files to the staging area.
  • git commit -m "Commit message": Commit changes in the staging area with a message.
  • git commit -a -m "Commit message": Stage and commit all modified files in one command.
  • git diff: Show changes between the working directory and the staging area.
  • git diff --staged or git diff --cached: Show changes between the staging area and the last commit.

Tracking Changes

Once you have initialized a repository, GIT will start tracking changes in your files. You can use the following commands to check the status of your repository and track changes:

git status

The git status command displays the current state of your repository, showing which files are modified, untracked, or staged for commit.

git add <file>

The git add command stages changes in a file, preparing it for commit. You can specify a single file or use wildcards to add multiple files.

git diff

The git diff command shows the differences between the current state of your files and the last committed version. It helps you review the changes before committing them.

Committing Changes

Committing changes in GIT creates a new version of your project with the tracked modifications. Use the following command to make a commit:

git commit -m "Your commit message here"

The -m flag allows you to add a descriptive message to your commit, explaining the changes you made. It’s essential to provide meaningful commit messages for better project management and collaboration.

Branching and Merging

GIT’s branching and merging capabilities enable parallel development and collaboration among team members. You can create a new branch to work on a specific feature or bug fix and merge it back into the main branch when ready. Here are the commands to create a new branch and merge it:

  • git branch: List all branches in the repository.
  • git branch <branch_name>: Create a new branch.
  • git branch -d <branch_name>: Delete a branch.
  • git checkout <branch_name>: Switch to a different branch.
  • git checkout -b <branch_name>: Create a new branch and switch to it.
  • git merge <branch_name>: Merge changes from a different branch into the current branch.

Resolving Merge Conflicts

Merge conflicts occur when GIT encounters incompatible changes during the merging process. It happens when two branches modify the same part of a file independently. To resolve merge conflicts, follow these steps:

  1. Identify the conflicted files using git status.
  2. Open the conflicting file in a text editor and locate the conflicting sections marked by <<<<<<<, =======, and >>>>>>>.
  3. Edit the file to resolve the conflicts manually.
  4. Save the file and stage it using git add.
  5. Commit the changes to complete the merge process.

Working with Remote Repositories

GIT allows you to collaborate with other developers by working with remote repositories. You can clone, pull, and push changes to remote repositories, enabling seamless teamwork. Let’s explore how to work with remote repositories in GIT.

Cloning a Repository

To clone a remote repository to your local machine, use the following command:

git clone <repository-url>

Replace <repository-url> with the URL of the repository you want to clone. GIT will create a local copy of the entire repository on your machine.

Pulling and Pushing Changes

To incorporate the latest changes from a remote repository into your local copy, use the git pull command:

git pull

The git pull command fetches the changes from the remote repository and merges them into your current branch.

To push your local changes to a remote repository, use the git push command:

git push

The git push command sends your committed changes to the remote repository, making them available to others.

Remote Repositories

  • git remote add <remote_name> <remote_url>: Add a remote repository.
  • git remote -v: List all remote repositories.
  • git push <remote_name> <branch_name>: Push the local branch to a remote repository.
  • git pull <remote_name> <branch_name>: Pull changes from a remote repository.
  • git clone <remote_url>: Clone a remote repository to the local machine.

Working with Remotes

  • git fetch <remote_name>: Fetch changes from a remote repository without merging.
  • git remote show <remote_name>: Show information about a remote repository.
  • git remote rename <old_name> <new_name>: Rename a remote repository.
  • git remote remove <remote_name>: Remove a remote repository.
  • git push <remote_name> --delete <branch_name>: Delete a branch from a remote repository.
  • git pull --rebase <remote_name> <branch_name>: Fetch and rebase changes from a remote repository.

Forking and Collaboration

Forking a repository allows you to create your copy of a project on a remote hosting service like GitHub. It enables you to make changes independently and propose them to the original project through pull requests. Follow these steps to fork a repository and collaborate with others:

  1. Visit the repository you want to fork on the hosting platform (e.g., GitHub).
  2. Click on the “Fork” button to create a copy of the repository under your account.
  3. Clone the forked repository to your local machine using the git clone command.
  4. Make changes, commit them, and push them to your forked repository.
  5. Create a pull request on the original repository to propose your changes.

GIT Cheat Sheet Advanced Techniques

GIT offers advanced techniques that can enhance your version control workflow and provide greater control over your project’s history. Let’s explore some of these techniques.

Stashing Changes

Sometimes, you may need to switch to a different branch or work on a different task while your changes are not ready for a commit. In such cases, you can stash your changes using the following command:

  • git stash: Stash changes in the working directory for later use.
  • git stash list: List all stashed changes.
  • git stash apply: Apply the most recent stash and keep it in the stash list.
  • git stash apply stash@{n}: Apply a specific stash from the stash list.
  • git stash drop: Discard the most recent stash.
  • git stash drop stash@{n}: Discard a specific stash.

Reverting Commits

If you want to undo a specific commit in your project’s history, you can use the git revert command:

git revert <commit-hash>: Replace <commit-hash> with the hash of the commit you want to revert. GIT will create a new commit that undoes the changes made in the specified commit.

Rewriting History

GIT provides the flexibility to rewrite your project’s history by amending or reordering commits. Use the following commands for history rewriting:

git commit --amend: The git commit –amend command allows you to modify the last commit’s message or add additional changes to it.

git rebase -i <commit-hash>: The git rebase -i command enables interactive rebasing, allowing you to reorder, squash, or edit commits interactively.

History and Undo

  • git log: Show commit history.
  • git log --oneline: Show concise commit history.
  • git reset <commit_hash>: Reset the repository to a specific commit.
  • git revert <commit_hash>: Revert a commit by creating a new commit.
  • git checkout -- <file>: Discard changes in a file and restore it to the last commit.

Managing Submodules

GIT submodules are repositories within repositories, allowing you to include external projects as dependencies. To manage submodules, use the following commands:

git submodule add <repository-url> <destination-path>

The git submodule add command adds a submodule to your project. Replace <repository-url> with the URL of the submodule and <destination-path> with the path where you want to store it.

git submodule update --init --recursive

The git submodule update –init –recursive command initializes and updates all the submodules in your project.

Tagging

  • git tag: List all tags in the repository.
  • git tag <tag_name>: Create a lightweight tag at the current commit.
  • git tag -a <tag_name> -m "Tag message": Create an annotated tag with a message.
  • git show <tag_name>: Show information about a specific tag.
  • git push <remote_name> <tag_name>: Push a tag to a remote repository.
  • git push <remote_name> --tags: Push all tags to a remote repository.

Customizing GIT Configuration

You can customize various aspects of GIT’s behavior by modifying the configuration settings. Use the following commands to set your GIT configuration:

  • git config --global user.name "Your Name": Set your name for Git commits.
  • git config --global user.email "[email protected]": Set your email for Git commits.
  • git config --global color.ui auto: Enable colored output in Git.

Replace “Your Name” and [email protected] with your desired name and email address. These settings will be used for identifying your commits.

Rebasing

  • git rebase <branch_name>: Rebase the current branch onto a different branch.
  • git rebase --interactive <branch_name>: Perform an interactive rebase.
  • git rebase --continue: Continue the rebase after resolving conflicts.
  • git rebase --abort: Abort the rebase operation and return to the original state.

Gitignore

  • Create a file named .gitignore in the repository’s root directory to specify files and directories to ignore.
  • Use wildcards and patterns in the .gitignore file to define exclusion rules.

Temporary Commits

  • git stash save "Stash message": Stash changes in the working directory with a custom message.
  • git stash pop: Apply the most recent stash and remove it from the stash list.
  • git stash branch <branch_name>: Create a new branch from a stash.

Ignoring Patterns

  • Create or edit the .gitignore file in the repository’s root directory to specify files, directories, or patterns to ignore.
  • Use glob patterns, such as *.txt or build/, to define exclusion rules.

Tracking Path Changes

  • git rm <file>: Remove a file from the repository and the working directory.
  • git mv <old_path> <new_path>: Rename or move a file within the repository.
  • git log -- <file>: Show the commit history of a specific file.

Share & Update

  • git remote add origin <remote_url>: Add a remote repository and name it “origin.”
  • git push -u <remote_name> <branch_name>: Push a local branch to a remote repository and set it as the upstream branch.
  • git fetch <remote_name>: Fetch changes from a remote repository without merging.
  • git pull <remote_name> <branch_name>: Pull changes from a remote repository and merge them into the current branch.

Inspect & Compare

  • git show <commit_hash>: Show detailed information about a specific commit.
  • git diff <commit1> <commit2>: Show the differences between two commits.
  • git blame <file>: Display the commit and author information for each line in a file.

Stage & Snapshot

  • git add -p: Interactively choose and add changes to the staging area.
  • git reset <file>: Unstage a file and remove it from the staging area.
  • git stash save --include-untracked: Stash both tracked and untracked changes.

Synchronizing Repositories

  • git remote -v: List all remote repositories and their URLs.
  • git push <remote_name> <branch_name>: Push the local branch to a remote repository.
  • git fetch <remote_name>: Fetch changes from a remote repository without merging.

Tagging Known Commits

  • git tag <tag_name> <commit_hash>: Create a tag for a specific commit.
  • git tag -d <tag_name>: Delete a tag.

Review Your Work

  • git log --graph --oneline --all: Display a compact and graphical representation of the commit history.
  • git log --author=<author_name>: Show commit history filtered by the author’s name.
  • git diff --stat: Show a summary of changes made in each file.

Checkout/Switch

  • git checkout <branch_name>: Switch to a different branch.
  • git switch <branch_name>: Switch to a different branch.
  • git checkout -b <branch_name>: Create a new branch and switch to it.
  • git switch -c <branch_name>: Create a new branch and switch to it.
  • git checkout <commit_hash>: Switch to a specific commit.

Merge

  • git merge <branch_name>: Merge changes from a different branch into the current branch.
  • git merge --no-ff <branch_name>: Perform a non-fast-forward merge, creating a merge commit even if unnecessary.
  • git merge --abort: Abort a merge in progress and return to the pre-merge state.

Mergetool

  • git mergetool: Launch the configured merge tool to resolve merge conflicts.
  • git mergetool --tool=<tool_name>: Launch a specific merge tool to resolve conflicts.
  • git mergetool --tool-help: Display the available merge tools and their configuration.

Log

  • git log: Show commit history.
  • git log --oneline: Show concise commit history.
  • git log --graph: Display a graph representing the commit history.
  • git log --author=<author_name>: Show commit history filtered by the author’s name.
  • git log --since=<date>: Show commit history since a specific date.

Worktree

  • git worktree add <path> <branch_name>: Add a new working tree at the specified path and checkout a branch into it.
  • git worktree list: List all linked working trees.
  • git worktree prune: Remove stale working trees that are no longer needed.
  • git worktree remove <path>: Remove a linked working tree.

Status

  • git status: Show the current status of the repository.
  • git status -s or git status --short: Show the status in a short format.

Diff

  • git diff: Show changes between the working directory and the staging area.
  • git diff <commit1> <commit2>: Show changes between two commits.
  • git diff --staged or git diff --cached: Show changes between the staging area and the last commit.

Commit

  • git commit -m "Commit message": Commit changes in the staging area with a message.
  • git commit -a -m "Commit message": Stage and commit all modified files in one command.
  • git commit --amend: Amend the most recent commit with new changes.

Notes

  • git notes add -m "Note message": Add a note to a commit.
  • git notes show: Show notes for a commit.
  • git notes edit: Edit notes for a commit.

Restore

  • git restore <file>: Discard changes in a file and restore it to the last commit.
  • git restore --staged <file>: Unstage changes in a file, but keep the modifications.
  • git restore --source=<commit> <file>: Restore a file to a specific commit.

Reset

  • git reset <commit>: Reset the repository to a specific commit, discarding all changes after it.
  • git reset --soft <commit>: Reset the repository to a specific commit, keeping changes staged.
  • git reset --hard <commit>: Reset the repository to a specific commit, discarding all changes.

Remove (RM)

  • git rm <file>: Remove a file from the repository and the working directory.
  • git rm --cached <file>: Remove a file from the repository but keep it in the working directory.

Move (MV)

  • git mv <old_path> <new_path>: Rename or move a file within the repository.

GIT Best Practices

To make the most out of GIT and ensure a smooth version control workflow, consider the following best practices:

Creating Descriptive Commit Messages

Write clear and descriptive commit messages that summarize the changes made in a commit. A well-crafted commit message helps you and your team members understand the purpose and impact of a commit.

Using Branches Effectively

Create branches for new features, bug fixes, or experiments. Keep your main branch (often called master or main) stable and deployable. Regularly merge your branches back into the main branch to incorporate changes.

Regularly Updating the Repository

Pull the latest changes from the remote repository before starting your work. Regularly update your local copy to avoid conflicts and ensure you’re working with the latest code.

Collaborating with Team Members

Communicate with your team members and follow a consistent workflow for branching, merging, and code reviews. Establish clear guidelines and conventions to maintain a unified and efficient development process.

Handling Large Files

GIT is not optimized for handling large binary files. Consider using external tools like Git LFS (Large File Storage) or storing large files separately and referencing them in your repository.

Congratulations! You have reached the end of our GIT cheat sheet, and now you’re equipped with the knowledge and commands needed to utilize GIT for version control effectively. Remember to practice regularly and explore more advanced features to enhance your workflow. GIT’s flexibility and power make it an indispensable tool for every developer. Happy coding!

Leave a Comment

error: Content is protected !!