Day 12: Git Cheat Sheet
Table of contents
No headings in the article.
Sure! Here's a Git cheat sheet covering each topic with 5-6 commands:
Setup and Init:
git config --global
user.name
"Your Name"
: Set your name globally.git config --global
user.email
"
youremail@example.com
"
: Set your email globally.git init
: Initialize a new Git repository in the current directory.git clone <repository-url>
: Clone an existing repository from a URL.git remote add origin <repository-url>
: Add a remote repository.
Stage & Snapshot:
git add <file>
: Stage changes in<file>
for the next commit.git reset HEAD <file>
: Unstage changes in<file>
.git commit -m "Commit message"
: Commit staged changes with a message.git commit --amend
: Amend the last commit with new changes.git restore <file>
: Discard changes in<file>
.
Branch and Merge:
git branch
: List all branches in the repository.git branch <branch-name>
: Create a new branch.git checkout <branch-name>
: Switch to<branch-name>
.git merge <branch-name>
: Merge<branch-name>
into the current branch.git branch -d <branch-name>
: Delete a branch.
Inspect & Compare:
git status
: Show the current status of the repository.git log
: Display commit history.git diff
: Show changes between commits, branches, or files.git show <commit-id>
: Show changes made in a specific commit.git blame <file>
: Show who made changes to each line of<file>
.
Share and Update:
git push
: Upload local changes to a remote repository.git pull
: Fetch changes from the remote repository and merge them into the current branch.git fetch
: Fetch changes from the remote repository without merging.git remote -v
: List all remote repositories and their URLs.git push -u origin <branch-name>
: Push the current branch to the remote repository and set up tracking.
Tracking Path Changes:
git add -u
: Stage all changes to tracked files.git clean -n
: Show which files would be removed bygit clean
.git mv <old-path> <new-path>
: Move or rename a file and stage the change.git rm <file>
: Remove a file from the working directory and stage the deletion.git diff --cached
: Show changes staged for the next commit.
Rewrite History:
git commit --amend
: Amend the last commit.git rebase <branch-name>
: Rebase the current branch onto<branch-name>
.git reset --hard HEAD~<number>
: Reset the current branch to<number>
commits ago.git reflog
: Show a log of changes to the HEAD.git cherry-pick <commit-id>
: Apply changes from a specific commit to the current branch.
Temporary Commits:
git stash
: Temporarily save changes in the working directory.git stash apply
: Apply the most recent stash to the working directory.git stash list
: List all stashed changes.git stash drop
: Remove the most recent stash.git stash clear
: Remove all stashed changes.
Ignore Patterns:
.gitignore
: Create a file named.gitignore
to specify patterns of files to ignore.git check-ignore -v <file>
: Check if<file>
is ignored and by which rule.git add -f <file>
: Force-add an ignored file.git update-index --assume-unchanged <file>
: Mark<file>
as unchanged.git update-index --skip-worktree <file>
: Ignore changes to<file>
.
This cheatsheet covers a wide range of Git commands for various tasks.