GIT Commands

By: zigmoid
Posted on: 05/13/2014

Here’s a list of essential Git commands you’ll need as a developer:

Basic Git Commands

  1. Initialize a Git RepositoryshCopyEditgit init
  2. Clone a RepositoryshCopyEditgit clone <repo_url>
  3. Check StatusshCopyEditgit status
  4. Add Files to Staging AreashCopyEditgit add <file> # Add a specific file git add . # Add all changed files
  5. Commit ChangesshCopyEditgit commit -m "Your commit message"
  6. View Commit HistoryshCopyEditgit log

Branching & Merging

  1. Create a New BranchshCopyEditgit branch <branch_name>
  2. Switch to a BranchshCopyEditgit checkout <branch_name>
  3. Create & Switch to a New BranchshCopyEditgit checkout -b <branch_name>
  4. Merge a BranchshCopyEditgit merge <branch_name>
  5. Delete a BranchshCopyEditgit branch -d <branch_name> # Delete local branch git push origin --delete <branch_name> # Delete remote branch

Working with Remote Repositories

  1. Check Remote URLsshCopyEditgit remote -v
  2. Add a Remote RepositoryshCopyEditgit remote add origin <repo_url>
  3. Push Changes to Remote RepositoryshCopyEditgit push origin <branch_name>
  4. Pull Changes from Remote RepositoryshCopyEditgit pull origin <branch_name>
  5. Fetch Remote Changes (Without Merging)shCopyEditgit fetch origin

Undo & Reset

  1. Undo Last Commit (Keep Changes in Staging)shCopyEditgit reset --soft HEAD~1
  2. Undo Last Commit (Discard Changes)shCopyEditgit reset --hard HEAD~1
  3. Revert a Commit (Create a New Commit to Undo)shCopyEditgit revert <commit_hash>
  4. Discard Changes in a FileshCopyEditgit checkout -- <file>
  5. Remove a File from Staging AreashCopyEditgit reset <file>

Stashing Changes

  1. Stash Current ChangesshCopyEditgit stash
  2. Apply Stashed ChangesshCopyEditgit stash pop
  3. List Stashed ChangesshCopyEditgit stash list
  4. Drop a StashshCopyEditgit stash drop

Advanced Commands

  1. Rebase a BranchshCopyEditgit rebase <branch_name>
  2. Squash Commits (Interactive Rebase)shCopyEditgit rebase -i HEAD~<number_of_commits>
  3. Cherry-Pick a CommitshCopyEditgit cherry-pick <commit_hash>
  4. Check Differences Between CommitsshCopyEditgit diff <commit1> <commit2>
  5. Tag a CommitshCopyEditgit tag <tag_name>
  6. Push Tags to RemoteshCopyEditgit push origin --tags

Cleaning Up

  1. Remove Untracked FilesshCopyEditgit clean -f
  2. Delete All Local Branches Except main/mastershCopyEditgit branch | grep -v "main" | xargs git branch -D

This should cover most of your Git needs! 🚀 Let me know if you need more details on any command.