Dev In The Mountain Header
A Developer In The mountains having fun

Last reviewed

Git Commands Quick Ref

more...

Setup

bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Creating Repositories

git init - initialize a new Git repository

git clone [url] - clone a remote repository

General

git status

git add [files]

git add . - stage all changes in the current directory

git diff - diff of what is changed but not staged

git difftool

git diff --staged - diff of what is staged

git commit -m "[descriptive message]"

git commit -am "[descriptive message]" - commit and add all modified files to commit

branches

git branch - list branches (local)

  • Switches: -r - remote, -a - all

git branch [branch-name] - creates a new branch at current commit

git switch [branch-name] - switch to another branch (or the older git checkout [branch-name])

git switch -c [branch-name] - create a branch and switch to it (or the older git checkout -b [branch-name])

git merge [branch] - merge the specified branch into the current one.

git branch -d [branch-name] - delete a branch

Logs

git log - shows a log of all commits on current branch

git log --oneline - condensed log view

git log --follow [file] - show commits that changed the file (even across renames)

git show [commit] - show the details of a commit

share & remotes

git remote -v - list remotes

git remote add [alias] [url] - add a git URL as an alias

git fetch [alias] - fetch all branches from alias remote

git push - pushes to remote

git push -u origin [branch] - push a new branch to the remote and track it

git pull - fetch and merge from remote

Undoing Changes

git restore [file] - discard changes in working directory

git restore --staged [file] - unstage a file

git reset HEAD~1 - undo last commit (keep changes)

git reset --hard HEAD~1 - undo last commit (discard changes)

Rewrite History

git rebase [branch] - reapply the commits of the current branch on top of the specified one

Stash / temp storage

git stash — stashes current changes

git stash push -m "some message" — stashes current changes with a short message

git stash pop — restores last stash

git stash show — shows changes in most recent stash

Tags

git tag [name] — create a tag

Worktrees

Allows easy way to work on multiple branches at the same time.

git worktree add [path] [branch-name] — checks out the branch in a new directory at that path

git worktree list — lists all worktree directories and currently checkedout branch

git worktree remove [path] — removes a worktree from the repo

git worktree prune — removes worktrees that are no longer valid

More places to find me
Mental Health
follow me on Mastodon