1. How Does Git Store Data?

2. What is a Branch?

2.0 Repostiory history

  • Often times, we need to view the history of the repository:
git log
  • The entire history of the repository is available locally!

2.1 Creating a new branch

  • When you create a new branch, you actually create a new pointer for you to move around! As a default, the new pointer will always point at the commit you are currently on.
  • In this example, we create a new branch called testing:
git branch testing
  • As a result, a new pointer called testing is created and points at the current commit:

2.2 The HEAD pointer

  • To keep track of which branch you’re currently on, Git keeps a special pointer called HEAD.
  • In this case, we are still on master.
  • The git branch command only created a new branch, it did NOT switch to the new branch.

  • Now, to create a new branch and swith to this new branch at the same time, use git checkout with the -b flag!
git checkout -b ha_branch

2.3 Switching Branches

  • To switch to an existing branch, use the git checkout command.
git checkout testing
  • This moves HEAD to point to the testing branch.

  • What is the significance of that? Well, let’s do another commit! After running the git add and git commit commands, this is the result:

  • Now, we can switch back to the master branch:
git checkout master

  • That command did two things:
    • It moved the HEAD pointer back to point to the master branch, and it reverted the files in your working directory back to the snapshot that master points to.
    • It essentially rewinds the work you’ve done in your testing branch so you can go in a different direction.
  • Now, let’s make a few changes and commit again!

3. Branch Merging

git checkout master
git merge hotfix

git branch -d hotfix

3.1 Basic merging

  • Suppose you’ve decided that your issue #53 work is complete and ready to be merged into your master branch.
git checkout master
git merge iss53

  • Instead of just moving the branch pointer forward, Git creates a new snapshot that results from this three-way merge and automatically creates a new commit that points to it.
  • This is referred to as a merge commit, and is special in that it has more than one parent.

3.2 Merge conflicts

  • This is the part where Google becomes extremely handy (and I become less and less useful) to you!
  • There are many different merge conflicts. The textbook (Pro Git) covers some basic ones in section 3.2.
  • But based on my experience, it is best to search the internet for answer. Make sure to NOT do anything drastic (like deleting a branch or file or so on) since those actions are often inreversible!

This lecture notes referenced materials in 3.1, 3.2 sections of Pro Git by Scott Chacon and Ben Straub.