A beginner's guide to using Git when working with a team for the first time 
Tags: git, github, beginners, webdev
Published: 2020-08-05 10:02:48

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.
```bash
git branch name-you-want-for-branch
``` 

- This command will make your new branch **and** switch to this new branch you've created.
```bash
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](https://dev.to/jericopingul) 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
```bash
git checkout master
```

- To get the latest version of the remote master from Github
```bash
git pull origin master
```

- To switch back to the branch you are working on
```bash
git checkout your-branch-name
```

- To merge the changes from master to your branch
```bash
git merge master
```

**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
```bash
git add . 
```

- This part is optional, but I like doing it. It should give a green text if the changes made were added successfully.
```bash
git status
```

- To commit your changes
```bash
git commit -m "your-commit-message"
```

- To push to Github
```bash
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*.
1. Click on the branches tab on Github.
![branches github](https://dev-to-uploads.s3.amazonaws.com/i/rgxczhdmbp4yht776gxb.png)

2. Click **New pull request** tab on the branch that you want to merge to master. 
![Pull request](https://dev-to-uploads.s3.amazonaws.com/i/t42sv0hspi7mj04f0mgn.png)

3. Click on *Create pull request* button.
![create pull request](https://dev-to-uploads.s3.amazonaws.com/i/ny85vfkyzlkt3mnxdj3s.png)

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*
```bash
git checkout name-of-the-branch
```

- *to view all existing branches*
```bash
git branch -a
```

- *to delete a branch*
```bash
git branch -d branch-name
```

- *to check the destination of where your code is going to be pushed* (rarely used but handy to know)
```bash
git remote -v
```