Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    Learning Git Source Control and Branching

    In this Git tutorial, we'll set up a GitHub account and learn how to use commands like git add and git commit to save our coding progress at various checkpoints. Git repositories are useful for keeping a history of your code, rolling back, collaborating with other developers, and working offline. Unlike SVN, Git is decentralized, meaning each of your developers' local machines contain the entire repository so they only need to connect to the remote repository when synching up, releasing and deploying.

    Read on to learn more.

    System requirements

    • A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
    • At least 50GB of free disk space.

    Set Up a Remote Repository

    Sign up for Github at http://github.com.

    Add your SSH key to GitHub by following this tutorial.

    Create a Repos Folder

    Navigate to your home directory, by typing the following command in your shell/terminal.

    cd ~
    

    If it doesn't already exist, create a repos folder to store all of your code repositories.

    mkdir repos
    

    Navigate into the repos folder.

    cd repos
    

    Create a folder for your new repository. We use all lowercase and dashes in place of spaces to match GitHub's repo naming convention.

    mkdir my-project
    

    You should now have the following folder structure.

    ▾ home/
      ▾ repos/
        ▸ my-project/
    

    Navigate into my-project.

    cd my-project
    

    Create A New Code Repository In Github

    Follow the instructions on GitHub's documentation. The following git intialization steps are also provided in the documentation, but I'll list them out here, anyway.

    Initialize a new repository and link to the remote repo..

    mkdir my-project
    cd my-project
    echo "# my-project" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git remote add origin git@github.com:organization/my-project.git
    git push -u origin master
    

    Create a "develop" Branch

    After initializing the repository, we'll be positioned at the master trunk. Think of your code history as a tree. master is the main trunk and we'll want to create branches off of that trunk. So before we move forward, just visualize a tree's main trunk lying on its side with "master" written on it, like so:

    =master=====
    

    Now, let's create a branch off of master and name it "develop", by typing the following command in our shell/terminal:

    git checkout -b develop
    

    Now, picture a large branch growing out of the master with "develop" written on it.

        .====develop
       /
    master
    

    Our master branch is now safe. Anything we do to our code right now will modify develop. If, for some reason, we want to roll back or delete our branch altogether, we're free to do so.

    However, we don't want to make any edits to our code while we're standing on the develop branch. The master branch and the develop branch each have their own purpose.

    The Master Branch's Purpose

    The master branch is reserved only for packaging a release of our software. For example, when our software passes testing and we're ready to launch, we'll create a v1.0.0 "release" version. We're not ready to do that yet, though, which is why we're not on the master branch.

    The Develop Branch's Purpose

    The develop branch is reserved for creating feature branches. So yes, branches can have their own branches, just like a normal tree. For now, just follow along. It will be easier to understand once we visualize the tree structure.

    Type the following command in, which will push the new develop branch to the remote repository. We're essentially making a copy of our progress within GitHub.

    git push origin develop
    

    Creating a New Feature Branch

    The following commands will create a branch named rl_feature. Feel free to replace "rl" with your own initials. The naming convention for branches is initials/feature-name.

    git checkout -b rl_feature
    git push origin rl_feature
    

    Now, visualize this:

                 .==rl_feature
                /
        .====develop
       /
    master
    

    We're free to update our code and save our progress on the rl_feature branch. Other developers may also branch off of develop like so:

                 .==rl_feature
                /
        .====develop
       /         \
    master        \====tr_feature-2
    

    This allows us to merge all the branches back into develop when we're finished and stitch all of the code together like so:

                 .==rl_feature==.           .==rl_feature-3
                /                \         /
        .====develop--------------*----*==/
       /         \                    /
    master        \====tr_feature-2==/
    

    This process is a constant loop of branching off, making edits, merging back in so that develop is updated with everyone's features.

    The Code and Commit Loop

    If we were to zoom in to one of these feature branches, we would see the history of the developer's progress in the form of commits. In the following example, we're going to create a new file and update it, making commits along the way.

    touch test.txt
    git add .
    git commit -m "Created a text file"
    echo "My code" >> test.txt
    git commit -m "Updated the text file"
    git push origin rl_feature
    
                              commit  commit
                                |       |
                 .==rl_feature==*=======*
                /   
        .====develop
       /           
    master
    

    Think of commits as checkpoints we can roll back to. You want to make meaningful commits when you hit certain milestones or just any time you feel like experimenting with code you'll want to undo.

    Merging Your Feature Branch Back into Develop

    These are the commands for merging our code into the develop branch so that our feature can be shared with the rest of the team.

    git checkout develop
    git merge rl_feature
    git push origin develop
    
                              commit  commit
                                |       |
                 .==rl_feature==*=======*-.
                /                          \
        .====develop----------------------merge
       / 
    master
    

    Finally, we merge develop into the master trunk and create a release version of our software. We then create a new feature branch off of develop and repeat the code and commit loop all over again.

                              commit  commit
                                |       |
                 .==rl_feature==*=======*-.
                /                          \
        .====develop----------------------merge
       /                                     \
    master-----------------------------------merge---.---
                                                      \
                                                  v1.0.0 release
    

    Deleting Old Branches

    Once we've merged our feature branch into develop, after a considerable amount of time has passed, we go through and clean up old branches with the following commands.

    First, we make sure we're on develop or master. We can't merge or delete a branch we're currently on.

    ```bash git checkout develop ```

    Then we delete the branch from both the remote and the local repository.

    ```bash git push -d origin rl_feature git branch -d rl_feature ```

    Updating Your Local Repository

    Issue the following command. Replace initials/feature-branch with the branch name.

    git pull origin initials/feature-branch
    

    You'll do this more often on develop because other developers will be merging their branches into develop and you may want to update it before you merge in.

    git pull origin develop
    

    Other Useful Git Commands

          
          # Display any edits / if the branch is up to date
          git status
          # View which branch you're on
          git branch
          # View git history
          git log
          # View git history with branches
          git log --graph --full-history --all --color \
                  --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"]]]"
          
      

    Conclusion

    That concludes this Git tutorial. In part two of this series, we'll go over advanced git commands, and the real-world scenarios they address. For now, we should be fine with these commands as they satisfy the most common use cases.

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.