Cheatsheet: Git

Basic Commands

Initialize a new Git repository

git init

Clone an existing repository

git clone <repository_url>

Clone into a folder

git clone <repository_url> <folder_name>

Check repository status

git status

Stage changes for commit

git add <file_name> or git add .

Commit changes

git commit -m "commit message"

Branching & Merging

Create a new branch

git branch <branch_name>

Switch to a branch

git checkout <branch_name>

Create and switch to a new branch

git checkout -b <branch_name>

Merge a branch into the current branch

git merge <branch_name>

Delete a branch

git branch -d <branch_name>

Checkout a tag into a new branch

git checkout -b <branch_name> <tag_name>

Working with Remote Repositories

Add a remote repository

git remote add origin <repository_url>

Fetch changes from a remote repository

git fetch

Fetch changes from a remote repository and include tags

git fetch --tags

Pull changes from a remote repository

git pull

Push changes to a remote repository

git push origin <branch_name>

List all remote repositories

git remote -v

Undo & Restore

Undo local changes to a file

git checkout -- <file_name>

Unstage a file

git reset <file_name>

Revert to a previous commit (soft reset)

git reset --soft <commit_id>

Revert to a previous commit (hard reset, discards changes)

git reset --hard <commit_id>

Remove untracked files

git clean -f

Stashing & Rebasing

Stash uncommitted changes

git stash

Apply stashed changes

git stash apply

Create a rebase onto another branch

git rebase <branch_name>

Continue a rebase after resolving conflicts

git rebase --continue

Abort an ongoing rebase

git rebase --abort

Git Bisect

Start a binary search for the commit introducing a bug

git bisect start

Mark a known bad commit (where the bug is present)

git bisect bad <commit_id>

Mark a known good commit (where the bug is absent)

git bisect good <good_commit_id>

Mark the current commit as bad

git bisect bad

Mark the current commit as good

git bisect good

Run a custom script to automate bisecting

git bisect run <script_name>

Reset the repository to its original state after bisecting

git bisect reset

Git Switch

Create a new branch and switch to it

git switch -c <branch_name>

Switch to an existing branch

git switch <branch_name>

Switch to a branch based on a specific commit

git switch -c <branch_name> <commit_id>

Discard changes in the working directory and switch branches

git switch --discard-changes <branch_name>

Git Cherry-Pick

Apply a specific commit to the current branch

git cherry-pick <commit_id>

Cherry-pick a range of commits

git cherry-pick {{start_commit_id}}..<end_commit_id>

Cherry-pick multiple non-consecutive commits

git cherry-pick {{commit_id1}} {{commit_id2}}

Continue cherry-pick after resolving conflicts

git cherry-pick --continue

Abort cherry-pick operation

git cherry-pick --abort

Applying Changes from a Different Remote

Add a new remote

git remote add <remote_name> <remote_url>

Fetch changes from a remote branch

git fetch <remote_name> <branch_name>

Merge changes from a remote branch into the current branch

git merge <remote_name>/<branch_name>

Rebase current branch onto a remote branch

git rebase <remote_name>/<branch_name>

Checkout a branch from a different remote

git checkout -b <branch_name> <remote_name>/<branch_name>