Git cheatsheet

Create a repository
# Create a new local repository
git init [name]

# Clone a repository
git clone [url]

# Clone a repository into a specified directory
git clone [url] [directory]
Config
# Set the name attached to your commits
git config --global user.name "[name]"

# Set the email attached to your commits
git config --global user.email "[email]"

# Enable some colorization of Git output
git config --global color.ui auto

# Edit the global configuration file
git config --global --edit
Observe your Repository
# Show the commit history
git log

# Show the commits that changed file
git log --follow [file]

# Show commits on branchA that not on B
git log branchB..branchA

# Show any object in Git
git show [SHA]
Stage and snapshot
# Show modified files in working directory
git status

# Add a file as it looks now to your stage
git add [file]

# Unstage a file in working directory
git reset [file]

# Diff of what is changed but not staged
git diff

# Commit your staged content
git commit -m [descriptive message]
Branch and merge
# List your branches
git branch

# Switch to another branch
git checkout my_branch

# Create and switch to a new branch
git checkout -b new_branch

# Delete the branch
git branch -d my_branch

# Merge the branch into the current
git merge [branch]
Remote
# Add a git URL
git remote add [alias] [url]

# Fetch down all the branches from remote
git fetch [alias]

# Merge a remote branch to current branch
git merge [alias]/[branch]

# Transmit local branch to the remote repo
git push [alias] [branch]

# Fetch and merge from remote branch
get pull
Temporary commits
# Save modified and staged changes
git stash

# List stack changes
git stash list

# Write working from top of stash
git stash pop

# Discard the stash
git stash drop
Tracking and Rewrite history
# Delete the file and stage the removal
git rm [file]

# Change a file and stage changes
git mv [old-path] [new-path]

# Apply commits of current branch
git rebase [branch]

# Rewrite working tree from commit
git reset --hard [commit]
.gitignore
logs/
*.notes
pattern*/

# Ignore node_modules folder
node_modules/
# Ignore Mac system files
.DS_Store
# "!" means don't ignore
!logs/.gitkeep

Config

.gitconfig
[core]
    autocrlf = false
    editor = vim
[user]
    name =
    email =
[alias]
    st = status
    br = branch
    co = checkout
    ci = commit
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
[init]
    defaultBranch = main