Skip to content

Git Cheatsheet

03-28-2023: Updated using ChatGPT


  • Pushing Existing Repo

    ### …or push an existing repository from the command line
    git remote add origin
    git branch -M main
    git push -u origin main
  • Fetch Only Latest Commits

    git fetch --no-tags --prune --no-recurse-submodules --depth=1 \
        origin +refs/heads/main:refs/remotes/origin/main

Branching and Merging

  • Create a new branch

    git branch [new_branch_name]
  • Switch to an existing branch

    git checkout [branch_name]
  • Create a new branch and switch to it

    git checkout -b [new_branch_name]
  • Merge changes from one branch into another

    git merge [source_branch] [destination_branch]
  • Rebase changes from one branch onto another

    git rebase [source_branch] [destination_branch]

Undoing Changes

  • Discard changes in working directory

    git checkout -- [file]
  • Unstage changes

    git reset [file]
  • Undo the last commit and keep changes in working directory

    git reset HEAD~
  • Undo the last commit and discard changes

    git reset --hard HEAD~
  • Remove a commit from the history

    git rebase -i HEAD~[number_of_commits_to_keep]

Git Config

  • Set global user name

    git config --global "[name]"
  • Set global user email

    git config --global "[email]"
  • Set global editor

    git config --global core.editor [editor_name]

Git Hooks

  • Install a hook

    cp [hook_name].sample .git/hooks/[hook_name]
  • Make a hook executable

    chmod +x .git/hooks/[hook_name]
  • Sample pre-commit hook to run tests

    # Run tests
    python test
    # If tests fail, abort the commit
    if [ $? != 0 ]; then
        echo "Tests failed. Aborting commit."
        exit 1


  • Show a log of changes

    git log
  • Show a diff of changes

    git diff [file]


  • Interactively rebase the last 3 commits

    git rebase -i HEAD~3

  • Pick specific commits and squash them into one commit

    git rebase -i HEAD~4

  • Mark the commits you want to squash with s and save the changes.

  • Rebase and preserve merges

    git rebase -p


  • Recovering lost commits

  • View a list of past commits that are no longer referenced

    git reflog

  • Recover a lost commit

    git cherry-pick [commit_hash]


  • Finding the commit that introduced a bug

  • Mark the current commit as "bad"

    git bisect bad

  • Mark an earlier commit as "good"

    git bisect good [commit_hash]

  • Automatically find the commit that introduced the bug

    git bisect run [test_script]


  • Adding a submodule

git submodule add [repository_url] [path]

  • Updating a submodule

  • Update all submodules

    git submodule update --recursive

  • Update a specific submodule

    git submodule update --remote [submodule_path]

Git Worktree

  • Creating a new worktree

git worktree add [path] [branch]

  • Listing worktrees

git worktree list


  • Updating author information in a repository

    git filter-branch --env-filter ' OLD_EMAIL="" CORRECT_NAME="Correct Name" CORRECT_EMAIL="" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags

ChatGPT Script

Generate a markdown list of the most common advanced git commands in the following format, with the commands grouped by "Command Group":

### Command Group

- #### Description of command

     code for command

Last update: 2023-03-28