Getting started with Git & Github
Tags: git, github, beginners, tutorial
Published: 2020-11-25 02:57:03

Hey everyone, here is the beginners guide to Git and GitHub hope you will take something out of it.

**GitHub repository** a.k.a repo is something that is hoisted on the GitHub website, which contains all of your files its also called as a remote. 

A GitHub repo contains something like **branches**. These branches are derived from the boss branch called main branch. The main branch has children branches which are derived from it. Once a child branch is created from the main branch, any changes made to the child branch will not affect the parent main branch. Lots and lots of branches are created when a project is being developed. Any project on the open-source literally has a ton of branches in it. 
 
**Branches** are created when a feature is being developed or a bug is being fixed or any change that is supposed to be made.  As soon as the change is done, the child branch is merged with the parent- main branch. 
**Issues ‼** are something like a change that is proposed or expected. They are something that needs your attention and time to work on. Issues are created when, bugs are encountered, feature is supposed to be added and many more things. Commits are the changes you make.

Coming to Pull requests 🤔no, we are not getting into that right now but, I promise we will come back to that.

#### HAVING YOUR OWN COPY OF THE REPO BY FORKING IT. 

1.	Now you need to switch to GitHub website to do this & hopefully you are logged in,
2.	Go to the repo you want to fork, and click on fork you can find that at top right corner I guess,
3.	Now you go to your GitHub profile, and click on repositories and you can see the forked repositories there.
MAKING A LOCAL COPY OF YOUR REPO… 👩‍💻 called as CLONE. 
If you are collaborating in a project then it’s a good idea to clone a forked 🍴 repo.

Side note: I hope you all are using VS code and git is installed in your PC or else you can use powershell /terminal too.
1.	Open VS Code ,
2.	Go to Terminal at the navbar and click on New Terminal or click Ctrl + `

We must tell git who we are so, 

`$ git config --global user.name "YOURNAME"`
`$ git config --global user.email YOUREMAIL`

3.	Now type    `$ cd ‘LOCATION YOU WANT TO CLONE THE REPO IN’` and press enter 

4.	Now, go to GitHub repository on the GitHub website, and click on clone or download and copy the URL.   
5.	Now type   `$ git clone THEURLYOUCOPIED` and press enter.
6.	Voila, you’ve successfully cloned the repo into your desired location.

   

so, now you can see the files of your cloned repo in the explorer tab of the VS code.

#### PUSH & PULL  .  
**Push** - pushing sends the recent commit history from your local repository up to GitHub. If you're the only one working on a repository, pushing is fairly simple. If there are others accessing the repository, you may need to pull before you can push.
**Pull** - a pull grabs any changes from the GitHub repository(remote) and merges them into your local repository.
**Commit** - committing is the process which records changes in the repository. Think of it as a snapshot of the current status of the project. Commits are done locally.
First things first, I want y ’all to promise me that you will pull(or fetch)  before you push or make any commits!!!
Just to make sure your local clone is updated with the remote repo.

So, how to pull ⬅? 
In the terminal of VSC hope you are in the cloned directory; type   `$ git pull REMOTENAME BRANCHNAME` 
and press enter that’s it!

Now coming to how to push ➡? btw why we push?
Suppose you make changes to the cloned repo in your local machine, you want those changes to reflect to the remote repo too, so you push.
Let’s assume that I’ve made some changes to STANDARDS.md file in my local directory assuming that I am in my cloned repo location, here are the steps I follow for pushing it into the remote repo –
1.	Opening my terminal in VS code,
2.	Using the command `$ git init` to initialize the files,
3.	Using  `$ git status`  which shows a bunch of the changes which are not added and it’s asking me to add those,
4.	Now I type `$ git add ./STANDARDS.md` to add the changes I’ve made to the staging area,

5.	Committing these changes by typing
`$ git commit -m ‘MY DESCRIPTION TO THE CHANGES I’VE MADE’`.

6.	Pushing these changes to a branch of desired repo by typing- 
`$ git push REMOTENAME BRANCHNAME`
7.	You’ve successfully pushed the changes you’ve made.

#### WTF is REMOTE???? 🙄

**REMOTES** 

Here’s something I found on Stack overflow-
As you probably know, git is a distributed version control system. Most operations are done locally. To communicate with the outside world, git uses what are called remotes. These are repositories other than the one on your local disk which you can push your changes into (so that other people can see them) or pull from (so that you can get others changes). 
Don’t worry, it will make more sense later.
ADDING REMOTES …
1.	Before adding let’s view our remotes by typing   `$ git remote -v`   in the VS Code terminal.
2.	You are able to see the HTTP links from where you cloned the repo.
3.	To add remotes, you type  `$ git remote add THENAMEYOUKEEP URLOFTHEREMOTE` and obviously press enter after each command.
4.	Now view the remotes by typing the same command from the step 1.
5.	Voila you’ve added the remote.

#### SETTING UP A REMOTE UPSTREAM

So, You’ve cloned the forked repo to your local machine, now you must set a **God Father**  repo. GodFather repo or upstream is the repo that you forked from.
Remote Upstream, is the remote that should be set if you want to avoid merge conflicts 😵. 
Now you already know how to setup REMOTES right?
1.	Type  `$ git remote add upstream URLOFTHEPARENTREPO` that’s it. 
2.	Now, type  `$ git remote -v` to view your remotes and hopefully you can see your upstream there.
Before you start editing/working on your cloned repo, you must first pull from the upstream. 
How to do that? Simple, type `git pull upstream BRANCHNAME` 

#### BRANCHES? 
You can see the branches in the branch’s dropdown menu located at the left corner of the GitHub repo.
You need to understand this workflow before,
 
This thing happens in collaboration.
You can view all the branches by typing  `$ git branches` in your terminal of VSC. The branch denoted with * asterisk denotes the current branch you are working on.
To switch to a particular branch, you can type   `$ git checkout BRANCHNAME`

#### GIT FETCH?

The **git fetch** command downloads commits, files, and refs from a remote repository into your local repo. When downloading content from a remote repo, git pull and git fetch commands are available to accomplish the task. You can consider git fetch the 'safe' version of the two commands.

git fetch basically makes another branch of the content you have fetched which you can merge later with a branch you like.
You can use the git fetch command if you have a remote set up from which you fetch data.

Command is simple-
`$ git fetch REMOTENAME BRANCHNAME`
REMOTENAME- the remote’s name you want to fetch from.
BRANCHNAME –the branch of the remote you want to fetch from.

later you can preview the fetched branch and merge the two branches by using-  
`$ git merge REMOTENAME/BRANCHNAME`
**REMOTENAME**- the remote’s name you want to merge the fetched remote to.
**BRANCHNAME** –the branch of the remote you want to merge the fetched branch to.

#### PULL REQUESTS
Now you have committed your every edit to your forked repo, you want to show the maintainers(one with write access) the changes you’ve made and you want them to make these changes to the original Godfather  repository too right?
That’s when pull requests come into play.
Now, how to do that? Simple,
Go to your forked repository on the GitHub website, choose the branch that contains your commits, click on the Pull Requests tab at the top, then click on Create Pull Request.
Look carefully at the branches and repos you want to make PR from and to. On the right you must have the branch & repo you have made the changes to by pushing from your local and on the left you have the so-called Godfather repository that you want to make Pull request  to.

 



I hope you are clear with most of the basics of Git and GitHub.

For more info-
•	 GitHub docs- https://docs.github.com/en/
•	 Git docs- https://git-scm.com/doc/



PS : While typing the commands ignore the `$` symbol and type the rest of it.




> “The more I learn, the more I realize how much I don't know.”
 *Albert Einstein*