Git - Mental models and Cheatsheet

I’m collating here the basic mental models that help me reason about Git’s behavior. I plan to add more diagrams covering how various commands affect the object store as well as the working, staging and repo area both on the local and the remote repository.

Stages of a file

Post git init a file in the directory can be in one of many stages. This is useful to understand how commands such as git reset, git rebase work.

Nuanced understanding of how a command affects the stage of a file(s) helps avoid situations where one might lose data by using one of these commands.

file-stages

Local and remote repositories

Git is a distributed version control system. Each local repository also contains a copy of all the remote repositories it is linked to. This comes in handy to understand the difference between git fetch and git pull

git-repositories

git-reset

git-reset

Difference between git reset and git checkout

difference-between-git-reset-and-git-checkout

git tag

Used to highlight certain commits as a milestone in the project.

Command Result

 
Create  
git tag <tagname> Creates a tag at the existing HEAD
git tag <tagname> -m "message" <commit-id> Creates a tag at the ‘commit-id’ with the message
git tag -f <tagname> <commit-id> To update the ‘tagname’ to point at ‘commit-id’

 
Checkout  
git checkout <tagname> To checkout the specific tag

 
List  
git tag List all tags
git tag -n List all tags with their messages

 
Delete  
git tag -d <tagname> To delete the tag ‘tagname’

 
Push to remote  
git push origin <tagname> To push the ‘tagname’ to remote repo
git push --tags To push all the tags to remote


*git push by default does not push tags to the remote repository


git rebase

Rebasing is the process of moving or combining a sequence of commits to a new base commit

Use cases

Rebasing to an upstream branch

Allows to modify the base commit of the current branch to the latest commit in the upstream branch giving a linear history of commits during development

git-rebase-branch

Modifying commit history

git-rebase-modify-history

git rebase gotchas

References