DEV Community

matiar Rahman
matiar Rahman

Posted on

List of essential Git commands

A comprehensive list of essential Git commands along with descriptions and functionalities:

  1. git init Description: Initializes a new Git repository in the current directory. Functionality: Creates a .git directory that tracks changes in the project. Example: bash Copy Edit git init
  2. git clone Description: Clones (copies) an existing Git repository from a remote server to your local machine. Functionality: Creates a working copy of a remote repository. Example: bash

git clone https://github.com/user/repository.git

  1. git add Description: Adds files to the staging area, preparing them for a commit. Functionality: Tells Git to track changes in the specified files. Examples: Add a specific file: bash

git add file_name
Add all changes:
bash
Copy
Edit
git add .

  1. git commit Description: Records a snapshot of the staging area to the repository history. Functionality: Saves the current changes, usually with a message explaining the changes. Example: bash

git commit -m "Your commit message"

  1. git status Description: Displays the state of the working directory and staging area. Functionality: Shows untracked, modified, or staged files. Example: bash

git status

  1. git log Description: Displays a log of all commits made to the repository. Functionality: Lists commit history, including hash, author, date, and message. Example: bash Copy Edit git log
  2. git diff Description: Shows changes between commits, commit and working directory, etc. Functionality: Compares differences between various file states. Example: bash

git diff

  1. git branch Description: Lists, creates, or deletes branches. Functionality: Helps in managing different branches (versions) of the project. Examples: List all branches: bash

git branch
Create a new branch:
bash

git branch new_branch_name
Delete a branch:
bash

git branch -d branch_name

  1. git checkout Description: Switches to a different branch or commit. Functionality: Allows navigating between different branches or commits. Examples: Switch to a branch: bash

git checkout branch_name
Create a new branch and switch to it:
bash

git checkout -b new_branch_name

  1. git merge Description: Combines changes from one branch into the current branch. Functionality: Merges different branches into a single branch. Example: bash

git merge branch_name

  1. git pull Description: Fetches and integrates changes from the remote repository to your local branch. Functionality: Updates the local branch with the latest changes from the remote repository. Example: bash

git pull origin branch_name

  1. git push Description: Uploads local repository content to a remote repository. Functionality: Sends your committed changes to a remote repository. Example: bash

git push origin branch_name

  1. git remote Description: Manages remote repository references. Functionality: Allows setting, viewing, and deleting remote repositories. Examples: Add a remote repository: bash

git remote add origin https://github.com/user/repository.git
View remote repositories:
bash

git remote -v
Remove a remote:
bash

git remote remove origin

  1. git fetch Description: Downloads objects and refs from another repository. Functionality: Fetches changes from a remote repository but doesn’t merge them. Example: bash

git fetch origin

  1. git rebase Description: Reapplies commits on top of another base tip. Functionality: Allows integrating changes while keeping a clean commit history. Example: bash

git rebase branch_name

  1. git reset Description: Resets the current branch to a specified state. Functionality: Can move the branch pointer to a previous commit, or unstage files. Examples: Unstage a file (but keep the changes): bash

git reset file_name
Reset to a previous commit (discard changes):
bash

git reset --hard commit_hash

  1. git stash Description: Temporarily saves uncommitted changes and restores a clean working directory. Functionality: Useful for saving changes without committing them. Examples: Stash changes: bash

git stash
Apply the stashed changes back:
bash

git stash apply

  1. git tag Description: Creates, lists, or deletes tags. Functionality: Tags are used to mark specific points in a repository, like releases. Examples: Create a new tag: bash

git tag v1.0.0
List all tags:
bash

git tag
Delete a tag:
bash

git tag -d v1.0.0

  1. git rm Description: Removes files from the working directory and the staging area. Functionality: Deletes tracked files from your project. Examples: Remove a file: bash

git rm file_name

  1. git mv Description: Moves or renames a file, directory, or symlink. Functionality: Useful when refactoring files within the repository. Example: bash

git mv old_file_name new_file_name

  1. git cherry-pick Description: Applies the changes introduced by a specific commit to the current branch. Functionality: Useful when you want to copy a specific commit from another branch. Example: bash

git cherry-pick commit_hash

  1. git archive Description: Creates an archive (e.g., tar or zip) of the files in the repository. Functionality: Useful for packaging a specific state of the project. Example: bash

git archive --format=tar HEAD > latest.tar

  1. git blame Description: Shows what revision and author last modified each line of a file. Functionality: Helpful for identifying who changed specific lines in a file. Example: bash

git blame file_name

  1. git show Description: Displays various types of objects (commits, tags, etc.). Functionality: Used to view commit details or object contents. Example: bash

git show commit_hash

  1. git bisect Description: Uses binary search to find the commit that introduced a bug. Functionality: Helps isolate which commit caused a problem. Example: bash

git bisect start

Top comments (0)