Cheatsheet: Git

Basic Commands

Initialize a new Git repository

git init

Clone an existing repository

git clone <repository_url>

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>

Working with Remote Repositories

Add a remote repository

git remote add origin <repository_url>

Fetch changes from a remote repository

git fetch

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