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.
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
sudo apt update
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.
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:
// global config for name
git config --global user.name "Hazrat"
// 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 of git checkout
git br
instead of git branch
git ci
instead of git commit
git st
instead of git 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
Example:
ssh-keygen -t rsa -b 4096 -C "hazrat17016@gmail.com"
Example:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Example:
cat ~/.ssh/id_rsa.pub
Example:
ssh -T git@github.com
Description :
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
Example
git status -s
Description : When run git status, Git provides information about:
Adds file contents to the staging area. Example
git add file1.txt
: Adds changes in file1.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"
Description :
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.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.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
Description :
git show
is a powerful command for viewing the contents of a commit in the repository.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
git branch
git branch branch-1
git branch -d
command followed by the name of the branch you want to delete. git branch -d branch-1
git checkout command
followed by the name of the branch you want to switch to. git checkout branchName
Description :
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.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 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
Description :
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).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
Description :
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 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.git reset HEAD~1
, the HEAD moves back one commit, effectively undoing the last commit and keeping changes in the working directory.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.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.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.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.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.
.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
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.