Thursday, August 29, 2024

Git Basic Commands

 To Install latest Git software https://git-scm.com/downloads After installing for the 1st time, we need to define/configure user name and user email Syn: git config –global user.name “username” Syn: git config –global user.email “email id”

Basic Operations:

  1. Clone the project if existing project else ignore this step
  2. Initialize
  3. Add
  4. Commit
  5. Push
  6. Pull

Advanced Operations:

  1. Branching
  2. Merging
  3. Rebasing

  1. Create new repository.
  2. To create the connection between local to server a. Git Init b. Git add c. Git commit image

**Git remote ** Used to connect between local and server Syn: git remote add origin For the first time we need to setup the upstream branch Syn: git push –set -upstream origin master Will set the upstream branch as master branch in remote.

Git Clone: To clone the code to the local Or to download the project from remote to local. Syn: git clone

Git Status: To know the changes between the local and server. Syn: git status

Git restore: Will restore the local changes to last pulled state. Syn: git restore

Git add: This cmd is used to add local changes to stage. Syn: Git add . (to add all changes to stage)

Git Status: To know the changes between the local and server. Syn: git restore –stage command used to unstage the changes.

Git Commit: To commit the changes Syn: git commit “message/comments”

Git Status: Syn: git status will return the current state of the files are committed to the respective branch.

Git Push: Syn: git push to push the changes. Note: If authentication is not happened then system will ask for authentication, give token or login into the git Now code will move to the branch.

Git Checkout: Used to change the branch Syn: git checkout <branch_name>

Git branch: Will retrieve the current branch name Syn: git branch

Branching Strategy:

We will create branch as per the feature level and after review merge/pull to the develop branch. 
Then, final review will happen and if no conflicts the code will be merged with master branch.

Note: master branch will be used to run the automation scripts. Note: master branch will be protected, we can changes properties in git.

image

Git Conflict: If multiple users are working on common files, while pulling the code before pushing the changes git will raise conflict. While pulling the changes git will show error as conflicts Option1: Right click on project  Git  resolve conflicts  compare the changes and do necessary changes OR OR OR We can do using stash concepts

Best Practices:

  1. Branching Strategy
  2. Follow Branching Convention
  3. Discard the changes if not needed (use git restore command to revert the changes)
  4. Track the differences in the files (git diff to see the differences or Git Menu  Commit  select the file and visually we can see the changes)
  5. Pull the changes from remote before pushing the latest changes.
  6. Push the changes to remote regularly.
  7. Stash the changes if needed (to store the changes temp ). a. “Git stash list” to get the list of stashed items b. “Git stash apply” to apply all changes c. “Git stash apply ” to apply the respective change

Stash vs Commit:

Stash: Stash is to store the changes temporarily. Syn: git stash Syn: git stash Syn: git stash list (will retrieve all the stashed records) Syn: git stash clear (to clear all the stashed records) Syn: git stash apply (to revoke the stashed records)

Commit: Commit is to store the changes permanently. Syn: git commit Syn: git commit

How to undo committed/ un-committed changes:

Committed: Already committed to server

Un-committed: changes are not committed to server. How to UNDO un-committed changes

  1. Git restore
  2. Git restore-staged
  3. Git checkout

Git Restore: Used to remove the changes which are not committed Syn: git restore Syn: git restore .

Git restore-staged: Used to revert the staged changes Syn: git restore –staged After this use command Syn: git restore .

Git Checkout Used to checkout the branches, using this we can revert the changes Syn: git checkout

How to Un-Do Committed changes:

We can achieve this by using the below commands. Using Git Reset  Git reset –soft HEAD~  Git reset –Hard

Git Log: To track all the committed changes Syn: git log (will retrieve all the committed logs ) Syn: git log –oneline (will retrieve the details in single line and it is readable)

Git reset –soft: Used to revert the local commits, but it will keep the changes in stage. After this use git restore to remove changes permanently. Syn: Git reset –soft HEAD~ (latest commit) Syn: Git reset –soft HEAD~2 (will revert the last two commits)

Git reset –Hard: Used to revert the local commits, but it won’t give any option to do or see the changes. It will restore as well. Syn: Git reset –Hard

How to remove the remote changes: -- using git Revert we can achieve this

Syn: git revert <commit id> and we need to quit by adding comments
Syn: git revert <commit id> --no-edit (we can do without adding any comments)

Merge Vs Rebase:

Merge: Preserves the history and log All commits are combined into a single commit Usage: when target branch is public then we use Merge Rebase: Rewrites the history and create a linear log All commits are rebased and same number of new commits are added. Usage: when target branch is private then we use rebase.

image image

Git Pull Vs git fetch:

The key difference between git fetch and pull is that git pull copies changes from a remote repository directly into your working directory, while git fetch does not. The git fetch command only copies changes into your local Git repo.

Git Fetch:

Used to fetch all changes from the remote repository to the local repository without merging into the current working directory Repository data is updated in the .git directory Review of commits and changes can be done No possibility of merge conflicts. Command for Git fetch is git fetch

Git Pull: Brings the copy of all the changes from a remote repository and merges them into the current working directory The working directory is updated directly Updates the changes to the local repository immediately. Merge conflicts are possible if the remote and the local repositories have done changes at the same place. Command for Git Pull is git pull

jsonPath Learnings

JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ regardless if it is an object or array.

JsonPath expressions can use the dot–notation 

JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ regardless if it is an object or array.

JsonPath expressions can use the dot–notation

$.store.book[0].title

or the bracket–notation

$['store']['book'][0]['title']

Operators

OperatorDescription
$The root element to query. This starts all path expressions.
@The current node being processed by a filter predicate.
*Wildcard. Available anywhere a name or numeric are required.
..Deep scan. Available anywhere a name is required.
.<name>Dot-notated child
['<name>' (, '<name>')]Bracket-notated child or children
[<number> (, <number>)]Array index or indexes
[start:end]Array slice operator
[?(<expression>)]Filter expression. Expression must evaluate to a boolean value.

Functions

Functions can be invoked at the tail end of a path - the input to a function is the output of the path expression. The function output is dictated by the function itself.

FunctionDescriptionOutput type
min()Provides the min value of an array of numbersDouble
max()Provides the max value of an array of numbersDouble
avg()Provides the average value of an array of numbersDouble
stddev()Provides the standard deviation value of an array of numbersDouble
length()Provides the length of an arrayInteger
sum()Provides the sum value of an array of numbersDouble
keys()Provides the property keys (An alternative for terminal tilde ~)Set<E>
concat(X)Provides a concatinated version of the path output with a new itemlike input
append(X)add an item to the json path output arraylike input
first()Provides the first item of an arrayDepends on the array
last()Provides the last item of an arrayDepends on the array
index(X)Provides the item of an array of index: X, if the X is negative, take from backwardsDepends on the array

Filter Operators

Filters are logical expressions used to filter arrays. A typical filter would be [?(@.age > 18)] where @ represents the current item being processed. More complex filters can be created with logical operators && and ||. String literals must be enclosed by single or double quotes ([?(@.color == 'blue')] or [?(@.color == "blue")]).

OperatorDescription
==left is equal to right (note that 1 is not equal to '1')
!=left is not equal to right
<left is less than right
<=left is less or equal to right
>left is greater than right
>=left is greater than or equal to right
=~left matches regular expression [?(@.name =~ /foo.*?/i)]
inleft exists in right [?(@.size in ['S', 'M'])]
ninleft does not exists in right
subsetofleft is a subset of right [?(@.sizes subsetof ['S', 'M', 'L'])]
anyofleft has an intersection with right [?(@.sizes anyof ['M', 'L'])]
noneofleft has no intersection with right [?(@.sizes noneof ['M', 'L'])]
sizesize of left (array or string) should match right
emptyleft (array or string) should be empty

Path Examples

Given the json

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

JsonPathResult
$.store.book[*].authorThe authors of all books
$..authorAll authors
$.store.*All things, both books and bicycles
$.store..priceThe price of everything
$..book[2]The third book
$..book[-2]The second to last book
$..book[0,1]The first two books
$..book[:2]All books from index 0 (inclusive) until index 2 (exclusive)
$..book[1:2]All books from index 1 (inclusive) until index 2 (exclusive)
$..book[-2:]Last two books
$..book[2:]All books from index 2 (inclusive) to last
$..book[?(@.isbn)]All books with an ISBN number
$.store.book[?(@.price < 10)]All books in store cheaper than 10
$..book[?(@.price <= $['expensive'])]All books in store that are not "expensive"
$..book[?(@.author =~ /.*REES/i)]All books matching regex (ignore case)
$..*Give me every thing
$..book.length()The number of books