Introduction
Git is a powerful version control system that allows developers to track changes in their code, collaborate with others, and manage different versions of their projects. Mastering Git commands is essential for efficient and effective code management. This cheat sheet provides a quick reference to some of the most commonly used Git commands.
Git Commands Cheat Sheet
Here's a handy cheat sheet of the most commonly used Git commands, ordered by their usage and popularity:
Git Command | Description |
---|---|
git init |
Initializes a new Git repository. |
git clone <repo> |
Clones a repository into a new directory. |
git status |
Displays the state of the working directory and staging area. |
git add <file> |
Adds a file to the staging area. |
git add . |
Adds all changes in the working directory to the staging area. |
git commit -m "message" |
Commits the staged changes with a descriptive message. |
git log |
Displays a log of all commits in the repository. |
git diff |
Shows changes between commits, commit and working directory, etc. |
git branch |
Lists all branches in the repository. |
git branch <branch_name> |
Creates a new branch. |
git checkout <branch> |
Switches to the specified branch. |
git checkout -b <branch> |
Creates and switches to a new branch. |
git merge <branch> |
Merges the specified branch into the current branch. |
git pull |
Fetches and merges changes from the remote repository to the current branch. |
git push |
Pushes local changes to the remote repository. |
git remote -v |
Shows all remote repositories associated with the local repository. |
git fetch |
Downloads objects and refs from another repository. |
git reset --hard <commit> |
Resets the current branch to the specified commit, discarding all changes in the working directory and staging area. |
git rm <file> |
Removes a file from the working directory and the staging area. |
git stash |
Stashes changes in the working directory that are not ready to be committed. |
git stash pop |
Applies the latest stashed changes and removes them from the stash list. |
git stash list |
Lists all stashed changes. |
git rebase <branch> |
Reapplies commits on top of another base tip. |
git cherry-pick <commit> |
Applies the changes introduced by the specified commit. |
git tag <tag_name> |
Creates a tag for the specified commit. |
git show <tag_name> |
Displays information about the specified tag. |
git blame <file> |
Shows what revision and author last modified each line of a file. |
git log --oneline |
Shows a condensed log where each commit is displayed on a single line. |
git config --global user.name "name" |
Sets the name to be used for commits. |
git config --global user.email "email" |
Sets the email to be used for commits. |
git reflog |
Shows a log of references, including commits that have been moved or deleted. |
git bisect |
Uses binary search to find the commit that introduced a bug. |
git submodule add <repo> |
Adds a new submodule to the repository. |
git submodule update --init |
Initializes, fetches, and checks out submodules. |
git clean -f |
Removes untracked files from the working directory. |
git grep <pattern> |
Searches for the specified pattern in the working directory. |
How to Initialize a Git Repository
git init
: Initialize a New Repository
Description: Sets up a new Git repository in the current directory.
Example:
git init
# Creates a new Git repository in the current folder
How to Clone a Repository
git clone <repo>
: Clone a Remote Repository
Description: Creates a local copy of a remote repository.
Example:
git clone https://github.com/user/repo.git
# Clones the repository to your local system
How to Check the Status of a Repository
git status
: Check Repository Status
Description: Displays the state of your working directory and staging area.
Example:
git status
# Shows which files are modified, staged, or untracked
How to Stage Changes
git add <file>
: Stage a File
Description: Adds a specific file to the staging area.
Example:
git add index.html
# Stages the file "index.html"
git add .
: Stage All Changes
Description: Stages all new, modified, or deleted files.
Example:
git add .
# Stages all changes in the current directory
How to Commit Changes
git commit -m "message"
: Commit Staged Changes
Description: Saves staged changes with a descriptive message.
Example:
git commit -m "Initial commit"
# Commits staged changes with the message "Initial commit"
How to View Commit History
git log
: View Commit History
Description: Displays a detailed log of all commits.
Example:
git log
# Shows a list of all commits with details
git log --oneline
: View Compact Commit History
Description: Displays a concise log, showing each commit on one line.
Example:
git log --oneline
# Shows a compact commit history
How to Work with Branches
git branch
: List All Branches
Description: Displays a list of all branches in the repository.
Example:
git branch
# Lists all branches
git branch <branch_name>
: Create a New Branch
Description: Creates a new branch.
Example:
git branch feature-login
# Creates a branch named "feature-login"
git checkout <branch>
: Switch Branches
Description: Switches to the specified branch.
Example:
git checkout feature-login
# Switches to the "feature-login" branch
git checkout -b <branch>
: Create and Switch to a New Branch
Description: Creates a new branch and switches to it.
Example:
git checkout -b feature-signup
# Creates and switches to "feature-signup"
How to Merge Branches
git merge <branch>
: Merge Branches
Description: Merges the specified branch into the current branch.
Example:
git merge feature-login
# Merges "feature-login" into the current branch
How to Fetch, Pull, and Push Changes
git fetch
: Fetch Updates from Remote
Description: Downloads changes from the remote repository without merging them.
Example:
git fetch origin
# Fetches updates from the "origin" repository
git pull
: Fetch and Merge Updates
Description: Fetches updates from the remote repository and merges them into the current branch.
Example:
git pull origin main
# Fetches and merges updates from "main"
git push
: Push Changes to Remote
Description: Pushes local commits to the remote repository.
Example:
git push origin main
# Pushes changes to the "main" branch in the remote repository
How to Compare Changes
git diff
: Compare Changes
Description: Displays differences between commits or between the working directory and the staging area.
Example:
git diff
# Shows unstaged changes
How to Undo Changes
git reset --hard <commit>
: Reset to a Commit
Description: Resets the current branch to a specific commit, discarding all changes.
Example:
git reset --hard abc123
# Resets the branch to the commit "abc123"
git revert <commit>
: Undo a Commit
Description: Creates a new commit to undo changes from a specific commit.
Example:
git revert abc123
# Undoes the changes made in "abc123"
How to Stash Changes
git stash
: Temporarily Save Changes
Description: Saves uncommitted changes for later use.
Example:
git stash
# Stashes current changes
git stash pop
: Apply Stashed Changes
Description: Applies the most recent stash and removes it from the stash list.
Example:
git stash pop
# Restores the most recent stash
How to Manage Remote Repositories
git remote -v
: List Remote Repositories
Description: Displays the URLs of remote repositories.
Example:
git remote -v
# Lists remote repository URLs
How to Tag Commits
git tag <tag_name>
: Create a Tag
Description: Creates a tag for a specific commit.
Example:
git tag v1.0
# Creates a tag "v1.0"
git show <tag_name>
: Display Tag Information
Description: Displays details about the specified tag.
Example:
git show v1.0
# Shows details of the "v1.0" tag
How to Search in Git
git grep <pattern>
: Search for a Pattern
Description: Searches for a specific pattern in the repository.
Example:
git grep "TODO"
# Searches for "TODO" in the repository
Advanced Git Commands
git rebase <branch>
: Reapply Commits
Description: Moves the current branch to another base commit.
Example:
git rebase main
# Reapplies commits on top of "main"
git cherry-pick <commit>
: Apply a Commit
Description: Applies changes from a specific commit to the current branch.
Example:
git cherry-pick abc123
# Applies changes from "abc123"
Conclusion
Mastering Git commands is essential for efficient and effective code management. This cheat sheet provides a quick reference to some of the most commonly used Git commands, helping you streamline your workflow and collaborate with others more effectively.
By understanding and using these commands, you can simplify your version control process, enhance your productivity, and ensure your projects are well-managed. Keep this guide handy to make the most of Git. Happy coding!
Comments
Post a Comment
Leave Comment