Catalog / GitHub Cheat Sheet

GitHub Cheat Sheet

A comprehensive cheat sheet covering essential GitHub commands, workflows, and features for version control and collaboration.

Basic Git Commands

Configuration

git config --global user.name "Your Name"

Sets your name for commit messages.

git config --global user.email "[email protected]"

Sets your email address for commit messages.

git config --global core.editor "code --wait"

Sets VS Code as the default editor for commit messages (replace code --wait with your editor command).

git config --list

Lists all git configuration settings.

Starting a Repository

git init

Initializes a new Git repository in the current directory.

git clone <repository_url>

Clones a repository from a remote URL.

Basic Workflow

git status

Shows the status of the working directory and staging area.

git add <file>

Adds a file to the staging area.

git add .

Adds all changes in the current directory to the staging area.

git commit -m "Commit message"

Commits the staged changes with a descriptive message.

Branching and Merging

Branch Management

git branch

Lists all local branches. The current branch is marked with an asterisk (*).

git branch <branch_name>

Creates a new branch.

git checkout <branch_name>

Switches to the specified branch.

git checkout -b <branch_name>

Creates and switches to a new branch.

git branch -d <branch_name>

Deletes a branch (only if it has been merged).

git branch -D <branch_name>

Forces deletion of a branch (even if it hasn’t been merged).

Merging Branches

git merge <branch_name>

Merges the specified branch into the current branch.

git mergetool

Opens a merge tool to resolve conflicts.

git log --graph --oneline --decorate

Visualize the branch and merge history.

Rebasing

git rebase <branch_name>

Rebases the current branch onto the specified branch. Rewrites commit history.

git rebase --abort

Abort the rebase process.

git rebase --continue

Continue the rebase process after resolving conflicts.

Remote Repositories

Connecting to Remotes

git remote add origin <repository_url>

Adds a remote repository named ‘origin’.

git remote -v

Lists all remote connections.

git remote show origin

Shows information about the remote ‘origin’.

Pushing and Pulling

git push origin <branch_name>

Pushes the specified branch to the remote ‘origin’.

git push -u origin <branch_name>

Sets up tracking information for the branch so that git pull and git push can be used without specifying the remote and branch.

git pull origin <branch_name>

Pulls changes from the remote ‘origin’ to the specified branch.

git fetch

Fetches all changes from the remote but does not merge them.

GitHub Specifics

git request-pull <start> <url> <end>

Generates a request pull message.

gh repo clone <repo>

Clone a repository. Requires GitHub CLI (gh).

Undoing Changes

Undoing Local Changes

git checkout -- <file>

Discards changes in the working directory for a specific file.

git restore <file>

Alternative to git checkout -- <file> for discarding local changes (more modern).

git reset HEAD <file>

Removes a file from the staging area.

git clean -n

Dry run to see which files would be removed by git clean.

git clean -f

Removes untracked files from the working directory.

Undoing Commits

git commit --amend -m "New commit message"

Amends the last commit with new changes or message.

git reset --soft HEAD~1

Resets the last commit, keeping the changes in the staging area.

git reset --mixed HEAD~1

Resets the last commit, keeping the changes in the working directory.

git reset --hard HEAD~1

Resets the last commit and discards the changes. Use with CAUTION.

git revert <commit_hash>

Creates a new commit that reverts the changes from the specified commit. Safe way to undo changes in shared repositories.