Branching allows you to work on multiple versions of your code at once, meaning you are free to experiment until you are ready to commit.
git branch: list out branches
git branch branch-name: create a new branch
git checkout branch-name: switch to the newly created branch
git checkout -b branch-name: shortcut! - use this command to create & switch to the new branch
git push origin branch-name: push branch & changes to github
Branching allows you to conveniently work on multiple versions of your code at once. 0:00 It's an exact copy of the original branch as it was at that point in time. 0:05 This means you're free to experiment and commit changes, 0:09 safe in the knowledge that your new branch won't be merged until you're ready. 0:11 Let's create a branch to work on the readme.md file. 0:15 In the console, we'll type git branch and 0:19 the name of the branch we would like to create, add-readme. 0:22 Branch names cannot contain spaces, so 0:27 we generally use the hyphen to separate words in our branch names. 0:29 Now when we type git branch, we see the name of our new branch, but 0:33 we're still checked out to master. 0:37 Type git checkout with the name of the branch that we'd like to check out. 0:39 Switch to branch add-readme. 0:46 Great! 0:49 Here's a short cut, git checkout-b and the name of the branch will create 0:50 the branch and switch you to it with one command. 0:54 From here, we'll create a readme.md file and 0:57 add some information about our repository. 1:00 What makes a good readme? 1:02 Well, it depends on the type of project. 1:04 However, for our project, a simple description of what it is will suffice. 1:06 So, here we'll just type touch README.md and 1:11 in workspaces we just Refresh, and we can see our README.md file is there. 1:18 We can double-click on it. 1:25 In the text editor, we'll start with a header. 1:28 We'll say Treehouse. 1:34 This repo is for all my 1:36 course files completed 1:41 through team treehouse. 1:46 And we'll save. 1:51 Okay, so now we'll use the commands from before, but with one small, but 1:53 important difference. 1:57 Instead of pushing to the master branch, we'll push to our new branch, add-readme. 1:58 So we'll type git status on branch add-readme, 2:05 Untracked files READM.md. 2:11 Okay, so we'll need to add git add. 2:15 Git commit- m, and we'll use 2:20 a message "add readme file". 2:25 Great. 2:30 And, we'll git push origin add-readme. 2:31 And we'll enter our credentials again. 2:39 We'll go to our GitHub repo. 2:49 And click Refresh, and here we can see that our 2:53 new branch was pushed up, the add-readme branch. 2:57 On the website, 3:02 we can switch between the branches just like we can on our local computer. 3:03 We can see on the master branch there is no readme, and 3:06 on the add-readme branch there is. 3:09 We can also see that there is now a new prompt, Compare & pull request. 3:12 In the next video, we'll cover how to create a pull request to 3:18 pull your changes into the master branch from the new branch. 3:20 But first, a review. 3:24 In this video, we learned [SOUND] that we can create and 3:26 switch to a new branch from the master branch with the git checkout -b command. 3:29 Then, we added a file, committed the change, [SOUND] and 3:33 pushed up to GitHub using the git push origin branch command. 3:36
You need to sign up for Treehouse in order to download course files.Sign up