Github-tutorial

From Grundy
Revision as of 23:46, 20 September 2017 by THeMaskedMan (talk | contribs) (→‎Pull)
Jump to navigation Jump to search

What is GitHub?

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere. This tutorial teaches you GitHub essentials like repositories, branches, commits, and Pull Requests. You’ll create your own Hello World repository and learn GitHub’s Pull Request workflow, a popular way to create and review code.


Getting started with GitHub

A REPOSITORY

A repository is usually used to organize a single project. Repositories can contain folders and files, images, videos, spreadsheets, and data sets – anything your project needs. We recommend including a README, or a file with information about your project. GitHub makes it easy to add one at the same time you create your new repository. 
To create a new repository
1. In the upper right corner, next to your avatar or identicon, click  and then select New repository.
2. Name your repository.
3. Write a short description.
4. Select Initialize this repository with a README.

Click Create repository

Adding Collaborators
You can add your team members as collaborators, so you guys can work at the same repository simultaneously.
for adding a collaborator, simply go to the repository, click on settings, and then on the collaborators tab on the left.


Fork and Clone

to make up a copy of someone else's repo in your account, you use Fork. you can fork a repository using the option given in top right.

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location, which is usually locally on your PC.

git clone <repo>

for ex :-

git clone https://github.com/tHeMaskedMan981/new-repo.git


Remote

Remote repositories are versions of your project that are hosted on the Internet or network somewhere. It is basically a link between your local repo and your online repository.
You can have several of them, each of which generally is either read-only or read/write for you.

git remote 

To see which remote servers you have configured, you can run the git remote command. It lists the short names of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin – that is the default name Git gives to the server you cloned from.

git remote -v

It lists all the remotes at once with their URLs.

Adding a remote : To add a new remote Git repository as a shortname you can reference easily, run

git remote add <shortname> <url>

Its use will be explained shortly.


Fetch

When you fetch, Git gathers any commits from the target branch that do not exist in your current branch and stores them in your local repository. However, it does not merge them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files. To integrate the commits into your master branch, you use merge. The command you use for fetching is

git fetch <remote>

For ex, we can use

git fetch upstream

if u want to fetch only a particular branch, u can use

git fetch <remote> <branch>


Pull

When you use pull, Git tries to automatically do your work for you. It is context sensitive, so Git will merge any pulled commits into the branch you are currently working in. pull automatically merges the commits without letting you review them first. the command for pulling is :

git pull <remote>

Git pull is essentially git fetch + git merge, ie., It fetches the changes and merge them automatically.

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.

git push <remote> <branch>

therefore whenever you are working on a project along with your team-mates, you must always pull the remote repository first, then create a branch and make changes, and finally push the changes. this way you can avoid possible merge-conflicts( this part is explained later).


Merge and Merge Conflicts

whenever we try to merge two branches, there is always a chance of getting a merge conflict.