- For this topic, we will use the materials provided in this textbook: Pro Git by Scott Chacon and Ben Straub.
1. How Does Git Store Data?
- Git doesn’t store data as a series of changesets or differences, but instead as a series of snapshots.
- If you make some changes and commit (
git commit), the next commit stores a pointer to the commit that came immediately before it.
2. What is a Branch?
- A branch in Git is simply a lightweight movable pointer to one of these commits.
- The default branch name in Git is
- As you start making commits, you’re given a
master branch that points to the last commit you made.
- Every time you commit, the
master branch pointer moves forward automatically.
2.0 Repostiory history
- Often times, we need to view the history of the repository:
- 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
git branch testing
- As a result, a new pointer called
testing is created and points at the current commit:
- To keep track of which branch you’re currently on, Git keeps a special pointer called
- In this case, we are still on
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
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
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
- There are many workflows that involve branches. One that you might see often is:
- A team consisting multiple team members. Each works on a different part of the project.
- The project is hosted on GitHub.
- Each person creates a branch for themselves (maybe with their name or with the task name, like
- Each works on their individual branch.
- Merge all the individual branches to put the project together.
- So merging is a big part of Git branches.
- Let’s say we have the following situation:
- You can run your tests, make sure the
hotfix is what you want, and finally merge the
hotfix branch back into your
git checkout master
git merge hotfix
- Now, there is no need for
hotfix anymore, so we can delete it!
git branch -d hotfix
- After that, let’s further work on
3.1 Basic merging
- Suppose you’ve decided that your issue #53 work is complete and ready to be merged into your
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.