Git Cheatsheet
03-28-2023: Updated using ChatGPT
GitHub¶
Branching and Merging¶
-
Create a new branch¶
-
Switch to an existing branch¶
-
Create a new branch and switch to it¶
-
Merge changes from one branch into another¶
-
Rebase changes from one branch onto another¶
Undoing Changes¶
-
Discard changes in working directory¶
-
Unstage changes¶
-
Undo the last commit and keep changes in working directory¶
-
Undo the last commit and discard changes¶
-
Remove a commit from the history¶
Git Config¶
Git Hooks¶
History¶
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