Skip to main content

Git Cheat Sheet

๐Ÿ”ง Setup

    Set your name and email


    git config --global user.name "Your Name" git config --global user.email "you@example"

    ย 

    Set default branch to main


    git config --global init.defaultBranch main

    ๐Ÿ“ Create or Clone Repository

      Initialize new repository

      git init

      ย 

      Clone existing repository

      git clone <repo_url>

      ๐Ÿ“„ Staging and Committing

        Check file status


        git status

        ย 

        Stage changes


        git add <file> git add . # Stage all changes

        ย 

        Commit staged changes


        git commit -m "Your message"

        ย 

        Amend last commit


        git commit --amend

        ๐Ÿ”™ Undoing Changes

        Undo Last Commit (Not Pushed)

        • Keep changes staged

          git reset --soft HEAD~1
        • Keep changes unstaged

          git reset --mixed HEAD~1
        • Discard commit and changes

          git reset --hard HEAD~1

        Reset to Specific Commit

        • Soft reset

          git reset --soft <commit_id>
        • Mixed reset (default)

          git reset --mixed <commit_id>
        • Hard reset

          git reset --hard <commit_id>

        Revert a Commit (Safe for Pushed Commits)

        • Create a new commit that undoes an old one


          git revert <commit_id>

        ๐ŸŒณ Branching

        • List branches


          git branch
        • Create new branch

          git branch <branch_name>
        • Switch to a branch

          git checkout <branch_name>
        • Create and switch

          git checkout -b <branch_name>
        • Delete branch

          git branch -d <branch_name> # Safe delete git branch -D <branch_name> # Force delete

        ๐Ÿ”€ Merging & Rebasing

        • Merge a branch into current

          git merge <branch_name>
        • Rebase current branch onto another

          git rebase <branch_name>
        • Interactive rebase

          git rebase -i HEAD~3

        ๐Ÿ“ค Pushing & Pulling

        • Push commits

          git push
        • Push new branch and track it

          git push -u origin <branch_name>
        • Pull latest changes

          git pull

        ๐Ÿ” Viewing History

        • Full commit log

          git log
        • Condensed view

          git log --oneline
        • Inspect specific commit

          git show <commit_id>

        ๐Ÿงน Cleaning Up

        • Delete untracked files/folders

          git clean -fd
        • Temporarily save changes

          git stash
        • Reapply stashed changes

          git stash pop

        ๐Ÿ“ฆ Tags

        • List all tags

          git tag
        • Create a new tag

          git tag <tag_name>
        • Push tag to remote

          git push origin <tag_name>

        ๐Ÿ”— Remote Repositories

        • View remotes

          git remote -v
        • Add a remote

          git remote add origin <url>
        • Remove a remote

          git remote remove origin