Skip to content

Git Cheatsheet

03-28-2023: Updated using ChatGPT

GitHub

  • Pushing Existing Repo

    ### …or push an existing repository from the command line
    git remote add origin https://github.com/peddamat/xxx.git
    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 user.name "[name]"
    
  • Set global user email

    git config --global user.email "[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

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

History

  • Show a log of changes

    git log
    
  • Show a diff of changes

    git diff [file]
    

Rebasing

  • 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


Reflog

  • 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]


Bisect

  • 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]


Submodules

  • 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

Filter-branch

  • Updating author information in a repository

    git filter-branch --env-filter ' OLD_EMAIL="old_email@example.com" CORRECT_NAME="Correct Name" CORRECT_EMAIL="correct_email@example.com" 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