Here's a comprehensive scribe sheet for frequently used Git commands in professional settings, complete with descriptions, examples, and screenshots.
Install Git on your system.
- Download Git from the official Git website.
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
- Update the package list:
sudo apt update
- Install Git:
sudo apt install git
Install Homebrew if you haven't already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Displays the currently installed version of Git.
- This command checks if Git is installed on your system. If Git is not installed, it will install it for you. Example
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 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:
- System: Applies to every user on the system and all their repositories.
- Global: Applies to all repositories for the current user.
- Local: Applies to the specific repository you are currently working in
- 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"
- 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 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 alias is a mechanism for defining commands that are more accessible and easier to remember than the original command. These aliases make common Git commands shorter and easier to remember. For example:
-
git co
instead ofgit checkout
-
git br
instead ofgit branch
-
git ci
instead ofgit commit
-
git st
instead ofgit status
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
- Generate a New SSH Key
Example:
ssh-keygen -t rsa -b 4096 -C "hazrat17016@gmail.com"
- Add Your SSH Key to the SSH Agent
Example:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
- Add the Public Key to Your GitHub Account
Example:
cat ~/.ssh/id_rsa.pub
- 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 :
- SSH keys allow you to securely authenticate Git commands to your repositories. These keys are used to authenticate Git commands when you push changes to your repositories.
Initializes a new Git repository. Example
git init
Description :
Initializes a new Git repository. This command creates a new subdirectory named .git that contains all the necessary repository files.
To view the current status of the repository: Example
git status
- Git Status in Short
Example
git status -s
Description : When run git status, Git provides information about:
- Modified files: Files that have been changed but not yet staged for commit.
- Staged files: Changes that have been added to the staging area and are ready to be committed.
- Untracked files: Files that are not tracked by Git.
Adds file contents to the staging area. Example
git add file1.txt
: Adds changes infile1.txt
to the staging area.
git add file1.txt
git add .
: Adds all changes in the current directory and its subdirectories to the staging area.
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
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.
The git commit
command is used to record changes to the repository.
Example
git commit -m "commit message"
- The -m flag allows you to provide a commit message inline with the command.
- The commit message should be a brief description of the changes you are committing.
- After running git commit, Git creates a new commit with the changes staged in the index and records it permanently in the repository's history.
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
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
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}
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]
Applies the changes from the most recent stash and removes it from the list of stashes.
Example :
git stash pop
Saves the current state of the working directory and staging area, allowing you to apply or discard changes later.
Example :
git stash save
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]
Removes all stashes from the list of stashes.
Example :
git stash clear
Description :
git stash
is a handy tool for managing work in progress without committing changes.- It saves the current state of the working directory and staging area, allowing you to revert to it later.
- Stashed changes can be applied or discarded using git stash apply or git stash drop commands.
- git stash is useful for switching context quickly or temporarily setting aside unfinished work.
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
Description :
git log
is a powerful tool for inspecting the history of a repository.- It shows the commit history, including the commit hash (SHA-1 checksum), author name, email, date, and commit message.
- By default, git log displays the commits starting from the most recent.
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
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.
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
is a powerful command for viewing the contents of a commit in the repository.- It displays the commit hash, author, date, and commit message.
git show
is useful for viewing the changes made in a particular commit.
The git branch
command is used to list, create, or delete branches in a Git repository
Example
- To list all the local branches in the repository:
git branch
- Creating a Branch To create a new branch in Git, you use the git branch command followed by the name of the new branch you want to create.
git branch branch-1
- Deleting a Branch
To delete a branch in Git, you use the
git branch -d
command followed by the name of the branch you want to delete.
git branch -d branch-1
- Checking Out a Branch
To switch to a different branch in Git, you use the
git checkout command
followed by the name of the branch you want to switch to.
git checkout branchName
Description :
- When used without options, git branch lists all the local branches in the repository. The current branch is indicated with an asterisk (*).
- Can create a new branch by providing a name after the git branch command.
- To delete a branch, use the -d option followed by the name of the branch you want to delete.
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
Description :
git checkout
: This is the command to switch branches in a Git repository.branch-1
: This is the name of the branch you want to switch to.
The git merge
command is used to merge changes from one branch into another branch in a Git repository.
Example
git merge branch1
Description :
git merge
: This is the command to merge changes from one branch into another branch in a Git repository.branch-1
: This is the name of the branch you want to merge into the current branch.
The git rebase
command is used to rebase the changes in the current branch onto the specified branch.
Example
git rebase <branchName>
Description :
git rebase
creates a new commit that reverts the changes introduced by the specified branch.- It modifies the commit history and creates a new commit with the same changes as the last commit.
git rebase
is useful for creating a new commit with the same changes as the last commit without modifying the commit history.
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
git remote -v
: Lists all remote repositories along with their URLs.
git remote --v
git remote add <name of origin> <url>
: Adds a new remote repository.
git remote add origin https://github.com/pathURL.git
git remote remove <name>
: Removes the specified remote repository.
git remote remove <name>
Description :
- The -v option stands for "verbose," and it displays both the URL of the remote repository and its nickname.
- When clone a repository, Git automatically creates a remote called origin that points to the original repository.
- add multiple remotes to your repository to collaborate with others or to manage different hosting services.
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 push
: This is the command to push your local commits to a remote repository.origin
: This is the default name Git gives to the server from which you cloned your repository. It represents the remote repository.main
: This is the name of the branch you want to push your commits to on the remote repository.
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
Description :
git fetch
retrieves the latest changes from the remote repository specified by its name (e.g., origin).- It downloads new branches, tags, and commits from the remote repository without modifying your local branches.
- After fetching, you can inspect the changes using commands like git log or git diff.
git fetch
is useful for updating your local repository with changes from the remote repository without automatically merging them into your working branch.
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 is a combination of two other Git commands: git fetch and git merge.
- It fetches changes from the specified remote repository and integrates them into the current branch.
- The argument specifies the remote repository from which - want to pull changes, and specifies the branch from which you want to pull changes.
- After fetching the changes, git pull automatically merges them into your current branch.
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 --hard moves the HEAD to the specified commit and discards all changes after that commit.
Git Reset Soft and Hard
git reset --soft
moves the HEAD to the specified commit, keeping the changes in the index.
git reset --soft HEAD~1
git reset --hard
moves the HEAD to the specified commit and discards all changes after that commit.
git reset --hard HEAD~1
Description :
git reset
is a versatile command with different options for resetting the repository state.- With
git reset HEAD~1
, the HEAD moves back one commit, effectively undoing the last commit and keeping changes in the working directory. - Depending on the options used (soft, mixed, or hard), git reset can reset the staging area, working directory, or both.
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>
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
Description :
git rm
removes files from the working directory and the staging area.- It removes the specified files from the working directory and the staging area.
git rm
is useful for undoing changes made to files in the working directory or staging area.
Description :
git revert
creates a new commit that undoes the changes introduced by the specified commit.- It does not modify the commit history but instead creates a new commit with the reversed changes.
- Multiple commits can be reverted in a single command by specifying their commit hashes.
- Reverting a merge commit requires specifying the parent commit to revert to.
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 amend
creates a new commit with the same changes as the last commit.- It does not modify the commit history but instead creates a new commit with the same changes as the last commit.
git amend
is useful for creating a new commit with the same changes as the last commit without modifying the commit history.
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
creates a new commit that reverts the changes introduced by the specified commit.- It does not modify the commit history but instead creates a new commit with the reversed changes.
- Multiple commits can be cherry-picked in a single command by specifying their commit hashes.
- Cherry-picking a merge commit requires specifying the parent commit to cherry-pick from.
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.
- Navigate to your Git repository's root directory.
- 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
- *.log – Ignores all .log files.
- *.tmp – Ignores all .tmp files.
- node_modules/ – Ignores the node_modules - directory.
- dist/ – Ignores the dist directory.
- .DS_Store – Ignores macOS specific file.
- Thumbs.db – Ignores Windows specific file.
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.