Skip to content

Git Cheatsheet

Git-Demo.png

Introduction

Git is a popular version control system used by developers all over the world. It is a powerful tool that allows developers to track changes to their code and collaborate with others. However, with so many commands and options, it can be overwhelming. That’s why many developers use a Git cheat sheet to help them remember the most common commands and their syntax.

Installing Git

  • Windows: Download and execute the Git installer from Git’s official website.
  • Mac: If you have Homebrew installed, simply enter brew install git in the terminal. If not, download the installer from the Git website.
  • Linux: Use your distribution’s package manager. For Debian-based systems, enter sudo apt install git. For Fedora, enter sudo yum install git.

Configuring Git

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Advanced usage:

  • includeIf: including specific files if some condition was met for git

e.g.

[includeIf "gitdir:~/code/**"]
  path = ~/.config/git/personal
[includeIf "gitdir:~/work/**"]
  path = ~/.config/git/work

or

[includeIf "hasconfig:remote.*.url:[email protected]:orgname/**"]
  path = ~/.config/git/config-gh-org

[includeIf "hasconfig:remote.*.url:[email protected]:*/**"]
  path = ~/.config/git/config-gh

[includeIf "hasconfig:remote.*.url:[email protected]:*/**"]
  path = ~/.config/git/config-gl

[includeIf "hasconfig:remote.*.url:[email protected]:*/**"]
  path = ~/.config/git/config-srht
  • handles different git identities
Host gitlab.com
Hostname gitlab.com
User git
IdentityFile ~/.ssh/gitlab.id_ed25519

Host github.com
Hostname github.com
User git
IdentityFile ~/.ssh/github.id_ed25519

Creating Your First Git Repository

To start your project, create a new repository:

mkdir my-first-git-project
cd my-first-git-project
git init

This command sequence creates a new directory and initializes it as a Git repository.

git-cheat-sheet.png

Basic Commands

The following are some of the most basic Git commands that every developer should know:

  • git init: Initializes a new Git repository.
  • git add: Adds changes to the staging area.
  • git commit: Commits changes to the repository.
  • git status: Shows the status of the repository.
  • git log: Shows the commit history.

Branching and Merging

One of the most powerful features of Git is its branching and merging capabilities. The following are some of the most important commands for branching and merging:

  • git branch: Lists all branches in the repository.
  • git checkout: Switches to a different branch or commit.
  • git merge: Merges changes from one branch into another.

Collaboration and Remote Repositories

Git is designed for collaboration, and it’s important to know how to work with remote repositories. The following are some of the most important commands for collaboration and remote repositories:

  • git clone: Copies a repository from a remote server.
  • git pull: Downloads changes from a remote repository.
  • git push: Uploads changes to a remote repository.
  • git remote: Lists all remote repositories.

git-cheat-sheet-1.png

git-cheat-sheet-2.png

Resources

  • gitlab version(https://about.gitlab.com/images/press/git-cheat-sheet.pdf)
  • github version(https://education.github.com/git-cheat-sheet-education.pdf)
  • https://dev.to/doabledanny/git-cheat-sheet-50-commands-free-pdf-and-poster-4gcn
  • https://wizardzines.com/git-cheat-sheet.pdf
  • https://www.benji.dog/articles/git-config/
  • Git Interview Questions and Answers
Feedback