Skip to content
Git Commands Summary
Managing Your Repository
git init → Initializes a new Git repository.
- Example:
git init my-project (Creates a Git repository in my-project).
git clone <url> → Creates a local copy of a remote repository.
- Example:
git clone https://github.com/user/repo.git (Copies the repository to your machine).
git remote add <name> <url> → Adds a remote repository reference.
- Example:
git remote add origin https://github.com/user/repo.git (Links your local repository to a GitHub repository).
Tracking Changes
git add . → Stages all modified files for committing.
- Example:
git add . (Prepares all changed files for commit).
git commit --amend -m "<message>" → Modifies the last commit message.
- Example:
git commit --amend -m "Updated commit message" (Replaces the message without creating a new commit).
Branching and Merging
git branch -r → Lists all remote branches.
- Example:
git branch -r (Shows branches on a remote repository).
git branch -m <old> <new> → Renames a branch.
- Example:
git branch -m old-branch new-branch (Renames old-branch to new-branch).
git merge --no-ff <branch> → Merges a branch while keeping history intact.
- Example:
git merge --no-ff feature-branch (Merges feature-branch with a full record of changes).
Undoing and Resetting Changes
git reset --soft HEAD~1 → Removes the last commit but keeps changes staged.
- Example:
git reset --soft HEAD~1 (Deletes the last commit but keeps changes in staging).
git reset --mixed HEAD~1 → Removes the last commit and unstages the changes.
- Example:
git reset --mixed HEAD~1 (Deletes the commit and moves changes back to working directory).
git reset --hard HEAD~1 → Removes the last commit and discards changes.
- Example:
git reset --hard HEAD~1 (Erases everything related to the last commit).
Stashing Work
git stash push -m "<message>" → Saves changes with a name.
- Example:
git stash push -m "Work in progress" (Stores your unfinished changes).
git stash list → Displays saved stashes.
- Example:
git stash list (Shows all your stored stashes).
git stash apply stash@{1} → Restores a specific stash.
- Example:
git stash apply stash@{1} (Brings back an earlier stash).
Tagging Releases
git tag -a v1.0 -m "Version 1.0 release" → Creates an annotated tag.
- Example:
git tag -a v1.0 -m "Initial release" (Adds version tracking to the repository).
git tag -l "v*" → Lists all tags matching a pattern.
- Example:
git tag -l "v*" (Finds all tags beginning with v).
Cleaning Up History
git rebase -i HEAD~4 → Rewrites the last four commits interactively.
- Example:
git rebase -i HEAD~4 (Lets you edit or reorder the last four commits).
git reflog → Shows every reference update, including deleted commits.
- Example:
git reflog (Displays history of all commits—even those removed).
Finding and Comparing Changes
git grep "<keyword>" → Searches for a keyword inside the repository.
- Example:
git grep "functionName" (Finds all occurrences of functionName).
git bisect start → Helps locate the commit that introduced a bug.
- Example:
git bisect start (Begins binary search for a problematic commit).
git bisect bad → Marks the current version as faulty.
- Example:
git bisect bad (Indicates the current commit has an issue).
git bisect good <commit> → Marks a specific commit as working fine.
- Example:
git bisect good abc123 (Tells Git that abc123 is a good commit).
git bisect reset → Ends the bisect session.
- Example:
git bisect reset (Stops the search for a problematic commit).
Working with Submodules
git submodule add <url> <path> → Adds a submodule inside a repository.
- Example:
git submodule add https://github.com/user/library lib/ (Adds library to the lib folder).
git submodule update --init --recursive → Initializes submodules.
- Example:
git submodule update --init --recursive (Ensures all submodules are downloaded).
git submodule foreach git pull origin main → Updates all submodules.
- Example:
git submodule foreach git pull origin main (Fetches latest changes for all submodules).
Optimizing and Cleaning Up
git gc --prune=now → Removes unnecessary objects and optimizes storage.
- Example:
git gc --prune=now (Cleans up the repository).
git fsck → Checks the repository for corruption or inconsistencies.
- Example:
git fsck (Runs a consistency check).
git prune → Deletes unreachable Git objects to free space.
- Example:
git prune (Cleans up orphaned data).