Git - Mental models and Cheatsheet · Blog

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.


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




Difference between git reset and git checkout


git tag

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

Command Result

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’

git checkout <tagname> To checkout the specific tag

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

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


Modifying commit history


git rebase gotchas