Monday, May 30, 2022

Git Merge Rebase and Cherry Pick - deep dive

Hello guys 🙂 this post I would like to share with you about the "git merge" experiments.

We have multiple code lines in the project, once the developer completes his task branch is ready to move to the next branch for testing. 

Git Merge and git rebase



There are multiple ways to combine the code lines. If the HEAD pointed to the tip of the branch then the best way to merge from the other branches into the current branch. 

  • FF fast forward merge 
  • 3 Way merge


You will be on the target-branch (most of the time it will be main/master or release or production) and then you will run the following command with a source-branch.
git merge [branch-name]
The following example for the Git Merge 'ort' Strategy, let's begin
clear
mkdir merge_test; cd merge_test; git init 
# on main branch two commits
touch test1 ; git add . ; git commit -m "test1"
touch test2 ; git add . ; git commit -m "test2"
# branch out feature and do 2 commits 
git checkout -b feature
touch test3 ; git add . ; git commit -m "test3"
touch test4 ; git add . ; git commit -m "test4"
# switch back to main branch 2 commits
git switch main
touch test5 ; git add . ; git commit -m "test5"
touch test6 ; git add . ; git commit -m "test6"
graph
#confirm which branch 
git branch

# merge the feature branch to main 
git merge feature


 After merge
Git Merge 'ort" Strategy

The git merge execution example is given below:
# clone a project repo from GitHub/BitBucket
# Create branch a-branch do a commit
git checkout -b a-branch 
touch a ; git add ; git commit -m"a changes"
git status -s
# Get the main branch on top it create b-branch 
git switch main 
git checkout -b b-branch
# b changes commit
touch b ; git add ; git commit -m"b changes"
git status -b # assuming that you are on the b-branch
# get all the changes in a-branch get into the b-branch 
# HEAD pointing to b-branch 
git merge a-branch 
git log --oneline --graph -all
You can also clearly mention on the that what is source and target branch as shown below:
git merge [source branch] [target branch]

Merging a branch newbranch into a target branch as master.
git branch
git merge newbranch master
git log --oneline --graph -all
There is one more thing here "git rebase". Let's deep dive into rebase experiment now.

As per my understanding rebase will be used when we have few changes on a feature branch from there you worked on some more changes in a latest branch and those changes you wish to include into the master then rebase is best choice and when you merge the third level branch to master it will only considers the latest branch commits and it does not consider it's parent commits during master branch merge which is actual fast forward. Here is my understanding 

Before git rebase
Now let's do the rebase from the feature2 branch to master branch as shown below

Example of feature2 branch rebase to master from feature1 branch

The rebase and fast-forward merge execution sample is given below:

Example of rebase here rebased to from feature2 to master, that means excluding feature1

After the rebase of a branch it will be fast-forward merge only.
Git fast-forward merge automatically after rebase


Fast-forward merge experiment

We can use the --ff and --ff-only options when doing the merging a branch. here is the each one option meaning from the git help

--ff
           When the merge resolves as a fast-forward, only update the branch pointer, without creating a
           merge commit. This is the default behavior.


       --ff-only
           Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or
           the merge can be resolved as a fast-forward.

git merge fast forward



 master) B
c313a34 A

scmuser@localhost ~/merging (master)
$ git checkout -b "feature"
Switched to a new branch 'feature'

scmuser@localhost ~/merging (feature)
$ touch C; git add .; git commit -m"C"
[feature 76e64d8] C
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 C

scmuser@localhost ~/merging (feature)
$ touch D; git add .; git commit -m"D"
[feature 2c421a7] D
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 D

scmuser@localhost ~/merging (feature)
$ git log --oneline --graph --all
* 2c421a7 (HEAD -> feature) D
* 76e64d8 C
* 2548379 (master) B
* c313a34 A

scmuser@localhost ~/merging (feature)
$ git switch master
Switched to branch 'master'

scmuser@localhost ~/merging (master)
$ git status -b -s
## master

scmuser@localhost ~/merging (master)
$ git merge feature
Updating 2548379..2c421a7
Fast-forward
 C | 0
 D | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 C
 create mode 100644 D

scmuser@localhost ~/merging (master)
$ git branch -d feature
Deleted branch feature (was 2c421a7).

scmuser@localhost ~/merging (master)
$ git log --oneline --graph --all
* 2c421a7 (HEAD -> master) D
* 76e64d8 C
* 2548379 B
* c313a34 A

scmuser@localhost ~/merging (master)
$ ls
A  B  C  D 

What is Merge Conflicts?

If two collaborators use the same file for editing on the code lines. When it's pushed to the remote repository there is a chance to conflict.

  mkdir mergeconflict; cd mergeconflict
git init
vi hello.py 
# Entered line as -  print("Hello World!")

git add .; git commit -m"Initial commit"

# takeout a new branch from master
git checkout -b vastav

# Edit the file replaced with  - print("Hello Vastav!")
vi hello.py 
git add .; git commit -m"vastav commit"

#switch back to master branch
git switch master 

# modify the same line with - print("Hello Dihitha")
git add .; git commit -m"Dihitha commit"

# Double check you are on master branch and ready for merge vastav branch
git merge vastav

# Observe there is merge conflict
  
Git merge conflict


You can open the file in vi or vs code at your convenience. The conflict is represented by ">>>" and "===" symbols. If two teammates discuss the conflicts and then decide what code lines should be preserved and what lines can be removed.

Git merge conflict and its resolution



Once you save the conflict file, it will be committed to the local repo.

Cherry-pick Merge

How does cherry-pick works? 

We want to pick up a specific commit from a different branch and apply to the current branch. Git provided a subcommand 'cherry-pick'
 
Syntax:
git cherry-pick commit-id

When can I use the Cherry-pick?

  • To make it correct when a commit is made in a different branch accidentally.
  • Preferable way is traditional merges only
  • To apply the changes in an existing commit urgently 
  • Duplicate commits
  • Bug fixing in production
 
Cherry-pick example branches



Example of git cherry-pick
mkdir check-pick; cd cherry-pick; git init .
alias graph="git log --oneline --graph --all" #if it is added to .bashrc you can skip this
touch A; git add .; git commit -m "A"
touch B; git add .; git commit -m "B"
# branch out feature
git checkout -b feature'
touch C; git add . ; git commit -m "C"
touch D; git add .; git commit -m "D"
touch F; git add .; git commit -m "F"
# check git work-tree
graph
git checkout master
touch E; git add .; git commit -m "E"
graph
git cherry-pick 6353156 [select D commit-code]
graph

Execution output as follows:


git cherry-pick example
Here we conclude now with branches and their corresponding merging is easy. we know how to merge the code from another branch. Also know how to be specific.

Have fun with Git/SCM tools!!

Please write your comment while working with this merge and rebase post along with cherry-pick concept and their corresponding execution examples.

Sunday, May 29, 2022

GIT Commands reference for DevOps Engineer

Dear DevOps Engineer!! this post is full of experimenting with git commands running most of them on the 'Git Bash' window in my laptop. I've collected three things about each git command.
  • git command syntax
  • What this git command will do - short descriptions
  • An example command that I've executed
  • Screenshot of that command execution

How do I Start a Fresh repository in Git CLI(local repo)?

You need to create a folder where you would like to start your coding project. The code can be web-site related where you can have multiple directories involved such as HTML files into a folder, all images to be used in the website in a folder, CSS files into a folder, etc. First, make the folder structure for you project then at the root of the project need to initialize the project.  To create empty repo or re-initializing a repo

Syntax :
git init
Note: It is always better to have a fresh directory created and then run the above command to initialize a local Git repository.

git init example



Getting a remote repository to your git client - Local system/Laptop

You can have the remote repository either public or private teams on the following SCM clouds:
  1. GitHub 
  2. GitLab
  3. BitBucket

git clone Syntax:

git clone ssh://git@github.com/[username]/[repository-name].git

Create a local copy of a remote repository of GitHub or Gitlab. Make sure that you're doing first time in that folder, it should not have the same repo that means if your re-run the same command it cannot work.




Please make sure you have the correct access rights and the remote repository exists

Example of using gitlab URL:
 git clone https://github.com/BhavaniShekhar/vtdevops2021feb.git
Vybhava : git clone 


Syntax:
git commit -m "[commit message]"
commit changes
Example:
git commit -m "some message"
git commit command

Syntax:
git checkout -b [branch name origin/[branch name]

Clone a remote branch and switch to it
Example:
git checkout -b feature2
Differences between git checkout & switch command

Syntax:
git checkout [branch name]

Switch to a branch
Example:
git checkout
git checkout command
Syntax :
git checkout -

Switch to the branch last checked out
Example :
git checkout -
git last checkout command
Syntax:
git checkout -- [file-name.txt]

Discard changes to a file on which you are worked recently.
Example:

Syntax:
git push origin [branch name]

push to a branch to your remote repository
 Example:
git push origin slave
git push command
Syntax:
git push -u origin [branch name]

push changes to remote repository (and remember the branch)
Example:
git push -u origin feature1
git push origin command
Syntax:
git push

push changes to remote repository (remembered branch)
Example:
git push
git push command 
Syntax:
git push origin --delete [branch name]

delete a remote branch
 Example:
git push origin --delete feature3
git remote branch delete oush command

git pull command

Syntax:
git pull origin [branch name]

update local repository to the newest commit
Example:
git pull
git pull command

pull changes from remote repository Example:
git pull origin newbranch2

git remote

Syntax:
git remote add origin ssh://git@github.com/[username]/[repository-name].git
    or
 git remote set-url origin ssh://git@github.com/[username]/[repository-name].git

add a remote repository Example 1:
git remote add origin git@gitlab.com:gitops4/docker-exercises.git

set a repository's origin branch to ssh Example 2:
git remote set-url origin git@gitlab.com:gitops4/docker-exercises.git

git log

The syntax for the git log command is:
git log [--summary] [--oneline]


view changes
Example:
git log

git log command
view changes made to files or directory in (detailed)
Example:
    git log --summary
git log summary command
view changes(briefly)
Example:
    git log --oneline
git log oneline command
Syntax:
git diff [source branch] [target branch]

preview changes before merging 

References: 
 Git log command examples https://www.thegeekstuff.com/2014/04/git-log/

Git File Lifecycle

In this post, we will explore, experiment and see git basic files and folder-related commands if you are familiar with the Linux file system this will be easy for you! But, again no need to worry about that we will see every command execution with experiments.


Every software product/server Lifecycle can be visible with their STATUS output, where they are currently if you know then you can move to different Lifecycle state. Let's understand how this navigation happens on the Git repository.

Git File lifecycle status changes with commands


Git Status

Git Status will always compare the files and folders with the indexed with untracked files and display their status. 

Syntax:

git status [options]

This command will check the status of the current branch by comparing it with the master branch
Example:
git status -s 

This `git status` command will show the working tree status. and it is having multiple useful options.
When you use the -s or --short option it will display the concisely where status will be represented with single alphabet 
  1. ' ' = unmodified
  2. M = modified
  3. A = added
  4. D = deleted
  5. R = renamed
  6. C = copied
  7. U = updated but unmerged
In the help page you can find more details about different format output meanings in three different tables.
 

Adding files and folders to the Stage area

We can add files from the work area to the stage area using the `git add` command. 

git add command


Here we can add TEXT files because all program codes will be ASCII text format only.

Syntax:
git add [file-name]
this files can be recently modified shell or python program using some editor.

Add a file to the staging/index area

Example:
 git status # shows nothing
 vi public/product.html # create a file and edit then save it.
 git status # observer it shows red color output.
 git add public/product.html
 git status # shows the changes in green     
   
Adding file to Git repository (local)

Adding entire folder 

You are working on a website project you need to add all the images files (.jpg) required for your website are need to place into a folder called 'images'.  To send from Untracked state files to tracked level or you can say this as work-copy to index area entire folder you can send by providing foldername as argument to git add command.

clear
cd source_code
mkdir images css publish
cd images
#copy some images to images directory here for learning used touch filename.jpg used
cd ..
git add images 
Git add entire folder example

When you observer the the staged files now the two image files will be in green colored that indicates that they are moved to index area of Git repo.

After adding folder status

Adding Current folder content

Similar to Unix/Linux  uses dot to indicate current folder content it may contain

Git add with current directly



Syntax:
git add -A

Adding all folders all files -A

add all new and changed files to the staging area

Git add All Folder



Example:
git add -A




Removing files and directories from repository

Now this can be in three use cases. 
Use case 1: let's see how to remove a file or directory from local repository and also from the filesystem
git rm testcode.file [code-dir] -f
git- rm -f
Remove files from git repository

  git rm publish/product.html # Errors out
git rm -f publish/product.html 
git status 
cd publish; ls # file removed from the filesystem as well
  
Use case 2: removing a file or directory from local repo and not from filesystem
git rm testcode.file [code-dir] --cached
Use case 3: removing a file or directory display a trail run, not really deletes anything from the repository.
git rm testcode.file [code-dir] –dry-run

Check the list of files changed

There are lots of options to check this with git and subcommands Here we will see with ls-files option so that you can see the list of files or folders recently changed
 git ls-files 
List of files in single line space separated
 git ls-files –z 
This is another wonder option with -d , where Git will lists the files or folders which are last delated from the existing repository.
git ls-files -d

Renaming file using git mv

Similar to LInux mv command we can rename a file or folder using git mv command. Here we can have existing file or folder and renamed to new file name or folder. After this if we check the status in short it will shows 'R' that means Renamed.
git mv publish/index.html publish/sanjay.html
git status -s -b
Hope you enjoyed this post of learning love on GitOps!!! Yom be interested in the next git learning post on git branching.

Wednesday, May 25, 2022

Git branching - local and remote

Hello all, in this post specially dedicated to all DevOps/DevSecOps Engineers.  Where their daily routine will have this commands if we know these we can play with them easily if you don't then it will be horrifies you! 

Git Branching explained in detailed

Prerequisites to this you should be aware git basics and to experiment with these command examples should have git installed on your Mac/Linux or git bash on Windows system.

What is the Branch in Git?

A Git branch is a separate line of work, where your work will not disturb other DevOps team members work.
Branch is a simply a movable  pointer to commit, this we can observe using git branch command output wherever you see that '*' prefixed there the pointer pointing. Default branch is master or main (new trend) but it is optional we can name anything when we initiate the git repository.

Main branch is the FIRST branch of your project that means once you run the 'git init' then 'git branch' you can observe this.
On the branch we can have many changes until find the solution and accordingly you need to do multiple commits.

Each branch created for different purpose for example new feature development we name them as 'feature'. similarly we can also have planned releases where we name them as 'release1.0' or 'dev-release'. Again it is flexible to the SCM manager decision.

Why we need to create branch in Git?

The Major reasons for Git branching are:

  • Any new features development could BREAK the code, to save this.
  • No disturbance between two developer codes that means this git branching simplify the collaboration work which gives more agility to the project

Gitting Start Branching

Here I'm with an interesting story about SCM Git branching that helps to work in collaborative environments where DevOps is a culture.

Let me share with you guys about the steps that I've executed in this are realtime requirements.

Top 10 Examples on git branch sub-command 


1. How to create a branch and check using git sub-commands?

To create a new branch from the existing branch. Here you should be on one of the branch. Generally we start from main or master branch.
git branch newbranch
git branch

2. How to jump onto the new branch directly when it is created?

Create a branch and switch to it in single command
git checkout -b feature
git branch

3. How to switch between the git branches?

Switch to branch We have two command options for switching between branches. 
a. Regular subcommand for switching to a branch
git checkout newbranch
git branch
b. New way to switch between the branch
git switch master
git branch

5. How can I view all local repo branches? 

To view all branches which are created in the local repository no option will do or else we can use status with -b to see in single-line output
git branch 
git status -b -s #alternative option

6. How can I see remote branches on my local laptop/local repo?

To view all the remote branches only you need to use -r or --remote as options

This is command will help you to get more info about all the branches are available in the remote git server such as GitHub or BitBucket etc.
 
git branch -r 
git branch --remote 
git branch remote to view your github or bitbucket branches



7. How can I see all local and remote repositories together?

To view all branches including remote branches use -a or --all options 
git branch -a 
git branch --all 
Observe the different colors for each repos.
  1. The green color indicates current working tree(branch)
  2. The red indicates remote repo branches
  3. The white color indicate local branches
git branch all command example



8. Is there any way to delete a git branch in local repo? 

Delete a branch can be allowed when we use the -d or --delete options on a target branch.

Note : You cannot delete the branch on which you are working. Switch to anyother branch then run this.
git branch -d newbranch
git branch --delete newbranch
Removing a branch


[This is best option to delete because it preserves the recent changes made on the local branch] 9. Deleting the remote branch This is delicated operation, double check before you run this command.
git branch -D myremote-branch 
Compare -d and -D example for git branch removal


9. How to delete a remote branch in git command line ?

Better option is to use delete option with push command
git push origin --delete myremote-branch

10. How to rename a branch on git?

Move a branch/Rename 
a. Linux move command used to rename same way here a branch can be renamed and its reflog
git branch -m newbranch dev
git branch --move newbranch dev 
Git branch move or renaming


b. Rename forcefully 
Even though your target branch exist the git move will happen forcefully when we use -M option.
git branch -M newbranch dev


Hope you enjoy this story. Connect with our earlier technical articles might give some new tricks or tips for your project growth and more productive.

Cheers!!

Sunday, May 15, 2022

Controlling EC2 Instance from CLI: AWS automations

When you start learning few commands about AWS CLI you can plan to automate the process with simple bash shell script where you can include set of aws commands into it with simple bash script controls. 

How to automate AWS EC2 stop/start using aws cli?

Objective of this post is to develop a simple controlling script which can use the AWS CLI commands for start-instances, describe-instances, stop-instances and adding bash scripting logic to it. First we start experiment with each aws ec2 command, then we can proceed by collecting those successful commands to form a automation script. Let's explore now.

How to automate AWS EC2 instance using aws-cli


How to start an AWS EC2 instance "start-instances" command


To start the aws instance you need to pass the instance-id as argument. Following is the command example.
aws ec2 start-instances --instance-id i-instancenumber
Please change to your instance-id value replace the instancenumber with yours.
Execution output looks like this:

aws ec2 start-instances execution initially in pending state


aws ec2 stop-instances command

To stop the AWS EC2 Instance you need to pass the instance-id as a argument. Following is the command example.
aws ec2 stop-instances --instance-id i-instancenumber
Please change to your instance-id value replace the instancenumber with yours.

EXECUTION
aws ec2 stop-instances


Describe instance status


The describe-instances-status subcommand will show the InstanceState, InstanceStatus and also SystemStatus. We can pick any of these as per the automation needs.
aws ec2 describe-instances-status --instance-id i-instancenumber
Please change to your instance-id value replace the instancenumber with yours.
Describing instance status specific to InstanceState which can be extracted as Name value and here trick is use the --output to TEXT format.

aws ec2 describe-instance-status --instance-id i-instancenumber \
 --query 'InstanceStatuses[*].InstanceState.Name' --output text
This output is nicely choped to test wheather an instance is in 'running', 'stopped', or 'pending' state. Using this we can decide how to proceed next, if it is running we can move to the logic where stop the instance works. otherwise nothing [] is status then we can proceed to start instance logic.

Execution outputs as follows:

aws ec2 describe-instance-status execution output

How to get the EC2 Instance Public IP address?

The describe-instances subcommand will help us to retrieve all details of instances. So we use this subcommand to pick Private or Public IP Address of given EC2 instance. You need to provide the instanceid to fetch the EC2 instance public IP Address.

aws ec2 describe-instances --instance-id i-instancenumber \
 --query "Reservation[*].PublicIpAddress" --output text
Results the Public IP Address of given ec2 instance

Using we can prepare nice shell script to automate the instance start and stop and checking the status.

Once you get the INSTANCE_IP that is Public IP we can connect with ssh command as shown below:
  ssh -o "StrictHostKeyChecking=no" -i aws-key.pem centos@$INSTANCE_IP
  
Here option -i is used for identity file
and  option 
-o "StrickHostKeyChecking=no" indicates do not prompt for the SSH finger print value entry. You can understand without given this option see use of this.
running automation script output looks like this.

How to modify the security group for running EC2 instance?
There was a problem when I've ran the aws ec2 run command instance was created and able to see it is in Running state. But unfortunately the ssh connectivity failing with the error message "Port 22 refused connection". Here the solution could be the proper security group must be associated with the EC2 instance.

AWS CLI command to modify the security group which is already existing in my other EC2 instance that is Running state and connectivity also normal, from the AWS Console we can get the seurity-group id from the normal instance (node1) can be used in the Issue instance (node2). Two inputs required here node2 instance-id and node1 security group id.

 aws ec2 modify-instance-attribute --instance-id i-instanacenumber  --groups sg-securitygroupid
  
Example screenshot of execution:

 
AWS CLI to modify attribute for running EC2 isntance



Categories

Kubernetes (24) Docker (20) git (13) Jenkins (12) AWS (7) Jenkins CI (5) Vagrant (5) K8s (4) VirtualBox (4) CentOS7 (3) docker registry (3) docker-ee (3) ucp (3) Jenkins Automation (2) Jenkins Master Slave (2) Jenkins Project (2) containers (2) create deployment (2) docker EE (2) docker private registry (2) dockers (2) dtr (2) kubeadm (2) kubectl (2) kubelet (2) openssl (2) Alert Manager CLI (1) AlertManager (1) Apache Maven (1) Best DevOps interview questions (1) CentOS (1) Container as a Service (1) DevOps Interview Questions (1) Docker 19 CE on Ubuntu 19.04 (1) Docker Tutorial (1) Docker UCP (1) Docker installation on Ubunutu (1) Docker interview questions (1) Docker on PowerShell (1) Docker on Windows (1) Docker version (1) Docker-ee installation on CentOS (1) DockerHub (1) Features of DTR (1) Fedora (1) Freestyle Project (1) Git Install on CentOS (1) Git Install on Oracle Linux (1) Git Install on RHEL (1) Git Source based installation (1) Git line ending setup (1) Git migration (1) Grafana on Windows (1) Install DTR (1) Install Docker on Windows Server (1) Install Maven on CentOS (1) Issues (1) Jenkins CI server on AWS instance (1) Jenkins First Job (1) Jenkins Installation on CentOS7 (1) Jenkins Master (1) Jenkins automatic build (1) Jenkins installation on Ubuntu 18.04 (1) Jenkins integration with GitHub server (1) Jenkins on AWS Ubuntu (1) Kubernetes Cluster provisioning (1) Kubernetes interview questions (1) Kuberntes Installation (1) Maven (1) Maven installation on Unix (1) Operations interview Questions (1) Oracle Linux (1) Personal access tokens on GitHub (1) Problem in Docker (1) Prometheus (1) Prometheus CLI (1) RHEL (1) SCM (1) SCM Poll (1) SRE interview questions (1) Troubleshooting (1) Uninstall Git (1) Uninstall Git on CentOS7 (1) Universal Control Plane (1) Vagrantfile (1) amtool (1) aws IAM Role (1) aws policy (1) caas (1) chef installation (1) create organization on UCP (1) create team on UCP (1) docker CE (1) docker UCP console (1) docker command line (1) docker commands (1) docker community edition (1) docker container (1) docker editions (1) docker enterprise edition (1) docker enterprise edition deep dive (1) docker for windows (1) docker hub (1) docker installation (1) docker node (1) docker releases (1) docker secure registry (1) docker service (1) docker swarm init (1) docker swarm join (1) docker trusted registry (1) elasticBeanStalk (1) global configurations (1) helm installation issue (1) mvn (1) namespaces (1) promtool (1) service creation (1) slack (1)