Git Cheatsheet: Essential Commands for Developers

4 min read
DevTechTools Team
Expert developers sharing knowledge and best practices

Git Cheatsheet: Essential Commands for Developers

Git is the most popular version control system used by developers worldwide. Whether you're a beginner or an experienced developer, having a quick reference for Git commands can save you time and boost your productivity.

Getting Started

Initial Setup

bash
# Configure your identity
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Check your configuration
git config --list

Creating a Repository

bash
# Initialize a new repository
git init

# Clone an existing repository
git clone <repository-url>
git clone <repository-url> <directory-name>

Basic Commands

Checking Status and History

bash
# Check repository status
git status

# View commit history
git log
git log --oneline
git log --graph --oneline --all

# Show changes in working directory
git diff
git diff --staged

Staging and Committing

bash
# Add files to staging area
git add <file>
git add .
git add -A

# Commit changes
git commit -m "Commit message"
git commit -am "Add and commit in one step"

# Amend last commit
git commit --amend

Branching and Merging

Branch Management

bash
# List branches
git branch
git branch -a  # Show all branches including remote

# Create new branch
git branch <branch-name>
git checkout -b <branch-name>  # Create and switch

# Switch branches
git checkout <branch-name>
git switch <branch-name>  # Modern alternative

# Delete branch
git branch -d <branch-name>
git branch -D <branch-name>  # Force delete

Merging

bash
# Merge branch into current branch
git merge <branch-name>

# Merge with no fast-forward
git merge --no-ff <branch-name>

# Abort merge in case of conflicts
git merge --abort

Remote Repositories

Working with Remotes

bash
# List remote repositories
git remote -v

# Add remote repository
git remote add origin <repository-url>

# Fetch changes from remote
git fetch
git fetch origin

# Pull changes (fetch + merge)
git pull
git pull origin <branch-name>

# Push changes
git push
git push origin <branch-name>
git push -u origin <branch-name>  # Set upstream

Undoing Changes

Unstaging and Reverting

bash
# Unstage files
git reset HEAD <file>
git restore --staged <file>  # Modern alternative

# Discard changes in working directory
git checkout -- <file>
git restore <file>  # Modern alternative

# Reset to previous commit
git reset --soft HEAD~1  # Keep changes staged
git reset --mixed HEAD~1  # Keep changes unstaged
git reset --hard HEAD~1  # Discard all changes

Reverting Commits

bash
# Create new commit that undoes changes
git revert <commit-hash>

# Revert multiple commits
git revert <commit-hash>..<commit-hash>

Advanced Commands

Stashing

bash
# Stash current changes
git stash
git stash push -m "Stash message"

# List stashes
git stash list

# Apply stash
git stash apply
git stash apply stash@{0}

# Pop stash (apply and remove)
git stash pop

# Drop stash
git stash drop stash@{0}

Rebasing

bash
# Rebase current branch onto another
git rebase <branch-name>

# Interactive rebase
git rebase -i HEAD~3

# Continue rebase after resolving conflicts
git rebase --continue

# Abort rebase
git rebase --abort

Cherry Picking

bash
# Apply specific commit to current branch
git cherry-pick <commit-hash>

# Cherry pick multiple commits
git cherry-pick <commit-hash1> <commit-hash2>

Useful Aliases

Add these to your Git configuration for faster workflows:

bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD"
git config --global alias.visual "!gitk"

Best Practices

  • Write meaningful commit messages - Use present tense and be descriptive
  • Commit often - Make small, logical commits
  • Use branches - Keep your main branch clean
  • Pull before push - Always sync with remote before pushing
  • Review before commit - Use git diff --staged to review changes
  • Use .gitignore - Exclude unnecessary files from version control
  • Common Scenarios

    Fixing Mistakes

    bash
    # Forgot to add a file to last commit
    git add <forgotten-file>
    git commit --amend --no-edit
    
    # Wrong commit message
    git commit --amend -m "Correct message"
    
    # Committed to wrong branch
    git log  # Note the commit hash
    git reset --hard HEAD~1
    git checkout <correct-branch>
    git cherry-pick <commit-hash>

    Collaboration

    bash
    # Update your branch with latest changes
    git checkout main
    git pull origin main
    git checkout <your-branch>
    git rebase main
    
    # Squash commits before merging
    git rebase -i HEAD~3
    # In the editor, change "pick" to "squash" for commits to combine

    Conclusion

    This cheatsheet covers the most commonly used Git commands that every developer should know. Git has many more features and options, but mastering these basics will handle 90% of your version control needs.

    Remember, the key to becoming proficient with Git is practice. Don't be afraid to experiment in a test repository, and always keep backups of important work!

    Need help with a specific Git scenario? Feel free to reach out or check the official Git documentation for more detailed information.

    Found this article helpful?

    Share it with others who might benefit from it.

    More Articles