Git — the commands you actually need to look up

/6E
Public by a guest 17h ago 1.7 KB 7 views
Raw Download
Report
# UNDO THINGS (the reason most people search git)
git restore <file>                 # discard unstaged changes to a file
git restore --staged <file>        # unstage, keep the changes
git reset --soft HEAD~1            # undo last commit, KEEP changes staged
git reset --hard HEAD~1            # undo last commit, THROW AWAY changes
git revert <sha>                   # undo a pushed commit safely (new commit)
git commit --amend                 # fix the last commit message or add a file
git reflog                         # find a commit you think you lost

# BRANCHES
git switch -c new-branch           # create and switch (modern 'checkout -b')
git branch -d old-branch           # delete merged branch (-D to force)
git push origin --delete old       # delete the remote branch
git branch -m newname              # rename the current branch

# LOOKING AROUND
git log --oneline --graph --all    # the one worth aliasing
git diff --staged                  # what you are about to commit
git blame -L 10,20 <file>          # who touched these lines
git show <sha>:<path>              # a file as it was at that commit

# STASH
git stash push -m "wip"            # stash with a message
git stash list
git stash pop                      # apply AND drop
git stash apply stash@{1}          # apply a specific one, keep it

# REMOTES
git remote -v
git fetch --prune                  # drop local refs to deleted remote branches
git pull --rebase                  # linear history instead of a merge bubble

# EMERGENCY
git checkout <sha> -- <file>       # restore one file from an old commit
git clean -nd                      # DRY RUN: what would be deleted
git clean -fd                      # actually delete untracked files/dirs