As part of my Front-End Web Development Course, we were put into a team of 4 for a group project, where we were tasked to create a website using HTML and CSS. Other than developing a static, responsive website with multiple pages, one of the main goals was to put what we have learned about Git and Github in practice.
The way I understand it, Git is a version control tool that allows a team of developers to work on the same code of the project, be able to maintain the code, track all the code modifications, and let them return to a previous version if required. All of these features allow for the development of new features more easily.
Github is a Version Management Web Service and possibly the biggest source code host. There are other services that do the same thing such as Bitbucket and Gitlab. Github is based on Git, but they are not the same thing.
Putting Git into practice was a steep learning curve for me and my team. There was so much confusion as to what commands to use, what, when, or how to push. Our biggest struggle was that we were ending up with conflicts on our codes that we did not know how to resolve. In the end, my team and I were able to get a better grasp of Git and Github's basic concepts. To further help us, I created my simple, personal cheat sheet, to make sure we minimize these conflicts.
- When working in a group project (or any project), make sure that you create a branch for the work/feature that you are doing. Do not commit a change directly to master. Always create a branch that will then be merged later to the master.
To create a new branch:
- This command will make your new branch, but you will still stay on the current branch.
git branch name-you-want-for-branch
- This command will make your new branch and switch to this new branch you've created.
git checkout -b name-you-want-for-branch
I believe that it is best practice in the industry to check every morning whether there is a change on the master branch (thanks Jerico for the heads up). For those who are like me, still learning, I hope the following will be useful.
To ensure that you have the latest master available from your group project's Github repo in your local machine:
This will minimize conflicts down the line.
- Since you will be on your branch, switch from your branch to master
git checkout master
- To get the latest version of the remote master from Github
git pull origin master
- To switch back to the branch you are working on
git checkout your-branch-name
- To merge the changes from master to your branch
git merge master
Always remember to work on your branch. Do not work on the master branch.
- To stage all your changes
git add .
- This part is optional, but I like doing it. It should give a green text if the changes made were added successfully.
- To commit your changes
git commit -m "your-commit-message"
- To push to Github
git push -u origin your-branch-name
Once you're happy with your code and want to merge your branch to the master branch in Github, you can create a pull request.
Note that our Github repo was set up so that at least one reviewer was needed before you can merge your branch to the master. This can be done by:
- Going to settings, then branches
- In Branch Protection Rules, click add rule
- Type in master on the Branch name pattern
- Tick Require pull request reviews before merging and Include administrators
I hope this helps! To finish off, I will list all the git commands that I find useful, hopefully, you will too ✌🏼
- to switch from one branch to another
git checkout name-of-the-branch
- to view all existing branches
git branch -a
- to delete a branch
git branch -d branch-name
- to check the destination of where your code is going to be pushed (rarely used but handy to know)
git remote -v