Let's talk about branches. We've touched on these very briefly in previous posts in this series, but let's look at them on their own. (And do it in five minutes!)
Hi: notes for absolute beginners may show up like this.
In Git, branches are used to isolate changes from the rest of the code base.
This way, you can keep the spaghetti code you're using to build a new feature or fix a bug to yourself until you're ready to merge it with the main code.
The master branch is typically considered the working branch, which means that any changes you make there may effect anyone - or anything - using the code. Not where you want to play around with a new feature!
Here we go:
For the rest of this I'm going to assume you're using GitHub, but there are lots of other Git hosting services out there. Remember, Git and GitHub are two different things.
First, you need to create your repository and navigate there in your terminal.
You can see which branch you're currently in by doing:
You should see something like:
On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
If you don't, go back and make sure that your repository is pointed at the correct remote
Now, make a new branch by doing:
git checkout -b new-feature
-b is saying "make a new branch!
You should now see:
Switched to a new branch 'new-feature'
You've now created a new branch!
Now, make some changes. I'm just going to create a file called
You can create a new file with
touch README.md in your terminal.
git add . and
git commit -m "Adds README"
This adds everything in the current directory
. and commits the changes. More here.
You should see:
[new-feature (root-commit) e18e06c] Adds README 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.md
Notice that the commit message went under the branch
new-feature in the first line of that message.
Now, push to the remote branch:
git push origin new-feature
You'll see something like:
Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Writing objects: 100% (3/3), 216 bytes | 216.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To github.com:afteralec/git-blog.git * [new branch] new-feature -> new-feature
Done! Pretty easy, right?
In a real project, you'd continue committing and pushing to your branch until your code was ready to see the light of day. (Or, at least, be merged with the main code of the project in the master branch!)
Typically, this is done using a pull request.
GitHub has a great walk-through on it here, but again, use whichever platform you've been using until now.
With GitHub, it's pretty easy:
Open the pull request from the repository's page. Click the pull requests tab at the top and then new pull request.
Follow the prompts and complete the form
Open the pull request!
In this example, as the repository owner, you'll be able to merge your own pull request - this isn't always the case!
For our purposes, go ahead and merge your changes.
From here, you have two options:
- Delete your branch.
- Merge the new master code into your branch.
For this, go ahead and delete the branch. Our new feature (the README file) has been added, and there's nothing else to do with it for now!
Back in your terminal (in the local repository) do:
git checkout master
Without the -b flag, this moves you around from branch to branch
This part's important - remember, you made changes in the new-feature branch, not the master branch, so this brings you back up-to-speed with the current master code.
You can also delete your branch locally!
To see your branches, do:
Flags again! You can add
-r to see only the remote branches, and add
-a- to see all branches.
git branch -D new-feature
You should see something like:
Deleted branch new-feature (was e18e06c).
This has been Git Branches in (Less Than?) Five Minutes.
If this helped you, you have something to add, or you or want to tell me why you think birds aren't real, leave a comment below.