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