Return to Video

Branches - How to Use Git and GitHub

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

more » « less
Video Language:
English
Team:
Udacity
Project:
UD775 - How to Use Git and GitHub
Duration:
02:53

English subtitles

Revisions Compare revisions