Git

Last Updated: 8/23/2023

What are Branches

  • You can think of a branch like a separate, isolated workspace.
  • Main workspace is called Master
  • We can have another workspace for working on a new feature in isolation. While we're developing this new feature, our code may get unstable. So we don't want to release the code in to master workspace. We continue working here, when we're done, we test our code and after we fix all the bugs, then we bring the changes in this workspace into the master. This is called merging.
  • Branching allows us to work on different work items without messing up with the main line of work, we keep the main line as stable as possible, so we can release it anytime. Anyone joining the team can start off on a stable codebase.
  • Git branches are super fast and cheap, because a branch in Git is just a pointer to a commit.
  • So the master branch is just a pointer to the last commit in the main line of work. As we make new commits, git move this pointer forward automatically. So it knows what is the latest code in the main line of work. That is the snapshot stores in this commit.
  • When we create a new branch, git creates a new pointer that can be moved around. This pointer is just a tiny file that contains a 40 byte commit ID. That's why creating a branch in Git is blazingly fast. Now when we switch to this branch and make new commits git moves this pointer forward, the master pointer stays where it is, so git knows the latest code in each branch.
  • If you switch back to master, it takes the snapshot from the commit that master points to and resets our working directory to that snapshot. So we always have a single working directory.
  • git know which branch we're currently working on using a special pointer called head. This pointer is also another type of file that contains the name of a branch like master.
  • When we switch to a different branch. Git moves the head pointer out. so it updates a tiny file and writes the name of the target branch. This is how we can track that branch we're currently working on.