Thursday, 2 July 2020

Git Commands

git       = local repository
github = online central repository

+++++++++++  Initial Setup ++++++++++++++++++++++++
env variable  :
gitdir = D:\000PortableSoftwares\GitPortable
path   = D:\000PortableSoftwares\GitPortable\bin
path   = D:\000PortableSoftwares\GitPortable\cmd



C:\Users\WELCOME>git --version  # check the version installed
C:\Users\WELCOME\PycharmProjects> create a Local_Folder and right click and select GIT BASH HERE
$ git config --global user.name ravind # set  global username
$ git config --global user.email ravindersanjay@gmail.com # set  global email id
$ git config --global user.name # retrive global username
$ git config --global user.email  # retrive global email
$ git init       # initialized empty git repository in Local_Folder and creates .git hidden folder
$ ls -lart       # shows hidden files
$ git status     #message - untracked file names


+++++++++++  Commands to move files to and from STAGING area +++++++++++++++++
$ git add test1.txt  # add one file to staging area    
$ git rm  --cached test1.txt # remove file from staging area
$ git add -A    # add all files at once to staging area
$ git add *.*    # add all files at once to staging area ( include *.* after add in command)
$ git add .    # add all files and folders at once to the staging area ( include . [DOT] after add command)
$ git add src  # add folder to staging  having name src 
$ git reset      # to undo all type of git add  commands given above to revert from staging area

+++++++++++  Commands to move files to and from COMMIT area +++++++++++++++++
$ git commit -m "this is my message for commit all staged files to local repository"

manually create a repository in github https://github.com/ravindersanjay/try.git
$ git remote add origin https://github.com/ravindersanjay/try.git  # connect local with github repo -OLD


How to get Personal Access Token :

$ git remote set-url origin https://ravindersanjay:EnterPersonalAccessTokenHere@github.com/ravindersanjay/hourly.git

$ git push -u origin master # uploads the files to GitHub repo which are already committed in local repo

$ git log #shows the list of activities done so far
$ git log  --oneline    # shows all git commits list




+++++++++++  Branch ++++++++++++++++++++++++
$ git branch feature1    #create new branch with name feature1, tip : run this when git status is up to date
$ git branch -d feature1 #delete the local branch only if it has already been pushed and merged with the remote branch
$ git branch -D feature1 #force the branch to be deleted, even if it hasn't been pushed or merged yet
$ git checkout feature1 # switch to branch 'feature1'
$ git branch     #check in which branch you are working
$ git push origin --delete <remote-branch-name>  # delete a remote branch  i.e github branch
$ git touch test2.txt # create a new file test2.txt
$ git add .
$ git push -u origin feature1 # pushes the files to github to feature1 branch


$ git checkout master
$ git merge feature1 # merge feature1 branch in master branch
$ git push -u origin master
$ git push origin master        # to push the code from local to github master branch


 +++++++++++  Tag ++++++++++++++++++++++++
$ git tag <tagname> #git tag v1.0
$ git tag # shows all tags
$ git tag -a <tagname> -m "tag for release ver 1.1" #detailed tag
$ git push origin v1.0
$ git push --tags # to push all tags at once
$ git tag -d v1.0
$ git tag --delete v1.0
$ git push origin -d v1.0 # to delete tags from remote
$ git push origin --delete v1.0

$ git tag -d v1.0 v1.1                 # delete multiple tags from local
$ git push origin -d v1.0 v1.1 # delete multiple tags from remote

$ git checkout -b <branch name> <tagname>    #git checkout -b ReleaseVer1 ver1.0
$ git tag <tag name> < reference of commit > # git tag v1.2 5fcdbd03

+++++++++ revert the mistake +++++++++++++++++++++++++++  
# accidently change the file in staging 
$git status # changes not staged for commit modified : contact.html  

# this command recovers the file to match the last commit
$git checkout "contact.html"
$git checkout -f  # to revert all modified files to unmodified 

# reverting from Github ( run below three commands in continuation) 
$ git log                                   # read the commit id 
$ git revert commitID # commitID is hashcode
$ git push # revert and push commands should be executed together 
  

+++++++++ ++++ Git Stash +++++++++++++++++++++++
$git stash -m "added something in the code"

$git stash list

$git stash apply stash@{0}

$git stash drop stash@{2}   ==> deletes a perticular stash

$ git stash clear  ==> deletes all the stash

$git stash pop  ==> deletes the latest stash


+++++++++ extra commands starts+++++++++++++++++++++++++++
$ clear 
$ ls
$ git help

$ code .  # opens    the visual studio 



+++++++++ extra commands ends +++++++++++++++++++++++++++ 

make change in file
#git diff  # shows difference between working directory and staging area
#git diff  --staged    # compares staging area with last commit


$git commit -a -m 'skipped staging area and fixed"


$git rm waste.html    # remove the file 
$ git status   -s          #


$ touch .gitignore  #create gitignore file in your working git folder
$ git rm --cached logs/inner.log      # stops tracking the inner.log file


connect local repository to central repository
git remote add origin "https://github.com/ravindersanjay/SampleProject.git"
git pull origin master 
 

steps to merging first branch with master branch
git checkout master
git merge firstbranch

rebasing steps( only master branch is maintained) 
git checkout master
git merge firstbranch

to pull the code from local to github
git pull origin master

git clone https://github.com/ravindersanjay/SampleProject/

# connect local and github
$git remote add origin https://github.com/ravindersanjay/HelloWord.git
$ git remote # message - origion
$ git remote # message - origin https://github.com/ravindersanjay/HelloWord.git (fatch) and (push)

resetting the changes   
git checkout 8digitofhashcode filename.txt
git log

press q to quit

-----------------------------
git remote set-url origin https://github.com/username/repository.git # set the new repository URL
git help     >>> shows help options
git init       >>> setup git in a folder
git status   >>> shows status
git add .    >>>  add all new files
git status
git reset    >>> undo add command
git commit -m "added hello word program to git"                                      >>> commit files to local git
git remote add origin https://github.com/ravindersanjay/HelloWord.git    >>> upload files to github at given location
git push -u origin master         >>> ??
git pull  -u origin master         >>> get latest code from github ??

GIT Commands: https://www.siteground.com/tutorials/git/commands/




++++++++++++++++++++++++++++++++++++++
Setup Git , GitHub with eclipse

EGIT comes installed with Eclipse. 


To generate Access Token
While pushing to github you will NOW need to provide ACCESS TOKEN in place of password
1. Login to your GitHub account
2. Verify your email address, if it hasn't been verified yet.
3. In the upper-right corner of any page, click your profile photo, then click Settings.
4. In the left sidebar, click Developer settings.
5. In the left sidebar, click Personal access tokens.
6. Click Generate new token.
7. Give your token a descriptive name.
8. To give your token an expiration, select the Expiration drop-down menu, then click a default or use the calendar picker
9. Select the scopes, or permissions, you'd like to grant this token. To use your token to access repositories from the command line, select repo.
10. Click Generate token

Eclipse Configuration with Git and GitHub:

Step 1 : Login to GitHub account 
Step 2 : Start a Project = Create a repository
Step 3 : Start Eclipse
Step 4 : Goto Perspective - Git Repositories and click on Add Git Repo
Step 5 : Create a project in Eclipse
Step 6 : Do a right click on Project - Team - Share - Add to git repo --> Select GitHub repository URI
Step 7 : Commit and Push the project to the repo. Enter username and access token to login. 
Step 8 : Commit and Push every change to the repo


Open Git Terminal in Eclipse : 
Eclipse --> Right click  on project --> show in Local Terminal --> Git Bash

No comments:

Post a Comment