DEV Community

Cover image for A beginner's guide to using Git when working with a team for the first time
Gladys Pascual
Gladys Pascual

Posted on

A beginner's guide to using Git when working with a team for the first time

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.

What is Git?

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.

What is Github?

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.

So what now?

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.

First things first

  • 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
Enter fullscreen mode Exit fullscreen mode
  • This command will make your new branch and switch to this new branch you've created.
git checkout -b name-you-want-for-branch
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • To get the latest version of the remote master from Github
git pull origin master
Enter fullscreen mode Exit fullscreen mode
  • To switch back to the branch you are working on
git checkout your-branch-name
Enter fullscreen mode Exit fullscreen mode
  • To merge the changes from master to your branch
git merge master
Enter fullscreen mode Exit fullscreen mode

Always remember to work on your branch. Do not work on the master branch.

Guide to push your local branch to Github

  • To stage all your changes
git add . 
Enter fullscreen mode Exit fullscreen mode
  • This part is optional, but I like doing it. It should give a green text if the changes made were added successfully.
git status
Enter fullscreen mode Exit fullscreen mode
  • To commit your changes
git commit -m "your-commit-message"
Enter fullscreen mode Exit fullscreen mode
  • To push to Github
git push -u origin your-branch-name
Enter fullscreen mode Exit fullscreen mode

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.

  1. Click on the branches tab on Github.
    branches github

  2. Click New pull request tab on the branch that you want to merge to master.
    Pull request

  3. Click on Create pull request button.
    create 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
Enter fullscreen mode Exit fullscreen mode
  • to view all existing branches
git branch -a
Enter fullscreen mode Exit fullscreen mode
  • to delete a branch
git branch -d branch-name
Enter fullscreen mode Exit fullscreen mode
  • to check the destination of where your code is going to be pushed (rarely used but handy to know)
git remote -v
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
annequinkenstein profile image
Anne Quinkenstein

yes, let's do it!

Collapse
 
gladyspascual profile image
Gladys Pascual

👩🏻‍💻👩🏻‍💻👩🏻‍💻

Collapse
 
alexanderrednaxela profile image
Alexander Heming

Thanks Gladys, this was very helpful !

Collapse
 
gladyspascual profile image
Gladys Pascual

That's great to hear, thanks Alex ✌🏽