Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What is a git branch?

mm
October 13, 2014

What is a git branch?

...

mm

October 13, 2014
Tweet

Other Decks in Technology

Transcript

  1. BRANCHING? • Nearly every VCS has some form of branching

    • Diverging from main line of development • Represents an independent line of development • Git’s branching model is it’s killer feature
  2. GIT BRANCHING • Git doesn’t store data as a series

    of changesets, but as a series of snapshots. • commit - Git stores a commit object that contains a pointer to the snapshot of the content you staged, author and message, and zero or more pointers to the commit or commits that were direct parents of this commit.
  3. VISUALIZE IT • Lets say we have a directory containing

    3 files. You stage them all and commit. • Staging - checksums each one, stores that version of the file in git repository (referred to in git as blobs), and adds checksum to staging area $ git add README test.rb LICENSE! $ git commit -m 'initial commit of my project'
  4. VISUALIZE IT CONT. • Git commit checksums project directories and

    stores them as tree objects in the git repository. • Git creates commit object with pointer to the root project tree object so it can re-create that snapshot when needed.
  5. WHAT IS LOOKS LIKE • Git repository contains 5 objects:

    • a blob or the contents of each file • a tree listing contents of the directory. • one commit with the pointer to that root tree and all the commit metadata
  6. WHAT IT LOOKS LIKE CONT. • Lets say we make

    some changes and commit: • You can see each commit stores a pointer to the commit that came before it. So after 3 commits ^^^…
  7. …ON TO BRANCHES • A simple, lightweight, moveable pointer to

    one of these commits. • Default branch is named ‘master’. • As you make commits master points to the last commit you made. • Moves forward automatically every time you commit.
  8. CREATING A NEW BRANCH • This creates a new pointer

    to the commit you are currently on. $ git branch testing
  9. HOW DOES GIT TRACK YOUR MOVEMENT? • Keeps a special

    pointer: ‘HEAD’ • HEAD is a pointer to the local branch you’re currently on.
  10. BRANCH MOVEMENT CONT. • Lets say we switch branches: $

    git checkout testing ! ! ! • This moves HEAD to point to the testing branch.
  11. BRANCH MOVEMENT CONT. • If we were to make a

    commit on testing… • The HEAD pointer moves forward with each commit. • master branch still points to the commit we were on before we switched branches.
  12. BRANCH MOVEMENT CONT. $ git checkout master! •Head pointer moved

    •Reverts files in working directory back to the ‘snapshot’ master points to. •Any changes here will diverge from older version. •Almost a rewind.
  13. BRANCH MOVEMENT CONT. • Lets say we make some changes

    and commit… • Project history diverges! • Changes isolated.
  14. EXTRAS • A git branch is actually a simple file

    that contains the 40 character checksum of the commit it points to. • This makes branches cheap to create and destroy. • It’s as simple as writing 41 bytes to a file (40 characters and a newline) • Other VCS’ can be much slower and involve copying project files, whereas git is instantaneous. • So use branches!!!
  15. USAGE • git branch - List all branches in repository

    • git branch <branch> - Create a new branch • git branch -d <branch> - Delete branch (safe, prevents deleting the branch if it has unmerged changes) • git branch -D <branch> - Force delete branch with unmerged changes (throw away) • git branch -m <branch> Rename current branch to <branch>
  16. DEVELOPMENT • Spawn branches to encapsulate changes for a new

    feature. • Make sure unstable code is never committed to the main code base. • Allows you to clean up your features history before merging into main branch.
  17. END • Use git branch and git status regularly! •

    Remind yourself which branch you are on • Avoid accidentally committing code where you didn't intend.