Getting started with Git

To get started with Git, download the latest version of Github for Windows.

Search for Git.exe in your system and find its containing folder path. Add this Git path to Windows Environment variables.

Path=C:\Users\User\AppData\Local\GitHub\PortableGit_ed44d00daa128db527396557813e7b68709ed0e2\bin

Setting path

###Add a Local Repository to Github with Command Line:

Create a new empty repository in Github. This is where the local repository is pushed.

In the Command Prompt, change the working directory to your local repository.
Initialize it as a Git Repository, with git init. Use git add . to add all file contents to the staging area.

git command

Run git status to view the status of the files in the working directory and staging area. We can use git diff to know which files have changed since the last commit.

git command

Commit the staged files in the local directory.
Use git commit -m "commit message".

git command

Copy the URL of Github Repository we created in the first step.
Run git remote add origin <Remote URL> to set our remote repository.
git remote -v to verify the remote URL.

git command

Push the Local Repository changes to Github Remote directory and specify the branch. Anything in master is always deployable.
Use git push origin master

git command

At this point fill in the Github Account Credentials, and we have successfully added a local directory to remote on Github.

git command

###Working with Branches: Create a new branch and switch to the new branch using,
git checkout -b new-branch.

Use git branch to know which branch we are currently in, it is starred and displayed in green.

git command

Push the new branch to remote repository, git push origin new-branch

Give the Github account credentials to authenticate.

git command

git command

We can find that our new branch is pushed to remote succesfully.

git command

Switch between the branches using, git checkout branchname.

git command

To delete a branch, switch out of the branch you want to delete.
Delete a Local branch with, git branch -d branchname.

git command

We can delete a Remote branch using,
git push origin --delete branchname as of Git v1.7.0
or use git push origin :branchname as of Git v1.5.0

###Fetching a Remote Repository:

clone is used to download a copy of a project to use the code, or to collaborate with someone on a project.

Run git clone <URL of the remote repository> with the URL of the project we want to copy.

git command

fetch retreives any new work and updates made to the remote repository, by collaborators. Use git fetch origin master.

git command

merge combines our local changes and changes made by the collaborators. Use git merge origin master.

git command

pull is a shortcut for completing both git fetch and merge in a single command. It grabs the updates and merges them with local work. Use git pull origin master.

git command

Git commands used above for quick reference

// Add a Local Repository to Github with Command Line:
> git --version
# To find git version

> git init
# To Initialize the local directory as a Git Repository

> git add .
# To add all file contents to the staging area

> git status
# To view the status of the files in the working directory and staging area

> git commit -m "commit message"
# To Commit the staged files in the local directory

> git remote add origin <Remote URL>
# Sets the new remote

> git remote -v
# Verifies the new remote URL

> git push origin master
# Push changes to remote repository

> git checkout -b new-branch
# Creates a new branch and switch to the new branch

> git push origin new-branch
# Push the new branch to remote repository

> git checkout branchname
# Switch between the branches

> git branch -d branchname
# To delete local branch 

> git push origin --delete branchname
# To delete a Remote branch as of Git v1.7.0

> git push origin :branchname
# To delete a Remote branch as of Git v1.5.0


// Fetching a Remote Repository:
> git clone <URL of the remote repository>
# Clones a repository to your computer

git fetch origin master
# Fetches updates made to a remote repository

git merge origin master
# Merges updates made online with your local work

git pull origin master
# pull is a shortcut for completing both git fetch and merge in a single command. It grabs the updates and merges them with local work.