Skip to content

Git Most Used Commands

You can click Refresh to rescan the indexes

1. Configuring Git

# Set user name
git config --global user.name "Your Name"

# Set user email
git config --global user.email "your.email@example.com"

# Check configuration
git config --list

2. Initializing and Cloning Repositories

# Initialize a new repository
git init

# Clone an existing repository
git clone <repository_url>

# Clone a repository with submodules
git clone --recurse-submodules <repository_url>

3. Staging and Committing Changes

# Add specific file to staging
git add <file>

# Add all files to staging
git add .

# Commit changes with a message
git commit -m "Commit message"

# Amend the last commit (without changing message)
git commit --amend --no-edit

# Amend the last commit (changing message)
git commit --amend -m "New commit message"

4. Viewing Commit History

# View commit history
git log

# View one-line summary of commits
git log --oneline

# View detailed commit changes
git log -p

5. Branching

# List branches
git branch

# Create a new branch
git branch <branch_name>

# Switch to a branch
git checkout <branch_name>

# Create and switch to a new branch
git checkout -b <branch_name>

# Delete a branch locally
git branch -d <branch_name>

# Delete a branch remotely
git push origin --delete <branch_name>

6. Merging and Rebasing

# Merge a branch into the current branch
git merge <branch_name>

# Rebase the current branch onto another branch
git rebase <branch_name>

# Abort a rebase
git rebase --abort

7. Checking Out Files from Another Branch

# Checkout a file from another branch
git checkout <branch_name> -- <file>

# Restore a deleted file from a specific commit
git checkout <commit_hash> -- <file>

8. Stashing Changes

# Stash current changes
git stash

# List stashes
git stash list

# Apply the latest stash
git stash apply

# Apply a specific stash
git stash apply stash@{n}

# Drop a specific stash
git stash drop stash@{n}

# Create a named stash
git stash push -m "Stash description"

9. Tagging

# Create a lightweight tag
git tag <tag_name>

# Create an annotated tag
git tag -a <tag_name> -m "Tag message"

# List tags
git tag

# Push a tag to remote
git push origin <tag_name>

# Push all tags to remote
git push --tags

# Delete a local tag
git tag -d <tag_name>

# Delete a remote tag
git push origin --delete <tag_name>

10. Undoing Changes

# Unstage a file (keep changes in working directory)
git reset <file>

# Undo the last commit (keep changes in working directory)
git reset --soft HEAD~1

# Undo the last commit (discard changes)
git reset --hard HEAD~1

# Revert a commit
git revert <commit_hash>

11. Resetting Branch to a Specific Commit

# Reset the current branch to a specific commit (keep changes in working directory)
git reset --soft <commit_hash>

# Reset the current branch to a specific commit (discard changes)
git reset --hard <commit_hash>

# Force push the branch to update remote repository
git push --force origin <branch_name>

12. Remote Repositories

# Add a remote repository
git remote add origin <repository_url>

# View remote repositories
git remote -v

# Fetch changes from remote
git fetch origin

# Pull latest changes
git pull origin <branch_name>

# Push changes to remote
git push origin <branch_name>

# Remove a remote
git remote remove <remote_name>

13. Comparing Changes

# Compare working directory with last commit
git diff

# Compare a branch with another
git diff <branch1>..<branch2>

# Compare a file between branches
git diff <branch1>:<file> <branch2>:<file>

# Show changes in a specific commit
git show <commit_hash>

14. Git Submodules

# Add a submodule
git submodule add <repository_url> <path>

# Update submodules
git submodule update --init --recursive

# Remove a submodule
git submodule deinit -f -- <path>
git rm -rf <path>
git commit -m "Removed submodule <path>"

15. Cleaning Up

# Remove untracked files
git clean -f

# Remove untracked directories
git clean -fd

# Remove untracked and ignored files
git clean -xfd