Comprehensive Guide to Git Commands and Usage
Contents
1 Introduction 2
2 Configuration Commands 2
3 Repository Management 2
4 Branching Commands 3
5 Committing Changes 4
6 Merging and Rebasing 4
7 Remote Repositories 5
8 Inspecting Changes 5
9 Undoing Changes 6
10 Stashing Changes 6
11 Tagging 7
12 Advanced Commands 7
13 Additional Resources 8
1
1 Introduction
This document provides a comprehensive guide to Git commands, their usage, and examples.
Git is a distributed version control system used for tracking changes in source code during
software development. This guide is organized into sections covering configuration, repository
management, branching, committing, merging, remote repositories, and advanced commands.
2 Configuration Commands
These commands set up and manage Git configuration settings.
• Set user information
git config --global [Link] "Your Name"
git config --global [Link] "[Link]@[Link]"
Sets the name and email associated with your commits.
• View configuration
git config --list
Displays all Git configuration settings.
• Set default editor
git config --global [Link] "vim"
Configures the default text editor for Git (e.g., vim, nano).
• Enable color output
git config --global [Link] auto
Enables colored output for Git commands.
3 Repository Management
Commands to initialize and manage Git repositories.
• Initialize a new repository
git init
Creates a new Git repository in the current directory.
• Clone a repository
git clone <repository-url>
Creates a local copy of a remote repository.
2
• Check repository status
git status
Shows the current state of the working directory and staging area.
• Add files to staging
git add <file>
git add .
Stages specific files or all changes for the next commit.
4 Branching Commands
Commands for creating and managing branches.
• List branches
git branch
Lists all branches in the repository; the current branch is marked with an asterisk.
• Create a new branch
git branch <branch-name>
Creates a new branch without switching to it.
• Switch branches
git checkout <branch-name>
Switches to the specified branch.
• Create and switch to a new branch
git checkout -b <branch-name>
Creates a new branch and switches to it.
• Delete a branch
git branch -d <branch-name>
Deletes a branch that has been merged.
3
5 Committing Changes
Commands for recording changes to the repository.
• Commit staged changes
git commit -m "Commit message"
Records staged changes with a descriptive message.
• Amend the last commit
git commit --amend
Modifies the most recent commit (e.g., to change the message or add files).
• View commit history
git log
Displays the commit history for the repository.
• View a concise log
git log --oneline
Shows a simplified commit history with one line per commit.
6 Merging and Rebasing
Commands to integrate changes from different branches.
• Merge a branch
git merge <branch-name>
Merges the specified branch into the current branch.
• Rebase a branch
git rebase <branch-name>
Reapplies commits from the current branch onto another branch.
• Resolve merge conflicts
git mergetool
Opens a tool to help resolve merge conflicts.
• Abort a merge
git merge --abort
Cancels a merge in progress and restores the previous state.
4
7 Remote Repositories
Commands for working with remote repositories.
• Add a remote repository
git remote add origin <repository-url>
Links a local repository to a remote repository.
• View remote repositories
git remote -v
Lists all remote repositories and their URLs.
• Push changes to remote
git push origin <branch-name>
Sends local commits to the specified remote branch.
• Pull changes from remote
git pull origin <branch-name>
Fetches and merges changes from the remote branch.
• Fetch remote changes
git fetch origin
Downloads changes from the remote repository without merging.
8 Inspecting Changes
Commands to view changes and differences.
• View changes in working directory
git diff
Shows differences between the working directory and the staging area.
• View staged changes
git diff --staged
Displays differences between the staging area and the last commit.
• View changes between commits
git diff <commit1> <commit2>
Compares changes between two commits.
5
9 Undoing Changes
Commands to revert or reset changes.
• Unstage changes
git restore --staged <file>
Removes a file from the staging area without discarding changes.
• Discard changes in working directory
git restore <file>
Reverts changes in a file to the last committed state.
• Reset to a previous commit
git reset <commit>
Moves the branch pointer to a specified commit, optionally discarding changes.
• Revert a commit
git revert <commit>
Creates a new commit that undoes the changes from a specified commit.
10 Stashing Changes
Commands to temporarily save changes.
• Stash changes
git stash
Saves changes in the working directory and staging area to a stash.
• List stashes
git stash list
Shows all stashed changes.
• Apply a stash
git stash apply
Reapplies the most recent stash without removing it.
• Drop a stash
git stash drop
Removes the most recent stash.
6
11 Tagging
Commands for creating and managing tags.
• Create a tag
git tag <tag-name>
Creates a lightweight tag at the current commit.
• Create an annotated tag
git tag -a <tag-name> -m "Tag message"
Creates a tag with a message.
• List tags
git tag
Lists all tags in the repository.
• Push tags to remote
git push origin <tag-name>
Sends a tag to the remote repository.
12 Advanced Commands
Commands for more complex Git operations.
• Cherry-pick a commit
git cherry-pick <commit>
Applies a specific commit to the current branch.
• Interactive rebase
git rebase -i <commit>
Opens an interactive interface to edit, squash, or reorder commits.
• Bisect to find bugs
git bisect start
git bisect bad
git bisect good <commit>
Uses binary search to find the commit that introduced a bug.
7
• Clean untracked files
git clean -f
Removes untracked files from the working directory.
13 Additional Resources
For more information, refer to the official Git documentation:
• Official Git Documentation
• Pro Git Book