Git is a powerful version control system that allows developers to manage and track changes in their code efficiently.
This comprehensive list includes essential Git commands organized by categories, providing descriptions and examples for each command.
Whether you’re a beginner or an experienced developer, this guide will help you navigate Git effectively.
Getting & Creating Projects
git init: Initializes a new Git repository in the current directory.- Example:
git init git clone <repository-url>: Creates a local copy of a remote repository.- Example:
git clone https://github.com/user/repo.git
Basic Snapshotting
git status: Displays the state of the working directory and staging area.- Example:
git status git add <file>: Adds a specific file to the staging area.- Example:
git add file.txt git add -A: Stages all changes (new, modified, deleted files).- Example:
git add -A git commit -m "<message>": Commits the staged changes with a descriptive message.- Example:
git commit -m "Initial commit" git rm <file>: Removes a file from the working directory and stages the removal.- Example:
git rm file.txt
Branching & Merging
git branch: Lists all local branches.- Example:
git branch git branch -a: Lists all local and remote branches.- Example:
git branch -a git branch <branch-name>: Creates a new branch with the specified name.- Example:
git branch feature-xyz git branch -d <branch-name>: Deletes a local branch that has been merged.- Example:
git branch -d feature-xyz git branch -D <branch-name>: Forces the deletion of a local branch, regardless of its merge status.- Example:
git branch -D feature-xyz git push origin --delete <branch-name>: Deletes a branch from the remote repository.- Example:
git push origin --delete feature-xyz git checkout -b <branch-name>: Creates a new branch and switches to it.- Example:
git checkout -b feature-xyz git checkout <branch-name>: Switches to the specified branch.- Example:
git checkout main git merge <branch-name>: Merges the specified branch into the current branch.- Example:
git merge feature-xyz git stash: Stashes changes in the working directory for later use.- Example:
git stash git stash pop: Applies the stashed changes back to the working directory.- Example:
git stash pop
Sharing & Updating Projects
git push origin <branch-name>: Pushes the specified branch to the remote repository.- Example:
git push origin main git push -u origin <branch-name>: Pushes changes and sets the upstream branch.- Example:
git push -u origin feature-xyz git pull: Fetches and merges changes from the remote repository to the current branch.- Example:
git pull git remote add <name> <url>: Adds a new remote repository.- Example:
git remote add origin https://github.com/user/repo.git git remote set-url origin <url>: Changes the URL of an existing remote repository.- Example:
git remote set-url origin https://github.com/user/new-repo.git
Inspection & Comparison
git log: Shows the commit history for the current branch.- Example:
git log git log --oneline: Displays a simplified commit history.- Example:
git log --oneline git diff <source> <target>: Shows differences between two branches or commits.- Example:
git diff main feature-xyz
Configuration
git config --global user.name "<name>": Sets the global username for commits.- Example:
git config --global user.name "John Doe" git config --global user.email "<email>": Sets the global email for commits.- Example:
git config --global user.email "[email protected]"
Conclusion
This comprehensive list of Git commands provides a solid foundation for managing your projects effectively.
By familiarizing yourself with these commands, you can streamline your workflow, collaborate with others, and maintain a clean and organized codebase.
Whether you’re initializing a new repository, managing branches, or pushing changes to a remote repository, these commands will help you navigate Git with confidence.
If you have any further questions or need additional commands, feel free to ask!


