A comprehensive list of essential Git commands along with descriptions and functionalities:
- 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
- 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
- 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 .
- 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"
- git status Description: Displays the state of the working directory and staging area. Functionality: Shows untracked, modified, or staged files. Example: bash
git status
- 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
- git diff Description: Shows changes between commits, commit and working directory, etc. Functionality: Compares differences between various file states. Example: bash
git diff
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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)