Git Commands Cheat Sheet

  1. Initialize a Git repository:
git init
  1. Check the repository status:
git status
  1. Add changes to the staging area:
git add . or git add fileName
  1. Commit changes with a message:
git commit -m "message"
  1. View the commit history:
git log
  1. Compare changes between the working directory and the staging area:
git diff
  1. Compare changes between the staging area and the previous commit:
git diff --staged
  1. Directly commit changes to tracked files (skip staging):
git commit -a -m "Direct Commit"
  1. Remove a file and stage the deletion:
git rm filename
  1. Rename a file and stage the rename operation:
git mv oldname newname
  1. Stop tracking a file but keep it in the working directory:
git rm --cached filename
  1. View the commit log in a one-line format:
git log --pretty=oneline
  1. Unstage changes for a specific file:
git restore --staged filename
  1. Discard changes in the working directory for a specific file:
git checkout -- filename
  1. Forcefully checkout the previous commit (discard local changes):
git checkout -f
  1. Create a global Git alias (e.g., git st for git status):
git config --global alias.st status
  1. Create a new branch and switch to it:
git checkout -b branchname
  1. Switch to an existing branch:
git checkout branchname
  1. List all branches in the repository:
git branch
  1. List branches that have been merged into the current branch:
git branch --merged
  1. Delete a branch if it has been merged into the current branch:
git branch -d branchname
  1. Forcefully delete a branch (without checking for merges):
git branch -D branchname
  1. List branches that haven't been merged into the current branch:
git branch --no-merged
  1. Check remote available or not
git remote
  1. Check from where to push and pull
git remote -v
  1. Remore remote branch
git push -d origin branchname

Remember to adapt these commands to your specific Git workflow and repository needs. Always use caution when performing operations that involve branch deletion or discarding changes.