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.
git init
: Initializes a new Git repository in the current directory.git init
git clone <repository-url>
: Creates a local copy of a remote repository.git clone https://github.com/user/repo.git
git status
: Displays the state of the working directory and staging area.git status
git add <file>
: Adds a specific file to the staging area.git add file.txt
git add -A
: Stages all changes (new, modified, deleted files).git add -A
git commit -m "<message>"
: Commits the staged changes with a descriptive message.git commit -m "Initial commit"
git rm <file>
: Removes a file from the working directory and stages the removal.git rm file.txt
git branch
: Lists all local branches.git branch
git branch -a
: Lists all local and remote branches.git branch -a
git branch <branch-name>
: Creates a new branch with the specified name.git branch feature-xyz
git branch -d <branch-name>
: Deletes a local branch that has been merged.git branch -d feature-xyz
git branch -D <branch-name>
: Forces the deletion of a local branch, regardless of its merge status.git branch -D feature-xyz
git push origin --delete <branch-name>
: Deletes a branch from the remote repository.git push origin --delete feature-xyz
git checkout -b <branch-name>
: Creates a new branch and switches to it.git checkout -b feature-xyz
git checkout <branch-name>
: Switches to the specified branch.git checkout main
git merge <branch-name>
: Merges the specified branch into the current branch.git merge feature-xyz
git stash
: Stashes changes in the working directory for later use.git stash
git stash pop
: Applies the stashed changes back to the working directory.git stash pop
git push origin <branch-name>
: Pushes the specified branch to the remote repository.git push origin main
git push -u origin <branch-name>
: Pushes changes and sets the upstream branch.git push -u origin feature-xyz
git pull
: Fetches and merges changes from the remote repository to the current branch.git pull
git remote add <name> <url>
: Adds a new remote repository.git remote add origin https://github.com/user/repo.git
git remote set-url origin <url>
: Changes the URL of an existing remote repository.git remote set-url origin https://github.com/user/new-repo.git
git log
: Shows the commit history for the current branch.git log
git log --oneline
: Displays a simplified commit history.git log --oneline
git diff <source> <target>
: Shows differences between two branches or commits.git diff main feature-xyz
git config --global user.name "<name>"
: Sets the global username for commits.git config --global user.name "John Doe"
git config --global user.email "<email>"
: Sets the global email for commits.git config --global user.email "[email protected]"
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!