In this module we will explain and show you the importance of being able to branch, merge, and revert your current state of your repository.
The basic GIT workflow is as follows: Create repository -> create or copy over files -> stage them to be saved at their current state -> commit them -> share the commit with your team -> work on new ideas or sections of the project -> merge them back with the original branch -> repeat.
Branching within Git allows us to work on our own section of the project without effecting anything on the main branch called Master. This allows us to work on a project within a team and add new modules or sections to our project in an organized manor.
- To create a new branch within SourceTree click on the Branch button in the top bar.
- Fill in
experiment2
for the New Branch name. - Click Create Branch
We will now make a quick change to the readme file within the new branch. Typically within a new branch you will make many more changes than this quick change.
- First verify we are working on the
experiment2
branch by making sure that is is bolded below the Branches tab. - Click on the Explorer button to open up the directory for our repository.
- Open readme.md and modify it as follows:
# Readme for Git-101
Hello there this is my latest update from branch exp2!
- Save the readme and close it. Head back to SourceTree.
- Stage the readme file.
- Click Commit and type out a commit message.
- Click Commit.
Merging allows us to take our current changes within a branch and apply them to another branch. We will merge our experiment2 branch with the main Master branch in this example.
- Within SourceTree double click on the master branch under Branches to checkout that branch.
- Click on Merge in the top menu.
- Select the experiment2 branch and click OK to merge your changes.
- The master branch now is the same as the experiment2 branch, all of our changes have been merged together.
Reverting allows us to move our current state of the repository back to a commit that happened previously. If you follow best practice by committing often you will have a few commits to choose from, or a larger history of what happened. Reverting is highly valuable if you or a teammate made a mistake and you would like to roll their changes back.
- Within SourceTree right click on the latest commit where master is equal to excitement2.
- Select Reverse commit
- Choose Yes to verify that you would like to roll back the latest commit to the master branch.
- You can inspect the changes to our latest commit by looking at the lower right pane and seeing the differences in the readme file.
- We now should push our changes to GitHub. Click on Push from the top menu.
- Here you can decide to push the experiment2 branch or only update the master. In our case we will only update the master by clicking Push.
- Our changes will be uploaded to our remote repository, GitHub.