Skip to content

Instantly share code, notes, and snippets.

@everget
Last active December 18, 2024 15:24
Show Gist options
  • Save everget/941d15988c8816a32818de83ba577c72 to your computer and use it in GitHub Desktop.
Save everget/941d15988c8816a32818de83ba577c72 to your computer and use it in GitHub Desktop.
A detailed Mermaid sequence diagram illustrating the complete Git workflow, showcasing command interactions across local and remote repositories, including setup, branching, staging, committing, pushing, pulling, and various advanced Git operations.

Git Command Workflow Sequence Diagram

%% 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 ~/<repo>/.git/refs
	participant Stash

	%% WorkingDirectory ~/<repo>
	participant WorkingDirectory as Working<br/>Directory

	%%(Index)
	participant StagingArea as Staging<br/>Area

	%% LocalRepository+RemoteBranchTracking ~/<repo>/.git
	participant LocalRepository as Local<br/>Repository
	participant RemoteBranchTracking as Remote<br/>Branch<br/>Tracking
%% end


%% box transparent  Remote
	participant RemoteRepository as Remote<br/>Repository
%% end


opt Setup
	LocalRepository ->> LocalRepository: git config
	Note over LocalRepository: configures global or repository-specific Git settings,<br/>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 <branch_name>
	Note over LocalRepository: creates a new branch

	LocalRepository ->> LocalRepository: git switch <branch_name>
	Note over LocalRepository: switches between branches.<br/>Use `-c` to create and switch.

	LocalRepository ->> LocalRepository: git checkout <branch_name>
	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<br/>branches in the repository.

	LocalRepository ->> LocalRepository: git branch -d <branch_name>
	Note over LocalRepository: deletes a local branch safely,<br/>ensuring no unmerged changes are lost.

	LocalRepository ->> LocalRepository: git branch -m <old_name> <new_name>
	Note over LocalRepository: renames a branch locally.
end


WorkingDirectory -->> Stash: git stash
Note over WorkingDirectory, Stash: temporarily saves uncommitted changes<br/>that are not yet ready to be committed

Stash ->> Stash: git stash list
Note over Stash: displays a list of all stashes<br/>available for the repository

Stash ->> Stash: git stash drop
Note over Stash: removes a specific stash entry<br/>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<br/>that have been modified or deleted.<br/>It does NOT stage:<br/>New (untracked) files or<br/>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).<br/>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<br/>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,<br/>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<br/>history alongside detailed file changes
end


opt Reset & Revert
	WorkingDirectory ->> WorkingDirectory: git clean
	Note over StagingArea, WorkingDirectory: permanently removes untracked files and<br/>directories from the working directory

	StagingArea -->> WorkingDirectory: git reset <file>
	Note over StagingArea, WorkingDirectory: discards changes made to a file

	LocalRepository -->> StagingArea: git reset --soft <commit>
	Note over LocalRepository, WorkingDirectory: resets the repository to a previous commit, but keeps changes staged

	LocalRepository -->> WorkingDirectory: git reset --mixed <commit>
	Note over LocalRepository, WorkingDirectory: resets the repository to a previous commit<br/>and keeps changes them in the working directory
	
	LocalRepository -->> WorkingDirectory: git reset --hard <commit>
	Note over LocalRepository, WorkingDirectory: resets the repository to a previous commit<br/>and removes changes from the working directory and staging area.<br/>WARNING: this removes all uncommitted changes permanently

	StagingArea -->> WorkingDirectory: git restore
	Note over StagingArea, WorkingDirectory: restores files to a previous state<br/>without switching branches or commits.

	StagingArea -->> WorkingDirectory: git checkout <file>
	Note over StagingArea, WorkingDirectory: discards changes made to a file and<br/>restores it to its last committed state

	LocalRepository -->> WorkingDirectory: git checkout HEAD
	Note over LocalRepository, WorkingDirectory: discards changes made to all tracked files<br/>and restores them to their last committed state

	LocalRepository -->> WorkingDirectory: git revert <commit>
	Note over LocalRepository, WorkingDirectory: creates a new commit that undoes<br/>the changes from a previous commit

	LocalRepository -->> WorkingDirectory: git revert --no-commit <commit>
	Note over LocalRepository, WorkingDirectory: prepares a revert without creating<br/>an immediate commit, allowing<br/>additional changes before finalizing
end


opt Compare
	WorkingDirectory -> StagingArea: git diff
	Note over WorkingDirectory, StagingArea: shows the differences<br/>between commits or branches

	WorkingDirectory -> LocalRepository: git diff HEAD
	Note over WorkingDirectory, LocalRepository: compares working directory<br/>changes against the last commit

	WorkingDirectory -> StagingArea: git diff --staged
	Note over WorkingDirectory, StagingArea: compares staged changes<br/>against the last commit.

	WorkingDirectory -> StagingArea: git diff <commit1> <commit2>
	Note over WorkingDirectory, StagingArea: compares two specific commits.

	WorkingDirectory -> StagingArea: git diff --cached
	Note over WorkingDirectory, StagingArea: alternative to --staged, showing<br/>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<br/>commits to find the introduction of a bug

	LocalRepository ->> LocalRepository: git reflog
	Note over LocalRepository: inspects reference logs<br/>to recover lost commits<br/>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<br/>them when the same conflict occurs again
end
Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment