Git Cheat Sheet

Git Cheat Sheet

Git helps to keep track of code at specific intervals.

  • Initialize a git repo

    git init
    
  • Add files to the staging area

      git add fileName
      OR
      git add .
    

    The git add . command is used to add all the files to the staging area.

  • Commit staged changes

      git commit -m "message for the commit"
    
  • View commit history

      git log
    
  • Create a new branch

      git branch branch-name
    
      git checkout -b new-branch
    
      git switch -c branch-name
    

    The checkout and switch command creates a new branch and switches to the created branch but the branch command does-not switch the branch automatically.

  • Merge two branches

      git merge
    

    For git merge to work properly you should be located in the branch where the additional information from the other branch should be added to. Suppose we need to merge branch-3 to the main branch then we should be in the main branch.

  • Delete Branches

    git branch -d/-D branch-name
    

    The git branch -d/-D branch-name is used to delete a specific branch. The small -d will only allow you to delete branches if you merge these branches. The capital -D will force the deletion, this means the branch will be deleted, no matter if you merged it or not.

A great resource for understanding git.