Version Control part 5
Week 1 - Version control
Branches
By default when we create a repository in Git/GitHub we have what is called the master branch, from this master branch we have other branches kind of like a tree has a center and branches stemming off. The master branch acts as the established code of a project, code that is tested and we know we are keeping. With branches we can try out adding new code to the project without effecting the master, it acts as a testing ground if you will. Once we are happy with the code we made on the branch we can simply merge it onto the master. Whilst in a branch all commands are the same but they wont effect the master in any way.
How do we do this? We do this with the command “git branch” followed by the branch name, example “git branch “my-new-feature””. To see all branches within a project we enter the command “git branch -a” in addition to showing us all the branches it will also indicate which branch we presently are on. To switch to a branch we use “git checkout” followed by the branch name, example “git checkout “my-branch name””. We can now jump back and forth between each branch using the checkout command, remember to use the git branch -a command to ensure your in the right place.
Now say we want to remove a branch, to do this we first checkout to the master branch, then enter the command “git branch -D” followed by the branch name, example “git branch -D “branch-name””. Finally when we are happy with the code in a branch we use the command “git merge” whilst in in the master branch, example “git merge “branch-name””. In some cases whilst doing this we will get conflicts, this will usually happen when someone else has made changes to code whilst you have been editing it yourself. What happens in this scenario is Git needs help determining what changes it needs to apply, this is a testament to why communication in a team is vital. First you will need to identify the changes with your fellow developers, then agree what is to be kept and what needs to be discarded. Once you have agreed on what to keep you need to first need to re-stage and commit it, then you will need to enter the command “: wq”.
Finally the “fetch” command, this is a very simple concept. In short if for example we did not have all our branches from our GitHub repository on our local system we could use the command “git fetch origin” followed by the git checkout command.