# Git Command Workflow Sequence Diagram ```mermaid %% https://mermaid.js.org/syntax/sequenceDiagram.html#styling %% It would be cool to add sequences to the top from the column names %%{ init: { "theme": "base", "sequence": { "actorFontFamily": "monospace", "actorFontWeight": "bold", "messageFontFamily": "monospace", "messageFontWeight": "bold", "noteFontWeight": "bolder" } } }%% sequenceDiagram %% box transparent Local %% actor Developer %% Stash+StagingArea ~//.git/refs participant Stash %% WorkingDirectory ~/ participant WorkingDirectory as Working
Directory %%(Index) participant StagingArea as Staging
Area %% LocalRepository+RemoteBranchTracking ~//.git participant LocalRepository as Local
Repository participant RemoteBranchTracking as Remote
Branch
Tracking %% end %% box transparent Remote participant RemoteRepository as Remote
Repository %% end opt Setup LocalRepository ->> LocalRepository: git config Note over LocalRepository: configures global or repository-specific Git settings,
such as the username, email, and editor LocalRepository ->> LocalRepository: git init Note over LocalRepository: initializes a new Git repository RemoteRepository -->> LocalRepository: git clone Note over RemoteRepository, LocalRepository: clones a repository from a remote source RemoteRepository ->> RemoteRepository: git remote Note over RemoteRepository: displays and manages remote repository connections end opt Branching LocalRepository ->> LocalRepository: git branch Note over LocalRepository: creates a new branch LocalRepository ->> LocalRepository: git switch Note over LocalRepository: switches between branches.
Use `-c` to create and switch. LocalRepository ->> LocalRepository: git checkout Note over LocalRepository: switches to a different branch LocalRepository ->> LocalRepository: git branch Note over LocalRepository: lists all branches in the repository LocalRepository ->> LocalRepository: git branch --all Note over LocalRepository: lists all local and remote
branches in the repository. LocalRepository ->> LocalRepository: git branch -d Note over LocalRepository: deletes a local branch safely,
ensuring no unmerged changes are lost. LocalRepository ->> LocalRepository: git branch -m Note over LocalRepository: renames a branch locally. end WorkingDirectory -->> Stash: git stash Note over WorkingDirectory, Stash: temporarily saves uncommitted changes
that are not yet ready to be committed Stash ->> Stash: git stash list Note over Stash: displays a list of all stashes
available for the repository Stash ->> Stash: git stash drop Note over Stash: removes a specific stash entry
by its index or name Stash ->> WorkingDirectory: git stash pop Note over Stash, WorkingDirectory: applies and drops changes from the stack Stash ->> WorkingDirectory: git stash apply Note over Stash, WorkingDirectory: ONLY applies changes WorkingDirectory ->> StagingArea: git add/mv/rm Note over WorkingDirectory, StagingArea: adds files to the staging area StagingArea ->> LocalRepository: git commit Note over StagingArea, LocalRepository: commits changes to the repository WorkingDirectory ->> LocalRepository: git commit -a Note over WorkingDirectory, LocalRepository: automatically stages all tracked files
that have been modified or deleted.
It does NOT stage:
New (untracked) files or
files that have not been previously committed LocalRepository ->> RemoteRepository: git push Note over LocalRepository, RemoteRepository: pushes changes to a remote repository LocalRepository ->> LocalRepository: git tag Note over LocalRepository: creates an immutable references for a specific commit opt Update RemoteRepository -->> WorkingDirectory: git pull Note over RemoteRepository, WorkingDirectory: pulls changes (fetch + merge).
Use `--rebase` for rebased history. RemoteRepository -->> RemoteBranchTracking: git fetch Note over RemoteRepository, RemoteBranchTracking: fetches changes from a remote repository RemoteRepository -->> RemoteBranchTracking: git fetch --prune Note over RemoteRepository, RemoteBranchTracking: fetches updates and prunes
stale branches. RemoteBranchTracking -->> WorkingDirectory: git merge Note over RemoteBranchTracking, WorkingDirectory: merges changes from one branch into another RemoteRepository -->> WorkingDirectory: git rebase Note over RemoteRepository, WorkingDirectory: reapplying commits on top of a fetched branch WorkingDirectory -->> WorkingDirectory: git rebase -i Note over WorkingDirectory: interactively edits commit history,
squashes, or reorders commits. LocalRepository -->> WorkingDirectory: git cherry-pick Note over LocalRepository, WorkingDirectory: applies the changes introduced by a specific commit to the current branch end opt Status WorkingDirectory -> StagingArea: git status Note over WorkingDirectory, StagingArea: shows the status of the repository %% shows the author, commit hash, and timestamp for each line of a file, helping identify when and by whom changes were made LocalRepository ->> LocalRepository: git show Note over LocalRepository: used to display information about a commit LocalRepository ->> LocalRepository: git blame Note over LocalRepository: shows who made changes to each line of a file LocalRepository ->> LocalRepository: git log Note over LocalRepository: shows a log of all the commits in the repository LocalRepository ->> LocalRepository: git whatchanged Note over LocalRepository: deprecated but useful for showing commit
history alongside detailed file changes end opt Reset & Revert WorkingDirectory ->> WorkingDirectory: git clean Note over StagingArea, WorkingDirectory: permanently removes untracked files and
directories from the working directory StagingArea -->> WorkingDirectory: git reset Note over StagingArea, WorkingDirectory: discards changes made to a file LocalRepository -->> StagingArea: git reset --soft Note over LocalRepository, WorkingDirectory: resets the repository to a previous commit, but keeps changes staged LocalRepository -->> WorkingDirectory: git reset --mixed Note over LocalRepository, WorkingDirectory: resets the repository to a previous commit
and keeps changes them in the working directory LocalRepository -->> WorkingDirectory: git reset --hard Note over LocalRepository, WorkingDirectory: resets the repository to a previous commit
and removes changes from the working directory and staging area.
WARNING: this removes all uncommitted changes permanently StagingArea -->> WorkingDirectory: git restore Note over StagingArea, WorkingDirectory: restores files to a previous state
without switching branches or commits. StagingArea -->> WorkingDirectory: git checkout Note over StagingArea, WorkingDirectory: discards changes made to a file and
restores it to its last committed state LocalRepository -->> WorkingDirectory: git checkout HEAD Note over LocalRepository, WorkingDirectory: discards changes made to all tracked files
and restores them to their last committed state LocalRepository -->> WorkingDirectory: git revert Note over LocalRepository, WorkingDirectory: creates a new commit that undoes
the changes from a previous commit LocalRepository -->> WorkingDirectory: git revert --no-commit Note over LocalRepository, WorkingDirectory: prepares a revert without creating
an immediate commit, allowing
additional changes before finalizing end opt Compare WorkingDirectory -> StagingArea: git diff Note over WorkingDirectory, StagingArea: shows the differences
between commits or branches WorkingDirectory -> LocalRepository: git diff HEAD Note over WorkingDirectory, LocalRepository: compares working directory
changes against the last commit WorkingDirectory -> StagingArea: git diff --staged Note over WorkingDirectory, StagingArea: compares staged changes
against the last commit. WorkingDirectory -> StagingArea: git diff Note over WorkingDirectory, StagingArea: compares two specific commits. WorkingDirectory -> StagingArea: git diff --cached Note over WorkingDirectory, StagingArea: alternative to --staged, showing
differences in the staging area. end opt Search & Debugging LocalRepository ->> LocalRepository: git grep Note over LocalRepository: searches for text in the repository LocalRepository ->> LocalRepository: git bisect Note over LocalRepository: automates binary search through
commits to find the introduction of a bug LocalRepository ->> LocalRepository: git reflog Note over LocalRepository: inspects reference logs
to recover lost commits
or check HEAD movements. end opt Conflict Resolution WorkingDirectory ->> WorkingDirectory: git mergetool Note over WorkingDirectory: launches a merge tool to resolve conflicts WorkingDirectory ->> WorkingDirectory: git checkout --ours/theirs Note over WorkingDirectory: selects the local/remote version of a conflicting file WorkingDirectory ->> WorkingDirectory: git rerere Note over WorkingDirectory: records resolved conflicts and reuses
them when the same conflict occurs again end ```