Categories: cheatsheetGit

Essential Git Commands – Cheatsheet for Developers

Basic Commands

  • Initialize a repository
    git init
  • Clone a repository
    git clone <repository_url>
  • Check repository status
    git status
  • Add files to staging area
    git add <file>
    git add .
  • Commit changes
    git commit -m "commit message"
  • Add and commit changes
    git commit -am "commit message"

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>
  • Merge a branch
    git checkout <target_branch>
    git merge <branch_to_merge>

Remote Repositories

  • Add a remote repository
    git remote add <remote_name> <repository_url>
  • List remote repositories
    git remote -v
  • Fetch changes from remote
    git fetch <remote_name>
  • Pull changes from remote
    git pull <remote_name> <branch_name>
  • Push changes to remote
    git push <remote_name> <branch_name>

Undoing Changes

  • Undo changes in working directory
    git checkout -- <file>
  • Unstage changes
    git reset HEAD <file>
  • Undo last commit but keep changes
    git reset --soft HEAD~1
  • Undo last commit and discard changes
    git reset --hard HEAD~1
  • Revert a commit
    git revert <commit_id>

Viewing History

  • Show commit history
    git log
  • Show commit history with changes
    git log -p
  • Show a specific commit
    git show <commit_id>
  • Show changes to a file
    git log -p <file>

Stashing Changes

  • Stash changes
    git stash
  • List stashes
    git stash list
  • Apply a stash
    git stash apply
  • Apply and drop a stash
    git stash pop
  • Drop a stash
    git stash drop

Tagging

  • Create a tag
    git tag <tag_name>
  • List tags
    git tag
  • Push tags to remote
    git push <remote_name> <tag_name>

Helpful Tips

  • Alias for common commands
    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
  • Configure user information
    git config --global user.name "Your Name"

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

Keep this cheatsheet handy for quick reference while working with Git!

Ethan Brown

View Comments

  • Great guide on viewing history and stashing changes. Very informative!

  • Good summary of commands for remote repositories. Easy to follow!

  • I like the section on branching. It explains how to create and switch branches.

  • The undoing changes part is very useful. Helps to correct mistakes quickly.

  • This cheatsheet is very helpful. It shows how to use basic commands in Git.

Recent Posts

Navigating the Top IT Careers: A Guide to Excelling as a Software Engineer in 2024

Discover essential insights for aspiring software engineers in 2023. This guide covers career paths, skills,…

1 month ago

Navigating the Future of Programming: Insights into Software Engineering Trends

Explore the latest trends in software engineering and discover how to navigate the future of…

1 month ago

“Mastering the Art of Software Engineering: An In-Depth Exploration of Programming Languages and Practices”

Discover the essentials of software engineering in this comprehensive guide. Explore key programming languages, best…

1 month ago

The difference between URI, URL and URN

Explore the distinctions between URI, URL, and URN in this insightful article. Understand their unique…

1 month ago

Social networks steal our data and use unethical solutions

Discover how social networks compromise privacy by harvesting personal data and employing unethical practices. Uncover…

1 month ago

Checking if a checkbox is checked in jQuery

Learn how to determine if a checkbox is checked using jQuery with simple code examples…

1 month ago