50+ Essential Git Commands with Examples
1. Git Setup & Configuration
# 1. Set Username
Sets the global username for Git commits.
git config --global [Link] "Your Name"
Example:
git config --global [Link] "John Doe"
# 2. Set Email
Configures the email associated with Git commits.
git config --global [Link] "[Link]@[Link]"
Example:
git config --global [Link] "johndoe@[Link]"
# 3. Enable Colored Output
Enables color highlighting in Git output.
git config --global [Link] auto
# 4. Show Git Configuration
Displays the current global Git settings.
git config --list
2. Creating & Cloning Repositories
# 5. Initialize a New Repository
Creates a new Git repository in the current directory.
git init
Example:
git init my-project
# 6. Clone an Existing Repository
Downloads an existing repository from a remote source.
git clone <repo-url>
Example:
git clone [Link]
3. Checking Repository Status
# 7. Show Status of Changes
Displays uncommitted changes and staged files.
git status
# 8. View Commit History
Shows a detailed history of commits.
git log
# 9. View Condensed History
Displays commit history in a compact format.
git log --oneline --graph
4. Staging & Committing Changes
# 10. Stage a Specific File
Adds a file to the staging area before committing.
git add <file>
Example:
git add [Link]
# 11. Stage All Changed Files
Stages all modified and new files for commit.
git add .
# 12. Commit Staged Changes
Saves the staged changes as a commit.
git commit -m "Commit message"
Example:
git commit -m "Added new feature"
# 13. Amend the Last Commit
Modifies the last commit message or adds new changes.
git commit --amend -m "New commit message"
# 14. Unstage a File
Removes a file from the staging area without deleting changes.
git reset <file>
Example:
git reset [Link]
# 15. Discard All Changes
Resets the repository to the last committed state.
git reset --hard HEAD
5. Branching & Merging
# 16. List Branches
Displays all local branches in the repository.
git branch
# 17. Create a New Branch
Creates a new branch without switching to it.
git branch <branch-name>
Example:
git branch feature-branch
# 18. Switch to Another Branch
Changes the active branch.
git checkout <branch-name>
Example:
git checkout feature-branch
# 19. Create and Switch to a New Branch
Creates a new branch and immediately switches to it.
git checkout -b <branch-name>
Example:
git checkout -b new-feature
# 20. Merge Another Branch
Integrates changes from another branch into the current branch.
git merge <branch-name>
Example:
git merge feature-branch
# 21. Delete a Branch
Removes a local branch.
git branch -d <branch-name>
Example:
git branch -d old-feature
# 22. Force Delete a Branch
Deletes a branch even if it has unmerged changes.
git branch -D <branch-name>
6. Working with Remote Repositories
# 23. Show Remote URLs
Lists the remote repositories linked to the local repo.
git remote -v
# 24. Add a Remote Repository
Associates a remote repository with the local project.
git remote add origin <repo-url>
Example:
git remote add origin [Link]
# 25. Push a Branch to Remote Repository
Uploads local branch changes to a remote repository.
git push -u origin <branch-name>
Example:
git push -u origin main
# 26. Pull Changes from Remote Repository
Fetches and merges changes from a remote repository.
git pull origin <branch-name>
Example:
git pull origin main
# 27. Fetch Changes Without Merging
Retrieves changes from the remote repository without applying them.
git fetch origin