git-cheat-sheet

Git Commands Scribe Sheet

by S.M. Hazrat Ali

Here's a comprehensive scribe sheet for frequently used Git commands in professional settings, complete with descriptions, examples, and screenshots.

Table of Contents :

  1. Git Setup and Configuration

  2. Staging & Committing

  3. Inspection and Comparison

  4. Branching and Merging

  5. Collaboration

  6. Git Undoing changes

  7. Git Tagging
  8. Git Cleanup
  9. Git Ignore Files
  10. Git Hooks
  11. Git Tips and Tricks
  12. Git References

Git Setup and Configuration :

Git Setup:

Install Git on your system.

1. Windows:
2. Linux:

For Debian-based distributions (e.g., Ubuntu): Git is likely already installed in Linux distributions. You can confirm this is the case on your server with the following command: git --version

  1. Update the package list:
   sudo apt update
  1. Install Git:
   sudo apt install git
3. macOS:

Install Homebrew if you haven’t already:

   /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Git Version

Displays the currently installed version of Git.

   git --version

Git Version

Description : Displays the current version of Git installed on your system. This command is useful for verifying that Git is installed and for checking the installed version to ensure compatibility with other tools or repositories.

Git Config

Git config command is a convenience function that is used to set Git configuration values on a global or local project level.

Description : The git config command allows you to get and set configuration options that control the appearance and behavior of Git. Configurations can be set at three levels:

Git Config

  1. Set User Name Sets the name that will be attached to your commits and tags. Example
   // global config for name
   git config --global user.name "Hazrat"
  1. Set User Email Sets the email address that will be attached to your commits and tags.
   // global config email
   git config --global user.name "hazrat@email.com"

Git Alias

Git aliases are a powerful workflow tool that create shortcuts to frequently used Git commands.

Example :

   git config --global alias.co checkout
   git config --global alias.br branch

Description :


Git SSH Keys Setup


This Section Optional for GitHub Users Who Want to setup SSH Keys

Setting up SSH for GitHub involves generating a new SSH key, adding it to your SSH agent, and then adding the public key to your GitHub account

  1. Generate a New SSH Key

Example:

   ssh-keygen -t rsa -b 4096 -C "hazrat17016@gmail.com"

  1. Add Your SSH Key to the SSH Agent

Example:

   eval "$(ssh-agent -s)"
   ssh-add ~/.ssh/id_rsa
  1. Add the Public Key to Your GitHub Account

Example:

   cat ~/.ssh/id_rsa.pub
  1. Test Your SSH Connection You can test your SSH connection to GitHub to ensure everything is set up correctly:

Example:

   ssh -T git@github.com

Description :

Back to Top

Staging & Committing

Git Init

Initializes a new Git repository. Example

   git init

Git Init Description : Initializes a new Git repository. This command creates a new subdirectory named .git that contains all the necessary repository files.

Git Status

To view the current status of the repository: Example

   git status

Git Status

Example

   git status -s

Git Status

Description : When run git status, Git provides information about:

Git Add

Adds file contents to the staging area. Example

  1. git add file1.txt: Adds changes in file1.txt to the staging area.
   git add file1.txt
  1. git add . : Adds all changes in the current directory and its subdirectories to the staging area.
   git add .

Git Add

3.git add --all : Adds all changes in the entire working tree to the staging area, including untracked files and removing files that have been deleted.

   git add --all

Git Add

Description : When you make changes to files in your working directory, Git doesn’t automatically track those changes. You need to explicitly tell Git which changes you want to include in the next commit using git add.

Git Commit

The git commit command is used to record changes to the repository. Example

   git commit -m "commit message"

Git Commit Description :

Git Stash

The git stash command is used to temporarily store changes that are not ready to be committed. It allows you to switch branches or perform other operations without committing incomplete changes.

Example

   git stash

Git Stash

2. git stash list

Lists all the stashed changes. Each stash is given an identifier, which you can use to apply or drop the stash later.

Example :

git stash list

Git Stash

3. git stash apply

Applies the changes from a specific stash identified by stash_id. If no stash_id is provided, it applies the most recent stash.

Example :

   git stash apply
   git stash aplly stash@{2}

Git Stash

4. git stash drop [stash_id]

Removes a specific stash identified by stash_id from the list of stashes. If no stash_id is provided, it drops the most recent stash.

Example :

   git stash drop [stash_id]

Git Stash

5. git stash pop

Applies the changes from the most recent stash and removes it from the list of stashes.

Example :

   git stash pop

Git Stash

6. git stash save

Saves the current state of the working directory and staging area, allowing you to apply or discard changes later.

Example :

   git stash save

Git Stash

7. git stash show [stash_id]

Shows the contents of a specific stash identified by stash_id. If no stash_id is provided, it shows the most recent stash.

Example :

   git stash show [stash_id]

Git Stash

8. git stash clear

Removes all stashes from the list of stashes.

Example :

   git stash clear

Git Stash

Description :

Inspection and Comparison

Git Log

The git log command is used to view the commit history of a repository. It displays a list of commits in reverse chronological order, showing the commit hash, author, date, and commit message.

Example

git log

Git Log

Description :

Git Reflog

The git log command is used to view the commit history of a repository. It displays a list of commits in reverse chronological order, showing the commit hash, author, date, and commit message.

Example

git reflog

Git Log

Description :

-git reflog provides insight into the recent actions within a repository, serving as a detailed record of reference updates. -It displays information such as commit hashes, descriptions of actions (e.g., commits, merges, resets), timestamps, and reference pointers affected. -Unlike git log, which focuses on commit history, git reflog highlights changes to the repository’s references, aiding in debugging, troubleshooting, and recovering lost work.

Git Show

The git show command is used to view the contents of a commit in the repository. It displays the commit hash, author, date, and commit message.

Example

   git show

Git Show Description :

Back to Top

Branching and Merging

Git Branch

The git branch command is used to list, create, or delete branches in a Git repository

Example

   git branch

Git Branch

   git branch branch-1

Git Branch

   git branch -d branch-1

Git Branch

   git checkout branchName

Git Branch

Description :

Git Checkout

The git checkout command is used to switch branches in a Git repository. It allows you to view, create, or delete branches in a Git repository. Example

   git checkout branch-1

Git Add

Description :

Back to Top

Git Merge

The git merge command is used to merge changes from one branch into another branch in a Git repository.

Example

   git merge branch1

Git Merge

Description :

Git Rebase

The git rebase command is used to rebase the changes in the current branch onto the specified branch.

Example

   git rebase <branchName>

Git Rebase

Description :

Back to Top

Collaboration

Git Remote

The git remote command allows you to manage the remote repositories associated with your local repository. You can add, rename, remove, and view information about remotes. Example

  1. git remote -v: Lists all remote repositories along with their URLs.
   git remote --v

Git Remote

  1. git remote add <name of origin> <url> : Adds a new remote repository.
   git remote add origin https://github.com/pathURL.git
  1. git remote remove <name> : Removes the specified remote repository.
   git remote remove <name>

Description :

Git Push

The git push command is used to upload local repository content to a remote repository.

Example To push the commits from the local main branch to the main branch on the remote repository named origin:

   git push origin main

Git Remote Description :

Git Fetch

The git fetch command is used to download the latest data from a remote repository without merging it with the data in your local repository. Example

   git fetch origin

Git Pull

Description :

Git Pull

The git pull command is used to fetch and integrate changes from a remote repository into the current branch of your local repository. Example

   git pull origin main

Git Pull Description :

Back to Top

Git Undoing changes

Git Reset

The git reset command is used to reset the current HEAD to a specified state. It can be used to undo changes, unstage files, or move the HEAD to a different commit.

Example

   git reset HEAD~1

Git Reset

Git Reset Soft and Hard

   git reset --soft HEAD~1

Git Reset

   git reset --hard HEAD~1

Git Reset

Description :

Git Revert

The git revert command is used to undo changes made in previous commits. It creates a new commit that reverts the changes made in the specified commit.

Example

   git revert <commitHash>

Git Rm

The git rm command is used to remove files from the working directory and the staging area. It removes the specified files from the working directory and the staging area.

Example

   git rm textFile1.txt

Git rm

Description :

Back to Top

Description :

Git Amend

The git amend command is used to amend the last commit with the changes made in the current commit. It creates a new commit with the same changes as the last commit.

Example

   git amend

Description :

Git Cherry Pick

The git cherry-pick command is used to rebase the changes in the current branch onto the specified branch.

Example

   git cherry-pick <commitHashCode>

Description :

Git Cherry Pick

Back to Top

Git Ignore Files

The .gitignore file tells Git which files (or patterns) it should ignore. This is useful for excluding temporary files, build artifacts, and other files that you do not want to commit to your repository.

  1. Navigate to your Git repository’s root directory.
  2. Create a .gitignore file if it does not already exist:
   touch .gitignore

Examples

# Ignore node_modules directory
node_modules/

# Ignore log files
*.log

# Ignore OS-specific files
.DS_Store
Thumbs.db

# Ignore build outputs
/dist

# Ignore environment files
.env
Common .gitignore Patterns

Description : By properly configuring your .gitignore file, you can ensure that unnecessary files are not included in your Git repository, keeping your project clean and organized.

Back to Top

Git References

Back to Top