GIT Commands

In this article we will see git commands from beginner to advance. It's important to know everyone if you are developer or devops.

1. git config

Command: git config –global “[name]”  

Command: git config –global “[email address]”  

This command sets the author name and email address respectively to be used with your commits.

2. git init

Command: git init [repository name]

This command is used to start a new repository.

3. git add

Command: git add [file]  

This command adds a file to the staging area.

Command: git add *  

This command adds one or more to the staging area.

4. git commit

Command: git commit -m “[ Type in the commit message]”  

This command records or snapshots the file permanently in the version history.

Command: git commit -a  

This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

5. git diff

Command: git diff  

This command shows the file differences which are not yet staged.

Command: git diff –staged 

This command shows the differences between the files in the staging area and the latest version present.

Command: git diff [first branch] [second branch]  

This command shows the differences between the two branches mentioned.

6. git reset

Command: git reset [file]  

This command unstages the file, but it preserves the file contents.

Command: git reset [commit]  

This command undoes all the commits after the specified commit and preserves the changes locally.

Command: git reset –hard [commit]  

This command discards all history and goes back to the specified commit.

7. git status

Command: git status  

This command lists all the files that have to be committed.

8. git rm

Command: git rm [file]  

This command deletes the file from your working directory and stages the deletion.

9. git log

Command: git log  

This command is used to list the version history for the current branch.

Command: git log –follow[file]  

This command lists version history for a file, including the renaming of files also.

10. git show

Command: git show [commit]  

This command shows the metadata and content changes of the specified commit.

11. git tag

Command: git tag [commitID]  

This command is used to give tags to the specified commit.

12. git branch

Command: git branch  

This command lists all the local branches in the current repository.

Command: git branch [branch name]  

This command creates a new branch.

Command: git branch -d [branch name]  

This command deletes the feature branch.

13. git checkout

Command: git checkout [branch name]  

This command is used to switch from one branch to another.

Command: git checkout -b [branch name]  

This command creates a new branch and also switches to it.

14. git merge

Command: git merge [branch name]  

This command merges the specified branch’s history into the current branch.

15. git remote

Command: git remote add [variable name] [Remote Server Link]  

This command is used to connect your local repository to the remote server.

16. git push

Command: git push [variable name] master  

This command sends the committed changes of master branch to your remote repository.

Command: git push [variable name] [branch]  

This command sends the branch commits to your remote repository.

Command: git push –all [variable name]  

This command pushes all branches to your remote repository.

Command: git push [variable name] :[branch name]  

This command deletes a branch on your remote repository.

17. git pull

Command: git pull [Repository Link]  

This command fetches and merges changes on the remote server to your working directory.

18. git stash

Command: git stash save  

This command temporarily stores all the modified tracked files.

Command: git stash pop  

This command restores the most recently stashed files.

Command: git stash list  

This command lists all stashed change sets.

Command: git stash drop  

This command discards the most recently stashed change set.

19. git clone

Command: git clone [url]  

This command is used to obtain a repository from an existing URL.

Ansible Playbook Roles

In this article, we will read about ansible-playbook roles. what is this and how does it work.

Roles let you automatically load related vars, files, tasks, handlers, and other Ansible artifacts based on a known file structure. After you group your content into roles, you can easily reuse them and share them with other users.

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of the playbook allows you to logically break the playbook into reusable components.

An Ansible role has a defined directory structure with eight main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use.

For example:

# playbooks

tasks/main.yml - the main list of tasks that the role executes.

handlers/main.yml - handlers, which may be used within or outside this role.

library/ - modules, which may be used within this role.

defaults/main.yml - default variables for the role. These variables have the lowest priority of any variables available and can be easily overridden by any other variable, including inventory variables.

vars/main.yml - other variables for the role.

files/main.yml - files that the role deploys.

templates/main.yml - templates that the role deploys.

meta/main.yml - metadata for the role, including role dependencies.


[user@ip]# mkdir -p playbook/roles/webserver/tasks

[user@ip]# tree                      (It will show tree hierarchy of playbook roles)

[user@ip]# cd playbook/

[user@ip playbook]# touch roles/webserver/tasks/main.yml

[user@ip playbook]# touch master.yml

[user@ip playbook]# vi roles/webserver/tasks/main.yml

inside main.yml type

-name: install apache
yum: pkg=httpd state=latest

and the save it and exit

[user@ip playbook]# vi master.yml

--- #Playbook Roles
- host: all
  user: ansible
 become: yes
 connection: ssh
    - webserver

and then save it and exit

[user@ip playbook]# ansible-playbook master.yml

 Ansible Playbook Vault

In this article, we will read about ansible-playbook vault. what is a vault and how to write code ansible-playbook vault.

Ansible allows keeping sensitive data such as passwords or keys in encrypted files rather than plain text in your playbooks.

Creating a new encrypted playbook
#ansible-vault create vault.yml

Edit the encrypted playbook
#ansible-vault edit vault.yml

To change the password encrypted playbook
#ansbile-vault rekey vault.yml

To encrypt an existing playbook
#ansible-vault encrypt vault.yml

To decrypt an encrypted playbook
#ansible-vault decrypt target.yml

Ansible Playbooks conditions

In this article, we will see ansible-playbook condition which is more important and whenever we have different scenarios, we put conditions according to the scenario.


When statement

Sometimes you want to skip a particular command on a particular node.

--- # Condition Playbook

- hosts: demo
  user: ansible
  become: yes
  connection: ssh
        - name: install apache on debian
          command: apt-get -y apache2
          when: ansible_os_family=="Debian"
        - name: install apache for redhat
          command: yum -y install https
          when: ansible_os_family=="RedHat"

See diagram for your reference that how to write code in playbook and output after execution file.

Ansible Playbook

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. It is human-readable data serialization language and commonly used for configuration files.

Playbook is like a file where you write codes consist of vars, tasks, handlers, files, templates and roles.

Each playbook is composed of one or more modules in a list and Module is a collection of configuration files.

Playbooks are divided into many sections such as -

Target Section = Defines that host against which playbooks task has to be executed

Variable Section = variables are names used to hold one or more values

Task Section = List of all modules that we need to run in order.

YAML (Yet another Markup Language)

For ansible, nearly every YAML files start with a list. Each item in the list is a list of key-value pairs commonly call a dictionary. 

All YAML files have to begin with "---" and end with "..."

All members of a listed line must begin with the same indentation level starting with "-"

for example:

--- # A list of fruits
        - Apple
        - Mango
        - Banana
        - Pineapple
        - Grapes

A dictionary is represented in a simple key: value form

for example:

--- # A list of customer
- customer:
        name: Rakesh
        job: Trainer
        skills: Ansible
        exp: 7 years

Extension for playbook files is .yml

Note:- There should be space between : and value

[user@ip]# vi target.yml

--- # First Test playbook
    - hosts: demo
      user: ansible
      become: yes
      connection: ssh
      gather_facts: yes

now to execute this playbook

[user@ip]# ansible-playbook target.yml

Ansible Ad-hoc commands

Ad-hoc commands are commands which can be run individually to perform quick functions.

These ad-hoc commands are not used for configuration management and development because these commands are of one-time usage.

The ansible ad-hoc commands use /usr/bin/ansible command line tool to automate a single task.


Ad hoc commands Syntax


#ansible <hosts> -a <"arguments"> -u <username> [--become]


#ansible demo -a “ls”


Hosts: It can be an entry in the inventory file. For specifying all hosts in the inventory, use all or "*".

Arguments: We should pass values that are required by the module. It can change according to the module used.

Username: It specifies the user account in which Ansible can execute commands.

Become: It's an optional parameter specified when we want to run operations that need sudo privilege. By default, it becomes false.

Ad-hoc commands

To check ls on demo group of hosts :

[ansible@ip]#ansible demo -a “ls”

To create files on demo group of hosts :

[ansible@ip]#ansible demo -a “touch file1”

To create files on all group of hosts :

[ansible@ip]#ansible all -a “touch file2”

To check list of hidden file with detailed demo group of hosts :

[ansible@ip]#ansible demo -a “ls -al”

To install httpd on demo group of hosts :

[ansible@ip]#ansible demo -a “sudo yum install httpd –y”

To install httpd on demo group with sudo permission of hosts :

[ansible@ip]#ansible demo -ba “yum install httpd –y”

To uninstall httpd on demo group with sudo permission of hosts :

[ansible@ip]#ansible demo -ba “yum remove httpd –y”