Git Tutorial

Git Hub Tutorial

Git Hub Site

https://github.com

Download Git

https://git-scm.com/downloads

Setup a git account in local computer

Download and install git from site https://git-scm.com/downloads

Kindly create a git hub account and a git repository. Below is a sample a/c and repository

Git A/c          : gitaccount
Git Repository   : git@github.com:gitaccount/gitrepo.git

communication protocols

1. https 2. ssh

Create a folder .ssh under C:\Users\Admin

C:\Users\Admin\.ssh

Create a new SSH Key ( Private and Public key pair)

Go to the folder C:\Users\Admin\.ssh, then issue below command

ssh-keygen -t rsa -C "your_email@example.com"
//above command will ask for the name of file and passkey, you can ignore the passkey.
// And it will create two files 
//user1      => private key file
//user1.pub  => public  key file 
now extract the content of user1.pub and update it in git site
open git web site, then under settings/SSH and GPG keys add a new SSH key and update the key
and you can give any title

Create config file

Create a file namely “config” under the .ssh folder and enter below lines

# User1
HOST github.com-[user1]
HOSTNAME github.com
USER git
IdentityFile  ~/.ssh/user1
# Joe BlueTree
HOST github.com-bluetree
HOSTNAME github.com
USER git
IdentityFile  ~/.ssh/joebluetree_rsa

# Divit
HOST github.com-divit
HOSTNAME github.com
USER git
IdentityFile  ~/.ssh/divit_rsa

Identifying multiple git accounts

you can give an identifier at the end of the host by entering a hyphen and an identifier .the same will be used while refereing the git url

Multiple git account settings

HOST github.com-[user1] 
git@github.com-[user1]:gitaccount/gitrepo.git

Clone Git Repository

goto a local folder and enter below command to clone a git repository

git clone git@github.com-[user1]:gitaccount/gitrepo.git
// use dos command to goto the clonned folder and set the default user name and email

init git repo manually

git init
git remote add origin url // add a remote repo
git --version

Set default name and email

Use below commands to set default user name and email id, which can be set globally or ocal folder only

git config -l
git config user.name "YOUR_USER_NAME"
git config user.email "YOUR_EMAIL"

git config --global user.name "YOUR_USER_NAME"
git config --global user.email "YOUR_EMAIL"

change https url to ssh / ssh to https url

git remote -v // will show the current remote url
git remote set-url origin git@github.com-[user1]:gitaccount/gitrepo.git 
git remote set-url origin https://github.com/gitaccount/gitrepo.git

Git Commands

// add all files or single file to repository
git add .
git add filename
// Commit files to repository
git commit -am "save"
// push the changes to repository
git push
// pull changes from repository to local computer
git pull

restore a file from staging area

git restore --staged <filename>

restore a file from commit area

git restore filename

restore to previous version

git log --oneline
git checkout  id
git checkout branch -> reset to latest head

Git Branching and merging

Branching is used to create a working folder (branch) where you can do changes and later when completed it can be merged to the original master branch. Below is a set of commands to create a branch namely hotfix then it is merged to master branch

// Create a branch hotfix
git branch   hotfix
// move to gotfix branch
git checkout hotfix
// move back to master branch
git checkout master
// merge hotfix branch to master branch
git merge    hotfix
// remove hotfix branch
git branch -d hotfix

Git remove remote repo reference from local repo

git branch -all
git fetch origin --prune
git fetch --prune

Git merging branch

fast forward merge
branch commit merge with no conflict
branch commit merge with a conflict

1 thought on “Git Hub Tutorial”

  1. Pingback: What Is Angular - Tech Mentor

Leave a Comment

Your email address will not be published. Required fields are marked *