Fork & Branch Git WorkFlow 🛤️
Tags: beginners, github, git, opensource
Published: 2020-11-18 08:59:52

Recently, I contributed to an open-source project **[Twindle](https://github.com/twindle-co/twindle)**, which is beginner-friendly and would like to help other beginner Devs through my learning.😇
 
Out of many, the hurdle of creating the very first **Pull Request** for a newbie can't be neglected for sure and for obvious reasons.😛 
Once successfully done, it was quite exciting for me too!! 😄 

***

To create a PR (Pull Request), I used a  **Fork & Branch Git Workflow** mentioned below:

# Prerequisites
- GitHub account
- Git installed in local machine

# Guidelines
Every other open source project has its own set of guidelines to follow before creating a PR. So, please go through the project details & make changes as per the rules.

# Terminologies
**1. Local:** It typically means "on our computer".

**2. Remote:** It is typically the code hosting service like Github, Bitbucket & others.

***

## Step 1: Fork the Repository
- Open the original repository (twindle) in GitHub.
- Click on the **Fork** in the top right corner (below your profile icon).
- In a matter of seconds, it will  be ready with your forked repository.


## Step 2: Make a Local Clone
- In your local machine, move to the directory/location you want to clone your forked repository.
- Right-click and choose `Git Bash Here` option. This will open the Git terminal.
- Type `git clone <URL of forked repository>`

>**Note:**
- "URL of forked repository" will be unique for every user. You can find it under Code > HTTPS in your GitHub account inside the forked repository.
- For this repository, default Git remote name is **origin**

**Syntax: `cd <repository-name>`** to enter the cloned repository.

- `cd twindle` in this case.

## Step 3: Adding a Remote
**Syntax: `git remote add <remote> <URL of original repository>`**
- `git remote add upstream https://github.com/twindle-co/twindle.git`

>**Note:**
Git remote name to pull changes from the original repo is **upstream** here. You can name it anything.
We will need it at a later point.

## Step 4: Working In a Branch
### 1) Create and Checkout
**Method 1**

- `git branch feature`
- `git checkout feature`

## OR

**Method 2**
(shorthand for Method 1)

**Syntax: `git checkout -b <branch-name>`**
- `git checkout -b feature`

Executing either method creates & switches to the new branch i.e feature.

### 2) Make changes to the files via any editor of your choice
Any changes made will be in the **feature** branch, not main (formerly known as the master) a.k.a root branch.

### 3) Commit those changes

- `git add .`
- `git commit -m "<message>"`

## Step 5: Pushing changes to GitHub

**Syntax: `git push <remote> <branch>`**
- `git push origin feature`

## Step 6: Opening a Pull Request
- GitHub makes it incredibly easy.
- Click on **Compare & Pull Request** button.
- Provide a brief title & description.
- Click on **Create Pull Request** to submit.
- Wait for it to be merged.

## Step 7: Cleaning Up After a Merged PR
Once the PR is merged,

- `git pull upstream main`

Pull changes from the original repo

>**Note:**
You need to switch the branch to main before deleting the feature branch (next command).

- `git checkout main`
- `git branch -d feature`

Deletes the feature branch locally

- `git push origin main`

Push changes to forked repo > main branch

- `git push origin --delete feature`

Deletes the feature branch remotely

## Step 8: Keeping your Forked Repo in Sync with the Original Repo
It's recommended to do this every time before raising a PR  / time-to-time to update your Forked Repo > main branch
- `git pull upstream main`
- `git push origin main`

***

I would like you all to contribute your bit in this fantastic project. 

💭 _Share your thoughts on how did you tackle your first PR ?_