Mastering Git with GitHub: A Comprehensive Cheat Sheet

  1. Initialize a repository:

     git init
    
  2. Clone a repository:

     git clone <repository_url>
    
  3. Check the status:

     git status
    
  4. Add changes to the staging area: ( git add . --> to stage all files)

     git add <file_name>
    
  5. Commit changes:

     git commit -m "Commit message"
    
  6. View commit history:

     git log
    
  7. Create a new branch:

     git branch <branch_name>
    
  8. Switch to a branch:

     git checkout <branch_name>
    
  9. Merge branches:

     git merge <branch_name>
    
  10. Delete a branch: ( -d to delete merged branch and -D to delete un-merged branch)

    git branch -d <branch_name>
    
  11. Undo changes in the working directory:

    git restore <file_name>
    
  12. Undo changes in the staging area:

    git reset HEAD <file>
    
  13. View changes between commits:

    git diff <commit1> <commit2>
    
  14. Show details of a commit:

    git show <commit>
    
  15. Set username and email:

    git config --global user.name "Your Name"
    git config --global user.email "youremail@example.com"
    
  16. View remote repositories:

    git remote -v
    
  17. Add a remote repository:

    git remote add <remote_name> <repository_url>
    
  18. Push changes to a remote repository:

    git push <remote_name> <branch_name>
    
  19. Pull changes from a remote repository:

    git pull <remote_name> <branch_name>
    
  20. Fetch changes from a remote repository:

    git fetch <remote_name>
    
  21. Stash changes:

    git stash
    
  22. Apply stashed changes:

    git stash apply
    
  23. To see a list of stashed changes.

    git stash list
    
  24. Create a new branch and switch to it:

    git checkout -b <new_branch_name>
    
  25. Rename a branch:

    git branch -m <old_branch_name> <new_branch_name>
    
  26. Configure a remote repository URL:

    git remote set-url origin <new_repository_url>
    
  27. Delete a remote branch:

    git push <remote_name> --delete <branch_name>
    
  28. Squash commits:

    git rebase -i HEAD~<number_of_commits>
    
  29. View commit history graphically:

    git log --graph --oneline --decorate --all
    
  30. Show the last commit:

    git show HEAD
    
  31. Show the difference between the working directory and the staging area:

    git diff
    
  32. Show the difference between the staging area and the last commit:

    git diff --staged
    
  33. Show the difference between two branches:

    git diff <branch1> <branch2>
    
  34. Show the difference between two commits:

    git diff <commit1> <commit2>
    
  35. Show commits that will be merged or rebased:

    git cherry-pick -n <commit>
    
  36. Abort a merge:

    git merge --abort
    
  37. Abort a rebase:

    git rebase --abort
    
  38. Continue a merge after resolving conflicts:

    git merge --continue
    
  39. Continue a rebase after resolving conflicts:

    git rebase --continue
    
  40. Configure default editor:

    git config --global core.editor <editor_name>
    
  41. Show commits that changed a file:

    git log -- <file_path>
    
  42. Ignore files globally: Create a .gitignore file and add file patterns to ignore.

  43. Check which files are ignored:

    git check-ignore -v <file>
    
  44. Show the last commit on each branch:

    git branch -v
    
  45. Change the commit message of the last commit:

    git commit --amend -m "New commit message"
    
  46. List all tags:

    git tag
    

These commands cover various aspects of Git version control and should serve as a handy reference for both beginners and experienced users.

0
Subscribe to my newsletter

Read articles from Vaishnavi Shivde directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vaishnavi Shivde
Vaishnavi Shivde

Aspiring DevOps Engineer | Linux | Git & Github | Shell Scripting | Docker | CI/CD Jenkins | Kubernetes | AWS | Terraform | JIRA | Python |