Git & Github
Git & Github
# Basic Operations
git commit -m "Commit message" # Commit staged changes with a
message
git commit -am "Commit message" # Add and commit changes in one
step (for tracked files)
git reset <file> # Unstage a file
git reset # Unstage all files
git log # View commit history
git show <commit_hash> # Show details of a specific commit
# Branches
git branch # List all branches (the asterisk marks the current
branch)
git branch <branch_name> # Create a new branch
git checkout <branch_name> # Switch to an existing branch
git checkout -b <new_branch_name> # Create and switch to a new branch
git merge <branch_name> # Merge a branch into the current branch
git branch -d <branch_name> # Delete a branch
# Remotes
git remote add origin <remote_url> # Add a remote repository
git push -u origin <branch_name> # Push the branch to the remote
repository
git pull origin <branch_name> # Pull changes from the remote repository
git clone <repository_url> # Clone a remote repository to your local
machine
# Stashing
git stash # Stash changes (save them for later)
git stash list # List all stashes
git stash apply # Apply the latest stash
git stash apply stash@{n} # Apply a specific stash (n is the stash index)
# Undoing Changes
git reset --hard HEAD # Discard all local changes (be careful!)
git checkout -- <file> # Discard changes in a specific file
git revert <commit_hash> # Create a new commit that undoes changes
from a specific commit
# Miscellaneous
git diff # Show changes between the working directory and
the staging area
git diff --cached # Show changes between the staging area and the
last commit
git clean -df # Remove untracked files and directories