Ultimate Git Commands Cheat Sheet - Comprehensive Guide for Developers
Git is an essential tool for version control in software development. This comprehensive Git commands cheat sheet provides detailed information, use cases, and examples to help you manage your repositories effectively.
Git Configuration
Set Username and Email
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Sets your username and email address for all repositories on your system.
Check Configuration
git config --list
Displays the current configuration settings.
Repository Management
Initialize a Repository
git init
Initializes a new Git repository or reinitializes an existing one.
Clone a Repository
git clone https://github.com/user/repository.git
Creates a copy of an existing repository.
Basic Snapshotting
Check Status
git status
Displays the state of the working directory and the staging area.
Add to Staging Area
git add .
Stages all changes (new files, modifications, deletions).
git add -A
Stages all changes in the entire repository.
git add -u
Stages modifications and deletions, without new files.
Commit Changes
git commit -m "Commit message"
Records the changes to the repository with a descriptive message.
Remove Files
git rm filename
Removes files from the working directory and staging area.
Move or Rename Files
git mv old_filename new_filename
Moves or renames files in the repository.
Branching and Merging
List Branches
git branch
Lists all local branches in the repository.
Create a New Branch
git branch new-branch-name
Creates a new branch.
Create a Branch from an Existing Branch
git checkout -b new-branch-name existing-branch-name
Creates a new branch from the specified existing branch and switches to the new branch.
Switch Branches
git checkout branch-name
Switches to the specified branch.
Merge Branches
git merge branch-name
Merges the specified branch into the current branch.
Delete a Branch
git branch -d branch-name
Deletes the specified branch.
Remote Repositories
Add a Remote
git remote add origin https://github.com/user/repository.git
Adds a remote repository.
View Remotes
git remote -v
Displays the current remotes.
Fetch from Remote
git fetch origin
Downloads objects and refs from another repository.
Pull from Remote
git pull origin main
Fetches and integrates changes from the remote repository into the current branch.
Push to Remote
git push origin main
Uploads local branch commits to the remote repository.
Update Remote URL
git remote set-url origin https://github.com/user/new-repository.git
Updates the URL of the remote repository.
Stashing and Cleaning
Stash Changes
git stash
Stashes the changes in a dirty working directory away.
Apply Stash
git stash apply
Applies the stashed changes.
Drop Stash
git stash drop
Removes the latest stash.
Clean Untracked Files
git clean -f
Removes untracked files from the working directory.
Reverting Changes
Discard Uncommitted Changes
git reset --hard
Discards all uncommitted changes in the working directory, returning to the last committed state.
Revert to a Specific Commit
git reset --hard commit-id
Resets the index and working directory to the specified commit.
History and Inspection
View Commit History
git log
Displays the commit history.
Show a Commit
git show commit-id
Displays the changes introduced by the specified commit.
View Differences
git diff
Shows changes between the working directory and index.
Blame a File
git blame filename
Shows what revision and author last modified each line of a file.
Tagging
Create a Tag
git tag -a 1.0 -m "Version 1.0"
Creates a new tag.
Push a tag to remote
git push origin tag-name
List Tags
git tag
Lists all tags in the repository.
Push Tags
git push origin --tags
Pushes all tags to the remote repository.
Delete a Tag
git tag -d tag-name
Deletes the specified tag locally.
Remove Tag from Remote
git push origin --delete tag tag-name
Deletes the specified tag from the remote repository.
File Permissions
Exclude Permission Changes from Git
git config core.fileMode false
Disables tracking of file permission changes.
Advanced Commands
Rebase Branch
git rebase branch-name
Reapplies commits on top of another base tip.
Cherry-pick Commit
git cherry-pick commit-id
Applies the changes introduced by an existing commit.
Amend Last Commit
git commit --amend -m "New commit message"
Modifies the most recent commit.
Revert a Commit
git revert commit-id
Creates a new commit that undoes the changes from a previous commit.
Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.
Recent Posts
- Disabling Payment Methods in WooCommerce Based on Conditions
- How to Update Product Quantity in WooCommerce Using Custom Code
- Dynamically Generating a Table of Contents in WordPress
- Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
- Understanding the Impact of git reset --hard Command
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.