This document provides a detailed overview of commonly used Git commands, organized by category.
Git Configuration
| Task | Command | Description |
|---|
| Configure username | git config --global user.name "<name>" | Sets the username for all repositories on your system. |
| Configure email | git config --global user.email "<email>" | Sets the email address for all repositories on your system. |
| Configure default text editor | git config --global core.editor <editor> | Sets the default text editor for Git commands. |
| View configuration settings | git config --list | Displays all Git configuration settings. |
| Configure line ending conversions | git config --global core.autocrlf <true/false/input> | Configures automatic conversion of line endings (CRLF/LF). |
Creating Repositories
| Task | Command | Description |
|---|
| Initialize a new repository | git init | Initializes a new Git repository in the current directory. |
| Clone an existing repository | git clone <repository_url> | Creates a copy of an existing Git repository. |
| Clone a repository to a specific folder | git clone <repository_url> <folder_name> | Clones a repository into a specified directory. |
Staging and Committing
| Task | Command | Description |
|---|
| Check repository status | git status | Shows the working directory and staging area status. |
| Stage a file | git add <file_name> | Adds a file to the staging area. |
| Stage all files | git add . | Adds all changes in the current directory to the staging area. |
| Commit changes | git commit -m "<commit_message>" | Commits the staged changes with a message. |
| Commit with a detailed message | git commit | Opens the default editor to write a detailed commit message. |
| Skip staging and commit directly | git commit -a -m "<commit_message>" | Stages all modified files and commits them with a message. |
| Amend the last commit | git commit --amend | Modifies the last commit with additional changes or a new commit message. |
Branching and Merging
| Task | Command | Description |
|---|
| List all branches | git branch | Lists all local branches. |
| Create a new branch | git branch <branch_name> | Creates a new branch without switching to it. |
| Create and switch to a new branch | git checkout -b <branch_name> | Creates and switches to a new branch. |
| Switch to an existing branch | git checkout <branch_name> | Switches to the specified branch. |
| Delete a branch | git branch -d <branch_name> | Deletes the specified branch (only if merged). |
| Force delete a branch | git branch -D <branch_name> | Forcefully deletes the specified branch. |
| Merge a branch into the current branch | git merge <branch_name> | Merges the specified branch into the current branch. |
| Abort a merge | git merge --abort | Aborts the current merge and resets the branch to its pre-merge state. |
| Rebase the current branch | git rebase <branch_name> | Reapplies commits on top of another base branch. |
Remote Repositories
| Task | Command | Description |
|---|
| Add a remote repository | git remote add <name> <url> | Adds a new remote repository with the specified name. |
| View remote repositories | git remote -v | Displays the URLs of all remotes. |
| Remove a remote repository | git remote remove <name> | Removes the specified remote repository. |
| Rename a remote repository | git remote rename <old_name> <new_name> | Renames a remote repository. |
| Fetch changes from a remote repository | git fetch <remote> | Downloads objects and refs from another repository. |
| Pull changes from a remote repository | git pull <remote> <branch> | Fetches and merges changes from the specified branch of a remote repository into the current branch. |
| Push changes to a remote repository | git push <remote> <branch> | Pushes local changes to the specified branch of a remote repository. |
| Push all branches to a remote | git push --all <remote> | Pushes all branches to the specified remote. |
| Push tags to a remote repository | git push --tags | Pushes all tags to the specified remote repository. |
Inspecting and Comparing
| Task | Command | Description |
|---|
| View commit history | git log | Shows the commit history for the current branch. |
| View a simplified commit history | git log --oneline --graph --all | Displays a compact, graphical commit history for all branches. |
| Show commit details | git show <commit_hash> | Shows the changes introduced by a specific commit. |
| Compare branches | git diff <branch_1> <branch_2> | Shows differences between two branches. |
| Compare staged and working directory | git diff --staged | Shows differences between the staging area and the last commit. |
| Compare changes with the last commit | git diff HEAD | Compares the working directory with the latest commit. |
Undoing Changes
| Task | Command | Description |
|---|
| Revert changes in a file | git checkout -- <file_name> | Discards changes in the working directory for a specific file. |
| Reset staging area | git reset <file_name> | Removes a file from the staging area without changing the working directory. |
| Reset to a specific commit | git reset --hard <commit_hash> | Resets the working directory and staging area to the specified commit, discarding all changes. |
| Soft reset to a commit | git reset --soft <commit_hash> | Resets the staging area to the specified commit, keeping changes in the working directory. |
| Revert a commit | git revert <commit_hash> | Creates a new commit that undoes the changes of a specified commit. |
| Remove untracked files | git clean -f | Removes untracked files from the working directory. |
| Remove untracked directories | git clean -fd | Removes untracked directories and their contents from the working directory. |
Tagging
| Task | Command | Description |
|---|
| List all tags | git tag | Lists all tags in the repository. |
| Create a new tag | git tag <tag_name> | Creates a new lightweight tag. |
| Create an annotated tag | git tag -a <tag_name> -m "<message>" | Creates a new annotated tag with a message. |
| Show tag details | git show <tag_name> | Displays details about the specified tag. |
| Delete a tag | git tag -d <tag_name> | Deletes the specified tag locally. |
| Push a tag to a remote repository | git push <remote> <tag_name> | Pushes a tag to the specified remote repository. |
| Push all tags to a remote repository | git push --tags | Pushes all local tags to the remote repository. |
| Delete a tag from a remote repository | git push <remote> :refs/tags/<tag_name> | Deletes a tag from the specified remote repository. |
Stashing
| Task | Command | Description |
|---|
| Stash changes | git stash | Stashes current changes in the working directory and staging area. |
| List all stashes | git stash list | Displays a list of all stashes. |
| Apply a stash | git stash apply <stash_name> | Applies a specific stash to the working directory. |
| Apply and drop a stash | git stash pop <stash_name> | Applies the latest stash and removes it from the stash list. |
| Drop a stash | git stash drop <stash_name> | Removes a specific stash from the stash list. |
| Clear all stashes | git stash clear | Removes all stashes from the stash list. |
Submodules
| Task | Command | Description |
|---|
| Add a submodule | git submodule add <repository_url> <path> | Adds a submodule to the repository. |
| Initialize submodules | git submodule init | Initializes local configuration for submodules. |
| Update submodules | git submodule update | Fetches and checks out the latest changes in submodules. |
| View submodule status | git submodule status | Displays the status of submodules. |
| Deinitialize a submodule | git submodule deinit <path> | Deinitializes a submodule and removes its working directory. |
Miscellaneous
| Task | Command | Description |
|---|
| View Git version | git --version | Displays the currently installed version of Git. |
| View help for a command | git <command> --help | Shows the help manual for a specific Git command. |
| View a summary of changes | git shortlog | Summarizes commits by author. |
| Create a Git archive | git archive --format=zip --output=<file.zip> <branch> | Creates a compressed archive of a repository. |
| Reapply changes from another branch | git cherry-pick <commit_hash> | Applies changes from a specific commit to the current branch. |
| Rebase interactively | git rebase -i <base_commit> | Allows for interactive rebasing, which lets you reorder, squash, or drop commits. |
Useful Aliases
| Alias | Command | Description |
|---|
git hist | git log --oneline --graph --all --decorate | Shows a pretty and concise graph of the commit history. |
git lg | git log --graph --pretty=oneline --abbrev-commit --decorate --all | A compact view of the commit history. |
git st | git status | Shortcut for viewing the current status. |
git ci | git commit -m | Shortcut for committing with a message. |
git co | git checkout | Shortcut for switching branches or restoring files. |
This guide provides a solid foundation for working with Git, whether you're just getting started or need a quick reference for more advanced tasks.