Skip to main content

Comprehensive Guide to Git Commands

Comprehensive Guide to Git Commands
Deepak Kamboj
Senior Software Engineer
7 min read
Git

This document provides a detailed overview of commonly used Git commands, organized by category.

Git Configuration

TaskCommandDescription
Configure usernamegit config --global user.name "<name>"Sets the username for all repositories on your system.
Configure emailgit config --global user.email "<email>"Sets the email address for all repositories on your system.
Configure default text editorgit config --global core.editor <editor>Sets the default text editor for Git commands.
View configuration settingsgit config --listDisplays all Git configuration settings.
Configure line ending conversionsgit config --global core.autocrlf <true/false/input>Configures automatic conversion of line endings (CRLF/LF).

Creating Repositories

TaskCommandDescription
Initialize a new repositorygit initInitializes a new Git repository in the current directory.
Clone an existing repositorygit clone <repository_url>Creates a copy of an existing Git repository.
Clone a repository to a specific foldergit clone <repository_url> <folder_name>Clones a repository into a specified directory.

Staging and Committing

TaskCommandDescription
Check repository statusgit statusShows the working directory and staging area status.
Stage a filegit add <file_name>Adds a file to the staging area.
Stage all filesgit add .Adds all changes in the current directory to the staging area.
Commit changesgit commit -m "<commit_message>"Commits the staged changes with a message.
Commit with a detailed messagegit commitOpens the default editor to write a detailed commit message.
Skip staging and commit directlygit commit -a -m "<commit_message>"Stages all modified files and commits them with a message.
Amend the last commitgit commit --amendModifies the last commit with additional changes or a new commit message.

Branching and Merging

TaskCommandDescription
List all branchesgit branchLists all local branches.
Create a new branchgit branch <branch_name>Creates a new branch without switching to it.
Create and switch to a new branchgit checkout -b <branch_name>Creates and switches to a new branch.
Switch to an existing branchgit checkout <branch_name>Switches to the specified branch.
Delete a branchgit branch -d <branch_name>Deletes the specified branch (only if merged).
Force delete a branchgit branch -D <branch_name>Forcefully deletes the specified branch.
Merge a branch into the current branchgit merge <branch_name>Merges the specified branch into the current branch.
Abort a mergegit merge --abortAborts the current merge and resets the branch to its pre-merge state.
Rebase the current branchgit rebase <branch_name>Reapplies commits on top of another base branch.

Remote Repositories

TaskCommandDescription
Add a remote repositorygit remote add <name> <url>Adds a new remote repository with the specified name.
View remote repositoriesgit remote -vDisplays the URLs of all remotes.
Remove a remote repositorygit remote remove <name>Removes the specified remote repository.
Rename a remote repositorygit remote rename <old_name> <new_name>Renames a remote repository.
Fetch changes from a remote repositorygit fetch <remote>Downloads objects and refs from another repository.
Pull changes from a remote repositorygit pull <remote> <branch>Fetches and merges changes from the specified branch of a remote repository into the current branch.
Push changes to a remote repositorygit push <remote> <branch>Pushes local changes to the specified branch of a remote repository.
Push all branches to a remotegit push --all <remote>Pushes all branches to the specified remote.
Push tags to a remote repositorygit push --tagsPushes all tags to the specified remote repository.

Inspecting and Comparing

TaskCommandDescription
View commit historygit logShows the commit history for the current branch.
View a simplified commit historygit log --oneline --graph --allDisplays a compact, graphical commit history for all branches.
Show commit detailsgit show <commit_hash>Shows the changes introduced by a specific commit.
Compare branchesgit diff <branch_1> <branch_2>Shows differences between two branches.
Compare staged and working directorygit diff --stagedShows differences between the staging area and the last commit.
Compare changes with the last commitgit diff HEADCompares the working directory with the latest commit.

Undoing Changes

TaskCommandDescription
Revert changes in a filegit checkout -- <file_name>Discards changes in the working directory for a specific file.
Reset staging areagit reset <file_name>Removes a file from the staging area without changing the working directory.
Reset to a specific commitgit reset --hard <commit_hash>Resets the working directory and staging area to the specified commit, discarding all changes.
Soft reset to a commitgit reset --soft <commit_hash>Resets the staging area to the specified commit, keeping changes in the working directory.
Revert a commitgit revert <commit_hash>Creates a new commit that undoes the changes of a specified commit.
Remove untracked filesgit clean -fRemoves untracked files from the working directory.
Remove untracked directoriesgit clean -fdRemoves untracked directories and their contents from the working directory.

Tagging

TaskCommandDescription
List all tagsgit tagLists all tags in the repository.
Create a new taggit tag <tag_name>Creates a new lightweight tag.
Create an annotated taggit tag -a <tag_name> -m "<message>"Creates a new annotated tag with a message.
Show tag detailsgit show <tag_name>Displays details about the specified tag.
Delete a taggit tag -d <tag_name>Deletes the specified tag locally.
Push a tag to a remote repositorygit push <remote> <tag_name>Pushes a tag to the specified remote repository.
Push all tags to a remote repositorygit push --tagsPushes all local tags to the remote repository.
Delete a tag from a remote repositorygit push <remote> :refs/tags/<tag_name>Deletes a tag from the specified remote repository.

Stashing

TaskCommandDescription
Stash changesgit stashStashes current changes in the working directory and staging area.
List all stashesgit stash listDisplays a list of all stashes.
Apply a stashgit stash apply <stash_name>Applies a specific stash to the working directory.
Apply and drop a stashgit stash pop <stash_name>Applies the latest stash and removes it from the stash list.
Drop a stashgit stash drop <stash_name>Removes a specific stash from the stash list.
Clear all stashesgit stash clearRemoves all stashes from the stash list.

Submodules

TaskCommandDescription
Add a submodulegit submodule add <repository_url> <path>Adds a submodule to the repository.
Initialize submodulesgit submodule initInitializes local configuration for submodules.
Update submodulesgit submodule updateFetches and checks out the latest changes in submodules.
View submodule statusgit submodule statusDisplays the status of submodules.
Deinitialize a submodulegit submodule deinit <path>Deinitializes a submodule and removes its working directory.

Miscellaneous

TaskCommandDescription
View Git versiongit --versionDisplays the currently installed version of Git.
View help for a commandgit <command> --helpShows the help manual for a specific Git command.
View a summary of changesgit shortlogSummarizes commits by author.
Create a Git archivegit archive --format=zip --output=<file.zip> <branch>Creates a compressed archive of a repository.
Reapply changes from another branchgit cherry-pick <commit_hash>Applies changes from a specific commit to the current branch.
Rebase interactivelygit rebase -i <base_commit>Allows for interactive rebasing, which lets you reorder, squash, or drop commits.

Useful Aliases

AliasCommandDescription
git histgit log --oneline --graph --all --decorateShows a pretty and concise graph of the commit history.
git lggit log --graph --pretty=oneline --abbrev-commit --decorate --allA compact view of the commit history.
git stgit statusShortcut for viewing the current status.
git cigit commit -mShortcut for committing with a message.
git cogit checkoutShortcut 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.