Before going into what Git and Github is, lets go over version control systems and it's appliction to the tech space. VCS is short for Version control system, it is a Version management system that tracks changes in files along with the order in which the changes to these files were made.
Git is an example of a Version control system (VCS), Github is different from Git, but since they are used side by side it is often confused for Git. Github is more like a shared hosting platform for all repositories and applies Git for effective collaboration within organizations and developers. One of the features Github provides is forking. Forking is basically creating a copy of the main repo on your user account so experimental changes can be made without affecting the main product.
With collaboration being one of the major strengths of Git and Github, it also allows for synchronous development within teams of developers or organizations.
Below is a visual representation of Github workflow, references will be made to this diagram as we continue.
Assuming you want to start working on a project
- Fork: A fork is basically a remote copy of the original repo. To make a fork of the original repo,
- Navigate to the top right corner of the repository and click "Fork".
- Select the destination of the Fork, you'll most likely choose your Github profile. In this case we'll refer to the main repository as upstream and the forked repos on you profile as origin.
Clone: The next step would be making a copy of the remote repository and sending it to your local machine. To do this, simply Open your terminal and navigate to the directory of your choice on your working machine then run
git clone <url to remote repository. Here we're cloning the forked repo.
Now you're ready to start working and editing file, before doing anything, you'll have to create a new branch and this will be your development branch. To do this, run:
git checkout master
git checkout -b name_of_new_branch
If some changes has been made to the original repository maybe by other devs, you can update your local repo by running the fetch command.
git fetch upstream
git merge upstream/masterTo merge changes with master branch.
This will make sure you have the latest version of the repo.
If changes were found you'll have to sync your remote forked repo to keep it upto date, run
git push origin branchname.
Pull Request: This is done when changes from the local repo are synced with the forked repo and they are ready to be pushed to the main repository. These changes are not fully merged to the main repo, they'll have to go through review by the admin before the merge request will be accepted.
A branch is used to create a copy of a repository at a particular time, it stores the state of the repo at that point in time and forms an independent line of development, i.e changes made to the repository while in this branch won't affect the state of the main project.
Types of Branches
Branches can be classified based on their use, some examples of these branches are namely:
- The master branch
- The dev branch
- The feature branch
- The hotfix branch
These branches have unique applications in collaborating with git.
The Master branch: The master branch is the main working line, like the main state of the project. It has to be with as little errors as possible this is why it isn't used as the main working branch.
The Dev/Develop branch: It is normally used as a kind of staging environment or testing environment where new features will be developed and tested before being merged to the master branch to effect the changes of the new code.
The Features branch: Most work in projects may have several features to be rolled out at different point in time, development of these features usually start from the master branch so it'll carry along the code history of the whole project and start from the most recent release state of the application. After development of each feature, it is then branched out and then merged with the Dev branch, it has no direct access to the master branch. There can only be one master and dev branch, but git allows for multiple features branch.
The Hotfix branch: This branch is used in cases where the application has a bug and needs to be rectified urgently, here a branch is created from the master branch, then work is done to fix the bug before being merged with the Dev branch.
Common branch operations
It's good to understand that branches are just git pointers to commits in time, creating a branch doesn't change the repository
Creating a branch
To create a branch named feature1 use
git branch feature1
This only creates a new branch feature1 any commit made now won't be saved on this branch because we're still on the master branch, to switch to the new branch, we use
git checkout feature1
Now any changes or commits made to the code will be saved in the new branch.
Deleting a branch
Once you've finished working on a branch, you can merge it to the master or dev branch before deleting it, deleting the branch won't affect the repositories history except the changes made in the branch has not been merged.
To delete the feature1 branch, use
git branch -D feature1
Note that the previous command will only delete the copy of the feature1 branch on the local machine, another copy will still be on the remote repository, to delete the feature1 branch from the remote repository, run
git push origin --delete feature1
Pull a remote branch
To pull a branch from the remote repo use git pull against the origin and specify the branch name.
git pull origin <branch name>
git branch to check the list of available branches
git checkout <branch name> to switch the pointer to the new branch
To merge branches we use the
git merge command, but before doing that, you have to switch to the branch you want to merge to example master branch.
git checkout master to switch to the master branch
git merge feature1 to merge the feature1 branch to master
This is only valid for a local repository, to merge with a remote branch, we use
git push --set-upstream origin <branch name>
Top comments (0)