# Git & GitHub Guide

_This article was originally written for a project I have been working on GitHub. Find the project repository_ [_here_](https://github.com/chrisdedman/Golang-Web-App)_._

### How to use Git and GitHub?

Here is a straightforward way to use Git and GitHub with some of the most basic, but very important commands that you, as a developer, should know. Unfortunately, schools do not teach their computer science student how to use Git or GitHub. So, here we are.&#x20;

***

### Clone a project

First of all, you will need to open your terminal, navigate to the location you want the project to be, then run the command:

```bash
git clone github_link
cd project_name_you_cloned
```

### Create a new branch

Then, once you are in the current directory of the project, you can create a new branch to start making changes. To do so, you can run the command:

```bash
git checkout -b new_feature
```

The **-b** tag means that you create a new branch. **new\_feature** is the name of the branch that you will be working on. It is good practice to give it a meaningful name related to the work you are doing. You will need to create a new branch every time you are building a new feature (every time your changes are merged into the codebase).

### Navigated and created branches

You can navigate through branches, and go back to the main branch by running the same command but without the -b tag and with the branch name main instead of your branch.

```bash
git checkout main
```

You also can create multiple branches, each new branch should be created from the main branch. You can also look up all the branches you have on your local machine by typing:

```bash
git branch
```

### Add and commit changes

Once you are done with your changes and think it is ready to be reviewed, you can stage and commit all your files.

```bash
git status # this will show you the path of your changed files
git add path_of_the_changed_file
```

It is also good practice to commit together each file that a related to the change you did. Instead of committing all files at once with a single commit message, add the related files and commit them separately from other changes. To commit the changes, once you added the changed files, type the command:

```bash
git commit
```

This will open a **vim terminal** where you can log a long message to your commit. If the changes do not require a long message, you can simply type:

```bash
git commit -m “you meaningful message here”
```

### Push changes

Once all your files are committed, you are now ready to push the changes to GitHub. If it is the first time committing, you may enter:

```bash
git push --set-upstream origin name_of_your_branch
```

If you already pushed to the remote branch, you can just type:

```bash
git push
```

From there, you can now open a pull request (PR) from the GitHub repository of the project. Once the PR is open, it will be reviewed. Then once reviewed and all the comments from the reviewer are addressed, your code will be merged into the codebase and integrated into the software.

***

### Rebase your remote branch

Now, let's say that you are working on your remote branch, building a new feature. You may be working with other people, and other people may have to commit and merge their features into the main codebase. To avoid having your remote branch outdated, there is a solution to update your own branch locally. You will just need to step back into the main branch, pull the changes from there, go back to your own branch, and merge the main branch into your remote branch. Keep in mind that you will have to add and commit your current changes before doing this. Here are the commands to run:

```bash
git checkout main              # step back to main
git pull                       # pull the changes from the main branch
git checkout my_remote_branch  # check back to your remote branch
git merge main                 # merge the main branch into your remote branch
```

Now, your remote branch is up to date with the codebase.
