English 字幕

← Branches - How to Use Git and GitHub


Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. You've just learned how to initialize your repository and
  2. add commits to it using the staging area.
  3. So far, each commit that you've made has built upon the previous commit,
  4. creating a linear commit history.
  5. Now let's take a look at some times when you might want to make a commit history
  6. that branches out into multiple versions.
  7. Making a straight line of changes makes a lot of
  8. sense if you're just fixing bugs, adding new features or updating documentation.
  9. But what if you wanted to try out a new experimental feature that
  10. you're still not sure will work, but you still want to have a working demo to
  11. show your friends when they ask what you're working on?
  12. Or maybe you're learning Italian and want to make a version of your project for
  13. Italian-speaking people while leaving the native language version intact.
  14. In this case, you'll probably want to eventually create a setting for
  15. switching between the two languages.
  16. But maybe as a first step, you just try changing the text and
  17. seeing if you need to rework your layout a little.
  18. Of course, you could just make your changes and
  19. record the IDs for all the commits.
  20. And then use git checkout to jump back to the real official version when you
  21. need to show it to somebody.
  22. And then back to your other version afterward.
  23. But what if you find a bug in your real official version and
  24. you need to make a fix there?
  25. You need to update your records and use this new commit as your real version.
  26. This would be a lot of overhead.
  27. To make this kind of situation easier, Git allows you to create labels for
  28. your commits.
  29. These labels are called branches.
  30. In this case, we would have three different branches.
  31. First, we would have master.
  32. Master is the name given to main branch in most Git repositories and
  33. every time you create a repository, Git creates a master branch for you.
  34. This branch we would probably call something like italian and
  35. this one we might call experimental or maybe just exp for short.
  36. Remember how in last lesson when you checked out some old commits,
  37. you'd get the detached head message?
  38. Basically at that point, Git was warning you that you were looking at
  39. a commit that was not labeled with a branch name.
  40. So far, you've always been in a detached head state or on the master branch,
  41. which Git creates for you.
  42. It's possible to check out a branch in the same way you checked out commits in
  43. the past, except with the hopefully human-readable branch name rather than
  44. an unpronounceable series of characters.
  45. If you check out a branch and
  46. then make a commit, the branch label automatically updates to the new commit.
  47. That branch also stays checked out, so you don't have to check it out again.
  48. This is how you've managed to stay on the master branch without knowing anything
  49. about branches up til this point.
  50. As a matter of terminology, we sometimes refer to the current last commit on
  51. a branch as being the tip of that branch.
  52. It is possible to have multiple branch labels attached to a single commit, but
  53. making a new commit will only update the branch that you have checked out and
  54. leave any others alone.
  55. At this point,
  56. it's natural to wonder if there's any way to combine two branches,
  57. say if your experimental feature has finally worked out, but
  58. your main branch has grown since the two diverged.
  59. The process of combining two commits is known as merging and
  60. we'll get to that later on in the lesson.
  61. But first, we want you to get comfortable creating, checking out and
  62. committing to branches.
  63. Caroline will help you get started with this.