Mastering Git: 100+ Commands with Real-Time Examples
Introduction
Git is a powerful version control system used to manage code, collaborate on projects, and maintain project history. In this blog, we’ll explore over 100 Git commands categorized and explained with real-world examples.
Basic Setup
git init: Initialize a new Git repository.
Example:git init
Creates a
.git
folder in the directory.git config: Configure user details or preferences.
Example:git config --global user.name "Your Name" git config --global user.email "your-email@example.com"
git clone: Clone an existing repository.
Example:git clone https://github.com/user/repo.git
Branching and Merging
git branch: Manage branches.
Example:git branch feature-branch # Create git branch # List git branch -d feature-branch # Delete
git checkout: Switch branches or commits.
Example:git checkout main git checkout <commit-hash>
git switch: Another way to switch branches.
Example:git switch feature-branch
git merge: Merge branches.
Example:git merge feature-branch
git rebase: Reapply commits onto a new base branch.
Example:git rebase main
git rebase --interactive: Edit, squash, or reorder commits interactively.
Example:git rebase -i HEAD~3
Staging and Committing
git add: Stage changes.
Example:git add file.txt git add . # Stage all changes
git commit: Save changes to the repository.
Example:git commit -m "Initial commit"
git commit --amend: Edit the last commit.
Example:git commit --amend -m "Updated commit message"
Undoing Changes
git reset: Undo commits or changes.
Example:git reset --soft HEAD~1 # Undo commit, keep changes staged git reset --hard HEAD~1 # Undo commit and discard changes
git revert: Create a new commit to reverse changes.
Example:git revert <commit-hash>
git clean: Remove untracked files.
Example:git clean -f
git stash: Save uncommitted changes temporarily.
Example:git stash git stash pop # Apply and remove stash
Viewing History
git log: View commit history.
Example:git log --oneline
git reflog: View all HEAD changes.
Example:git reflog
git blame: Find who made changes to each line of a file.
Example:git blame file.txt
git show: Display changes in a specific commit.
Example:git show <commit-hash>
Collaboration
git fetch: Download updates from a remote repository.
Example:git fetch
git pull: Fetch and merge updates.
Example:git pull origin main
git push: Upload commits to a remote repository.
Example:git push origin feature-branch
git remote: Manage remote repositories.
Example:git remote add origin https://github.com/user/repo.git
Advanced Commands
git cherry-pick: Apply a specific commit to the current branch.
Example:git cherry-pick <commit-hash>
git bisect: Find a bug by binary searching through commits.
Example:git bisect start git bisect bad git bisect good
git tag: Add tags to commits.
Example:git tag v1.0.0 git push --tags
git archive: Create an archive of repository files.
Example:git archive --format=zip HEAD > repo.zip
Patch Management
git format-patch: Generate patch files from commits.
Example:git format-patch HEAD~3
git apply: Apply a patch file.
Example:git apply patch-file.patch
Cleanup
git gc: Clean up unnecessary files.
Example:git gc
git prune: Remove unreachable objects.
Example:git prune
git clean: Remove untracked files and directories.
Example:git clean -fd
Aliases and Shortcuts
git alias: Create custom shortcuts.
Example:git config --global alias.co checkout
git config --edit: Edit Git configurations.
Example:git config --edit
Submodules
Git submodules allow you to manage repositories as subdirectories inside another Git repository. They are particularly useful for managing dependencies or reusable components.
- Add a submodule
git submodule add https://github.com/example/repo.git
Example: Adding a reusable library as a submodule.
- Initialize submodules
git submodule init
Example: Prepares the local repository to use submodules.
- Update submodules
git submodule update
Example: Updates the submodule to the latest commit in the parent repository.
- Deinitialize a submodule
git submodule deinit path/to/submodule
Example: Removes the submodule from your working directory.
Hooks
Git hooks are scripts that are triggered by specific Git actions, allowing you to automate tasks.
- View hooks
git hooks
Example: List all available hooks in your repository.
- Commit-msg hook
echo "Commit message validation" > .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
Example: Validates the format of commit messages.
- Pre-commit hook
echo "Code linting check" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Example: Runs a linter before committing code.
Security
Git provides tools to verify commits and tags to ensure their authenticity.
- View GPG signatures for commits
git log --show-signature
Example: Ensures the commit was made by a verified user.
- Verify a tag
git tag -v v1.0.0
Example: Checks the GPG signature of a tag.
Worktrees
Worktrees allow you to work on multiple branches simultaneously in different directories.
- Add a worktree
git worktree add ../feature-branch feature-branch
Example: Create a separate working directory for a branch.
- Remove a worktree
git worktree remove ../feature-branch
Example: Clean up a worktree when it's no longer needed.
Debugging
Debugging tools help diagnose issues in your repository.
- Check repository health
git fsck
Example: Identifies corrupt or missing objects.
- Verify a commit
git verify-commit <commit-hash>
Example: Checks the integrity of a specific commit.
- Verify a tag
git verify-tag <tag-name>
Example: Ensures a tag is valid and unaltered.
Ignoring Files
Use .gitignore
to exclude files or directories from being tracked.
Example .gitignore file:
# Ignore log files *.log # Ignore node_modules directory node_modules/
Example: Prevent tracking of temporary or dependency files.
Backup and Restore
Backup and restore repositories using Git bundles.
Create a bundle
git bundle create backup.bundle main
Example: Creates a backup of the
main
branch.Verify a bundle
git bundle verify backup.bundle
Example: Ensures the bundle is valid.
Collaboration (Code Review)
Use Git's diff tools to review changes effectively.
Word-level diff
git diff --word-diff
Example: Highlights changes at the word level.
Compare two commits
git diff <commit1>..<commit2>
Example: Shows the differences between two commits.
Show changed files only
git diff --name-only
Example: Lists files modified between commits.
Miscellaneous Commands
Other useful Git commands for specific tasks.
Archive a repository
git archive --format=zip --output=repo.zip main
Example: Creates a zip archive of the repository.
Sparse checkout
git sparse-checkout set <folder>
Example: Checks out only a specific folder.
Set default branch name
git config --global init.defaultBranch main
Example: Ensures all new repositories use
main
as the default branch.View file tree of a commit
git diff-tree --no-commit-id --name-only -r <commit-hash>
Example: Displays the file structure of a commit.
List tracked files
git ls-files
Example: Lists all tracked files in the repository.
Git Help
Git has built-in help documentation for quick reference.
View all help topics
git help -a
Example: Lists all available Git commands.
View config help
git help config
Example: Displays detailed help for the
config
command.
Collaboration with CI/CD
Git integrates seamlessly with CI/CD workflows.
Tagging for releases
git tag -a v1.0.0 -m "Release 1.0.0" git push origin v1.0.0
Example: Tags a release and pushes it to the remote repository.
Squashing commits for cleaner history
git rebase -i HEAD~3
Example: Combines multiple commits into one for a cleaner history.
Force push for overrides
git push --force
Example: Overwrites remote changes (use with caution).