Skip to content

Most Frequently Used Git Commands

homepage-banner

TL;DR

Basic Commands

  • Initialize a new Git repository:

    git init
    
  • Check the status of your repository:

    git status
    
  • Add files to the staging area:

    git add <file>
    git add .
    
  • Commit changes with a message:

    git commit -m "Your commit message"
    
  • View commit history:

    git log
    git log --oneline
    

Branching and Merging

  • List all branches:

    git branch
    
  • Create a new branch:

    git branch <branch-name>
    
  • Switch to a branch:

    git checkout <branch-name>
    
  • Create a new branch and switch to it:

    git checkout -b <branch-name>
    
  • Merge a branch into the current branch:

    git merge <branch-name>
    
  • Rebase the current branch onto another branch:

    git rebase <branch-name>
    

Undoing Changes

  • Discard changes in the working directory:

    git restore <file>
    
  • Unstage changes from the staging area:

    git restore --staged <file>
    
  • Revert a commit by creating a new commit:

    git revert <commit-hash>
    

Remote Repositories

  • Clone a remote repository:

    git clone <repository-url>
    
  • Add a remote repository:

    git remote add <remote-name> <repository-url>
    
  • Fetch changes from a remote repository:

    git fetch <remote-name>
    
  • Pull changes from a remote repository:

    git pull <remote-name> <branch-name>
    
  • Push changes to a remote repository:

    git push <remote-name> <branch-name>
    
Leave your message