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
git-reset
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
Modifying commit history
- Edit a commit to add a minor change to an older commit (such as adding a comment)
- Squash commits together to make a meaningful chunk
- Reorder commit listing to preserve order of working on a sub-task
- Removing commits that do not add meaning to the commit history
git rebase
gotchas
-
If you modify a commit during interactive rebase, that commit and all successive commits will have new SHA-1’s
-
Avoid rebasing commits that have been pushed to remote repository