Comprehensive Guide to Git Commands: From Basics to Advanced Operations
This document provides a detailed overview of essential Git commands, including
examples for practical understanding.
# Fundamental Git Commands:
1. Initialize a Git Repository:
Command: git init
Description: Creates a new Git repository in the current directory.
Example: git init my_project initializes a new repository named "my_project".
2. Stage Changes for Commit:
Command: git add <file> or git add .
Description: Stages files for inclusion in the next commit. Use . to stage all
tracked files.
Example: git add README.md stages the "README.md" file for commit.
3. Commit Staged Changes:
Command: git commit -m "descriptive commit message"
Description: Commits staged changes with a descriptive message explaining the
changes.
Example: git commit -m "Add initial project structure and README.md" commits staged
changes with a specific message.
4. Undo Staging:
Command: git reset HEAD <file> or git reset HEAD .
Description: Unstages previously staged files. Use . to unstage all files.
Example: git reset HEAD new_file.txt unstages the "new_file.txt" file.
5. Track New Files:
Command: git add <file>
Description: Starts tracking new files for future commits.
Example: git add images/logo.png tracks the "logo.png" file located in the "images"
folder.
6. Untrack Files:
Command: git rm <file> or git rm --cached <file>
Description: Stops tracking existing files. Use --cached to only remove files from
the staging area without deleting them.
Example: git rm old_script.py removes the "old_script.py" file from tracking and
deletes it.
7. Ignore Files:
Command: echo <file> >> .gitignore
Description: Adds files to the ".gitignore" file to prevent Git from tracking
unwanted files.
Example: echo compiled_files >> .gitignore ignores all files with the
"compiled_files" extension.
8. Status Check:
Command: git status
Description: Displays the current state of the working directory, staging area, and
branch.
Example: git status shows untracked files, staged changes, and branch information.
9. View Commit History:
s
Command: git log
Description: Shows a log of commits made to the current branch, displaying commit
messages, timestamps, and author details.
Example: git log --oneline displays a one-line summary of commits for easier
browsing.
# Branching and Merging:
1. Create a New Branch:
Command: git branch <branch-name>
Description: Creates a new branch from the current commit.
Example: git branch feature/new_feature creates a new branch named
"feature/new_feature".
2. Switch to a Branch:
Command: git checkout <branch-name>
Description: Switches to an existing branch.
Example: git checkout develop switches to the "develop" branch.
3. Merge a Branch:
Command: git merge <branch-name>
Description: Merges changes from another branch into the current branch.
Example: git merge feature/new_feature merges changes from the
"feature/new_feature" branch into the current branch.
4. Delete a Branch:
Command: git branch -d <branch-name> or git push --delete origin <branch-name>
Description: Deletes both local and remote branches.
Example: git branch -d feature/old_feature removes the "feature/old_feature" branch
locally.
Sure, here is the continuation of the comprehensive guide to Git commands:
# Advanced Git Operations:
1. Hard Reset to Remote Branch:
Command:git reset --hard origin/<branch-name>
Description: Forces the local branch to match the specified remote branch,
discarding any uncommitted changes.
Example:git reset --hard origin/master resets the local "master" branch to match
the remote "master" branch.
2. Rebasing Local Changes:
Command:git rebase origin/<branch-name>
Description: Applies local changes on top of the latest remote changes, maintaining
a linear commit history.
Example:git rebase origin/develop rebases local changes on top of the remote
"develop" branch.
3. Force Push after Rebase:
Command:git push --force
Description: Updates the remote branch with rebased changes, even if it rewrites
commit history.
Example:git push --force origin feature/new_feature updates the remote
"feature/new_feature" branch with rebased changes.
4. View Uncommitted Changes:
Command:git diff
Description: Displays uncommitted changes in the working directory.
Example:git diff shows changes made to files since the last commit.
5. View Differences Between Commits:
Command:git diff <commit-hash1> <commit-hash2>
Description: Shows differences between two specific commits.
Example:git diff HEAD~2 HEAD compares the current commit with the second-to-last
commit.
6. Amend Latest Commit Message:
Command:git commit --amend
Description: Modifies the message of the most recent commit without changing its
contents.
Example:git commit --amend -m "More accurate commit message" improves the message
of the latest commit.
7. Interactive Rebase:
Command:git rebase -i <branch-name>
Description: Reorganizes commit history interactively, allowing for squashing,
rewording, or deleting commits.
Example:git rebase -i master opens an editor to modify the commit history of the
"master" branch.
8. Squash, Reorder, or Delete Commits:
Command:git rebase -i HEAD~<number-of-commits>
Description: Squashes, reorders, or deletes commits in an interactive rebase
session.
Example:git rebase -i HEAD~3 allows editing the last three commits in the current
branch.
9. Cherry-pick Specific Commits:
Command:git cherry-pick <commit-hash>
Description: Applies a specific commit from one branch to another.
Example:git cherry-pick 1234567 picks the commit with the hash "1234567" and
applies it to the current branch.
10. Stash Uncommitted Changes:
Command:git stash
Description: Temporarily saves uncommitted changes to a stash, allowing for
switching branches or working on other tasks.
Example:git stash saves uncommitted changes for later use.
11. Apply Stashed Changes:
Command:git stash apply
Description: Restores uncommitted changes from the stash to the working directory.
Example:git stash apply restores the most recent stashed changes.