DEV Community

Anurag Gharat
Anurag Gharat

Posted on

4 1 1 1 1

Git cheat sheet

Here are some basic Git commands to ease your life as a developer.

Git Basics

  • View Git configurations
git config --list
Enter fullscreen mode Exit fullscreen mode
  • Set user name
git config --global user.name "example name"
Enter fullscreen mode Exit fullscreen mode
  • Set user email
git config --global user.email "example email"
Enter fullscreen mode Exit fullscreen mode
  • Initialize an empty git repository
git init
Enter fullscreen mode Exit fullscreen mode
  • Get the status of changes in files.
git status
Enter fullscreen mode Exit fullscreen mode
  • Display the entire commit history
git log
Enter fullscreen mode Exit fullscreen mode
  • Get the difference between the working directory and the staging area
git diff
Enter fullscreen mode Exit fullscreen mode
  • Show the difference between the working directory and the last committed change
git diff HEAD
Enter fullscreen mode Exit fullscreen mode

Handling file changes

  • Add a specific file to the staging area
git add <file-name>
Enter fullscreen mode Exit fullscreen mode
  • Add all changed files to the staging area
git add .
Enter fullscreen mode Exit fullscreen mode
  • Commit the staged changes with a commit message
git commit -m "Commit Message"
Enter fullscreen mode Exit fullscreen mode
  • Remove a specific file from the staging area
git rm <file-name>
Enter fullscreen mode Exit fullscreen mode
  • Revert a commit with a new commit
git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  • Stash current changes
git stash
Enter fullscreen mode Exit fullscreen mode
  • Apply stashed changes
git stash apply
Enter fullscreen mode Exit fullscreen mode
  • Reset your last commit and keep the changes
git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Handling branches

  • Create a new branch from the current branch
git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Switch to a different branch
git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Create a new branch and switch to it
git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • List down all branches
git branch
Enter fullscreen mode Exit fullscreen mode
  • Merge the branch with the current branch
git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

Working with Remote

  • Clone an existing git repository
git clone <repo-url>
Enter fullscreen mode Exit fullscreen mode
  • Connect your local git repository to a remote repository and assign it a name
git remote add origin <url>
Enter fullscreen mode Exit fullscreen mode
  • Pull changes from a remote branch
git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Fetch changes from a remote branch
git fetch origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Push committed changes to a remote branch
git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Push all the local branches to remote
git push origin --all
Enter fullscreen mode Exit fullscreen mode
  • Pull a remote branch and create a local branch for it
git checkout -b <local-branch-name> origin/<remote-branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Rebase the current branch with a specific branch
git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Interactively Rebase the current branch with a specific branch
git rebase <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Apply changes from a certain commit in your branch
git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (1)

Collapse
 
thesohailjafri profile image
Sohail SJ | TheZenLabs

I think last 3 should be part of the Handling branches. loved the compact git guide <3

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay