Git Cheatsheet
Git Cheatsheet
Git is a version control system that allows you to track changes to files and folders. It’s a
powerful tool that can be used for everything from small personal projects to large-
scale enterprise applications.
This guide is a quick reference to the most common Git commands. It’s not meant to be
a comprehensive guide to Git, but rather a quick reference to the most common
commands.
Here’s the revised git cheatsheet with improved section titles and reorganized:
# Track a remote branch and set up the local branch to automatically sync with it
git branch --track <branch_name> <remote_name>/<remote_branch>
# Set an existing local branch to track a remote branch
git branch -u <remote_name>/<remote_branch>
# Push a branch to a remote repository and set it to track the remote branch
git push -u <remote_name> <local_branch>
# Remove the tracking association between a local and a remote branch
git branch --unset-upstream <branch_name>
Commit History
# Show commit history
git log
# Display a condensed commit history
git log --oneline
# Show branching commit history
git log --graph
# Filter commit history by author
git log --author=<author_name>
# Show commit history since specific date
git log --since=<date>
# Show commit history until specific date
git log --until=<date>
Tags
# List all tags
git tag
# Create a new tag at a specific commit
git tag <tag_name> <commit_id>
# Create an annotated tag with a message
git tag -a <tag_name> -m "tag message"
# Delete a specific tag
git tag -d <tag_name>
# Delete a specific remote tag
git push <remote_name> --delete <tag_name>
# Show information about a specific tag
git show <tag_name>
Stashes
# Temporarily save changes in the working tree
git stash save "stash message"
# List all stashes
git stash list
# Apply changes from a specific stash
git stash apply <stash>
# Remove a specific stash
git stash drop <stash>
# Remove all stashes
git stash clear
Cherry-Picking
# Apply a specific commit from one branch to another
git cherry-pick <commit_id>
Commit Management
# Modify the latest commit
git commit --amend
# Create a new commit that undoes changes from a previous commit
git revert <commit_id>
# Discard changes and move HEAD to a specific commit
git reset --hard <commit_id>
# Move HEAD to a specific commit, but preserve staged changes
git reset --soft <commit_id>
# Show a record of all changes made to the local repository head
git reflog
Submodules, Subtrees, and Advanced Submodules
# Add a submodule to the current repository
git submodule add <repository_url> <path>
# Initialize and update all submodules recursively
git submodule update --init --recursive
# Add a subtree to the current repository
git subtree add --prefix=<path> <repository_url>
# Initialize the submodules in the repository
git submodule init
# Update the submodules to their latest commits
git submodule update
# Execute a specific command in each submodule
git submodule foreach <command>
# Unregister a submodule
git submodule deinit <path>
Hooks and Automation, and Diff and Merge Tools
# Locate hooks directory in the Git repository (usually in .git/hooks/)
git hooks
# Script names for specific hooks that can be added to the hooks directory
pre-commit, post-commit, pre-push, post-merge, etc.
# Make a hook script executable to ensure it's triggered when necessary
chmod +x <hook_script>
Work with Patches
# Generate a patch file for a specific commit
git format-patch <commit_id>
# Apply a patch to the current branch
git apply <patch_file>
# Apply a patch using the "git am" (apply mailbox) command
git am <patch_file>
Collaboration
# Generate a request-pull summary with the changes between two commits
git request-pull <start_commit> <end_commit> <url>
# Summarize the commit history, listing authors and their contributions
git shortlog
# List all files tracked by Git
git ls-files
# Search for a specified pattern in files tracked by Git
git grep <pattern>
Bisecting, Debugging, and Performance Issues
# Begin a bisect session to find the commit that introduced a bug
git bisect start
# Mark a commit as "bad," indicating it contains the bug
git bisect bad <commit_id>
# Mark a commit as "good," indicating it does not contain the bug
git bisect good <commit_id>
# End the bisect session and return to the original branch/commit
git bisect reset
# Verify the integrity of the Git repository
git fsck
# Run garbage collection to optimize the repository's performance
git gc
# Remove untracked files and directories (use with caution)
git clean –df
Tips and Tricks
# Interactively choose parts (hunks) of files to stage
git add -p
# Show the commit history and associated patches for a specific file
git log -p <file_name>
# Customize the format of the git log output
git log --pretty=format:"%h - %an, %ar : %s"
# Find text in commit messages (useful for locating specific changes)
git log --grep="<text>"
# Quickly view the changes in the working directory since the last commit
git diff --stat
# Display the branch history with decoration to see where branches have split or merged
git log --oneline --decorate --graph
# Set the git output pager to quit when the output is less than one screen, and not clear the
screen after displaying
git config --global core.pager 'less -RFX'
# Use Git's auto-correct feature to fix mistyped commands
git config --global help.autocorrect 1
# List aliases for Git commands
git config --get-regexp alias