DEV Community

Anurag Gharat
Anurag Gharat

Posted on

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

Top comments (0)