Skip to main content

Git Commands Cheat Sheet

Introduction

Brief overview of the importance of Git commands in managing repositories and version control.

Basic Git Commands

  • git init
    • Initializes a new Git repository.
  • git clone [url]
    • Clones a repository from a remote source to your local machine.

Working with Local Changes

  • git status
    • Shows the status of changes as untracked, modified, or staged.
  • git add [file]
    • Adds a file to the staging area.
  • git commit -m "[commit message]"
    • Commits your staged content as a new commit snapshot.

Branching and Merging

  • git branch
    • Lists all local branches in the current repository.
  • git branch [branch-name]
    • Creates a new branch.
  • git checkout [branch-name]
    • Switches to the specified branch and updates the working directory.
  • git merge [branch]
    • Merges the specified branch’s history into the current branch.

Working with Remotes

  • git remote -v
    • Lists all currently configured remote repositories.
  • git push [remote] [branch]
    • Pushes the branch to the remote repository.
  • git pull [remote]
    • Fetches the specified remote’s copy of the current branch and immediately merges it into the local copy.

Undoing Changes

  • git revert [commit]
    • Creates a new commit that undoes all of the changes made in the specified commit.
  • git reset [file]
    • Unstages the file, but it preserves the file contents.

Advanced Commands

  • git stash
    • Temporarily stores all modified tracked files.
  • git fetch [remote]
    • Downloads all changes from the remote, but doesn’t integrate them into your repository.