Install git
[root@linuxhost ~]# yum install git -y
Step 2) Configure global configurations for any repository that we use in future.
[osboxes@linuxhost ~]$ git config --global user.name "Pavan Bandaru"
[osboxes@linuxhost ~]$ git config --global user.email "pavankumarbandaru@hotmail.com"
[osboxes@linuxhost ~]$ git config --global --list
user.name=Pavan Bandaru
user.email=pavankumarbandaru@hotmail.com
It created a config file under user home directory
[osboxes@linuxhost ~]$ cat ~/.gitconfig
[user]
name = Pavan Bandaru
email = pavankumarbandaru@hotmail.com
You can also check what Git thinks a specific key’s value is by typing git config <key>:
[osboxes@linuxhost ansible-playbooks]$ git config user.name
Pavan Bandaru
.
Create a projects directory
[osboxes@linuxhost ~]$ pwd
/home/osboxes
[osboxes@linuxhost ~]$ cd projects/
[osboxes@linuxhost projects]$ ls -rlt
total 0
Initialize a git repository
Create a new repository: By default Git will create a branch called master when you create a new repository with git init
[osboxes@linuxhost projects]$ git init sample-repo
Initialized empty Git repository in /home/osboxes/projects/sample-repo/.git/
[osboxes@linuxhost projects]$ ls -rlt
total 0
drwxrwxr-x. 3 osboxes osboxes 18 Mar 5 15:47 sample-repo
[osboxes@linuxhost projects]$ cd sample-repo/
[osboxes@linuxhost sample-repo]$ ls -rlt
total 0
[osboxes@linuxhost sample-repo]$ pwd
/home/osboxes/projects/sample-repo
[osboxes@linuxhost sample-repo]$ ls -lart
total 0
drwxrwxr-x. 3 osboxes osboxes 25 Mar 5 15:47 ..
drwxrwxr-x. 3 osboxes osboxes 18 Mar 5 15:47 .
drwxrwxr-x. 7 osboxes osboxes 119 Mar 5 15:47 .git
[osboxes@linuxhost sample-repo]$
Initialize git repository on existing project: I have an existing project ansible-playbooks and I wanted to create a repository with an existing project name. Run "git init" command under your project name directory.
/home/osboxes/projects/ansible-playbooks
[osboxes@linuxhost ansible-playbooks]$ ls -rlt
total 52
-rw-------. 1 osboxes osboxes 0 Feb 27 09:41 linux-user.yml.txt
-rw-------. 1 osboxes osboxes 10 Mar 3 15:59 password.txt
-rw-------. 1 osboxes osboxes 355 Mar 3 15:59 mynewplaybook
-rw-------. 1 osboxes osboxes 1197 Mar 3 15:59 linux-user.yml
-rw-------. 1 osboxes osboxes 259 Mar 3 15:59 install-packages.yml
-rw-------. 1 osboxes osboxes 491 Mar 3 15:59 create_dir.yml
-rw-------. 1 osboxes osboxes 654 Mar 3 15:59 apache-install.yml
-rw-------. 1 osboxes osboxes 14 Mar 3 15:59 inventory.txt
-rw-------. 1 osboxes osboxes 311 Mar 3 15:59 install-packages_1.yml
-rw-------. 1 osboxes osboxes 342 Mar 3 15:59 addusers.yml
-rw-------. 1 osboxes osboxes 295 Mar 3 16:19 dict.yml
-rw-------. 1 osboxes osboxes 716 Mar 3 19:39 register.yml
-rw-------. 1 osboxes osboxes 429 Mar 3 20:15 limit-output.yml
-rw-------. 1 osboxes osboxes 508 Mar 3 20:18 inventory-loops.yml
[osboxes@linuxhost ansible-playbooks]$ git init
Initialized empty Git repository in /home/osboxes/projects/ansible-playbooks/.git/
Check the status of your files (We are on the master branch and have not added any files)
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
addusers.yml
apache-install.yml
create_dir.yml
dict.yml
install-packages.yml
install-packages_1.yml
inventory-loops.yml
inventory.txt
limit-output.yml
linux-user.yml
linux-user.yml.txt
mynewplaybook
password.txt
register.yml
nothing added to commit but untracked files present (use "git add" to track)
Add the file to the git repository and commit the changes. ( Now we added a file addusers.yml file and did not commit the changes. git status will track the changes)
[osboxes@linuxhost ansible-playbooks]$ git add addusers.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: addusers.yml
Untracked files:
(use "git add <file>..." to include in what will be committed)
apache-install.yml
create_dir.yml
dict.yml
install-packages.yml
install-packages_1.yml
inventory-loops.yml
inventory.txt
limit-output.yml
linux-user.yml
linux-user.yml.txt
mynewplaybook
password.txt
register.yml
commit the changes
[osboxes@linuxhost ansible-playbooks]$ git commit -m "First commit"
[master (root-commit) 51a789e] First commit
1 file changed, 15 insertions(+)
create mode 100644 addusers.yml
Use git add * or git add . incase if you want to add all the files
[osboxes@linuxhost ansible-playbooks]$ git add *
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: apache-install.yml
new file: create_dir.yml
new file: dict.yml
new file: install-packages.yml
new file: install-packages_1.yml
new file: inventory-loops.yml
new file: inventory.txt
new file: limit-output.yml
new file: linux-user.yml
new file: linux-user.yml.txt
new file: mynewplaybook
new file: password.txt
new file: register.yml
Untracked files:
(use "git add <file>..." to include in what will be committed)
.remote-sync.json
commit the changes
[osboxes@linuxhost ansible-playbooks]$ git commit -m "Second commit - Added all the files"
[master 2debee7] Second commit - Added all the files
13 files changed, 191 insertions(+)
create mode 100644 apache-install.yml
create mode 100644 create_dir.yml
create mode 100644 dict.yml
create mode 100644 install-packages.yml
create mode 100644 install-packages_1.yml
create mode 100644 inventory-loops.yml
create mode 100644 inventory.txt
create mode 100644 limit-output.yml
create mode 100644 linux-user.yml
create mode 100644 linux-user.yml.txt
create mode 100644 mynewplaybook
create mode 100644 password.txt
create mode 100644 register.yml
Check the status now. There are no new file to commit.
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
nothing to commit, working tree clean
Modify an existing file and check the status ( I have modified a file addusers.yml )
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: addusers.yml
no changes added to commit (use "git add" and/or "git commit -a")
Now you have two options to add and commit the changes
1) git add adduser.yml
git commit -m "Second commit on addusers"
2) git commit -am "Second commit on addusers" ( -a is for adding, -m is for commit message )
I used option 2 for adding and commit my changes
[osboxes@linuxhost ansible-playbooks]$ git commit -am "Second commit on addusers"
[master ea3b865] Second commit on addusers
1 file changed, 1 insertion(+), 1 deletion(-)
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
nothing to commit, working tree clean
Now modify an existing file and create a new file and check the status
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: addusers.yml
Untracked files:
(use "git add <file>..." to include in what will be committed)
dictionary.yml
no changes added to commit (use "git add" and/or "git commit -a")
Now add a file adduser.yml and don't add the new file and check the status ( Here one file addusers.yml is in staging area )
[osboxes@linuxhost ansible-playbooks]$ git add addusers.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: addusers.yml
Untracked files:
(use "git add <file>..." to include in what will be committed)
dictionary.yml
Add the second file and check the status ( Both the file are in "to be commited" state which is staging area )
[osboxes@linuxhost ansible-playbooks]$ git add dictionary.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: addusers.yml
new file: dictionary.yml
commit the change
[osboxes@linuxhost ansible-playbooks]$ git commit -m "Changes to adduser and added new file dictionar"
[master 34f1071] Changes to adduser and added new file dictionar
2 files changed, 15 insertions(+), 1 deletion(-)
create mode 100644 dictionary.yml
[osboxes@linuxhost ansible-playbooks]$
Un stage the added file ( Now I have modified the file and added but I don't want to commit the changes rather I wanted to un stage the file from Staging area. Use git restore --staged <file> command to revert the change )
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: addusers.yml
no changes added to commit (use "git add" and/or "git commit -a")
[osboxes@linuxhost ansible-playbooks]$ git add addusers.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: addusers.yml
Unstage the changes
[osboxes@linuxhost ansible-playbooks]$ git restore --staged addusers.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: addusers.yml
no changes added to commit (use "git add" and/or "git commit -a")
[osboxes@linuxhost ansible-playbooks]$
Use the following command incase if you want to discard the changes in your working directory:
git restore <file>
[osboxes@linuxhost ansible-playbooks]$ git restore addusers.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
nothing to commit, working tree clean
Display the history of commits are part of the current repository. ( User git log command )
[osboxes@linuxhost ansible-playbooks]$ git log
commit 34f107160fd8ded3fd8a0e8edbd2088faa6570f6 (HEAD -> master)
Author: Pavan Bandaru <pavankumarbandaru@hotmail.com>
Date: Fri Mar 5 16:51:49 2021 -0500
Changes to adduser and added new file dictionar
commit ea3b86521585d7406d7d85e5448f9ea94dee3290
Author: Pavan Bandaru <pavankumarbandaru@hotmail.com>
Date: Fri Mar 5 16:35:04 2021 -0500
Second commit on addusers
commit 2debee75efcf276445cadbe2937e646c13166e6e
Author: Pavan Bandaru <pavankumarbandaru@hotmail.com>
Date: Fri Mar 5 16:23:39 2021 -0500
Second commit - Added all the files
commit 51a789e2399329a799245f90d63327f4a9b4715b
Author: Pavan Bandaru <pavankumarbandaru@hotmail.com>
Date: Fri Mar 5 16:19:50 2021 -0500
First commit
[osboxes@linuxhost ansible-playbooks]$ git log --oneline --graph --decorate --color
* 34f1071 (HEAD -> master) Changes to adduser and added new file dictionar
* ea3b865 Second commit on addusers
* 2debee7 Second commit - Added all the files
* 51a789e First commit
Revert already committed changes or remove files from git repository ( File has been deleted when we enter git rm <file> how ever the changes are is still in staging area. So commit the changes you have made )
[osboxes@linuxhost ansible-playbooks]$ git rm dictionary.yml
rm 'dictionary.yml'
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: dictionary.yml
[osboxes@linuxhost ansible-playbooks]$ git commit -m "remove the file dictionary"
[master 1dc84bb] remove the file dictionary
1 file changed, 14 deletions(-)
delete mode 100644 dictionary.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
nothing to commit, working tree clean
Another way of removing the files using git add -u
[osboxes@linuxhost ansible-playbooks]$ rm dict.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: dict.yml
no changes added to commit (use "git add" and/or "git commit -a")
osboxes@linuxhost ansible-playbooks]$ git add -u
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: dict.yml
[osboxes@linuxhost ansible-playbooks]$ git commit -m " remove file dict.yml "
[master dbdb1e4] remove file dict.yml
1 file changed, 14 deletions(-)
delete mode 100644 dict.yml
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
nothing to commit, working tree clean
Moving files using git mv command
[osboxes@linuxhost ansible-playbooks]$ git mv addusers.yml new
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: addusers.yml -> new/addusers.yml
[osboxes@linuxhost ansible-playbooks]$ git commit -m " addusers file moved to a different location "
[master 8f5dcd5] addusers file moved to a different location
1 file changed, 0 insertions(+), 0 deletions(-)
rename addusers.yml => new/addusers.yml (100%)
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
nothing to commit, working tree clean
[osboxes@linuxhost ansible-playbooks]$
Ignoring Files
The rules for the patterns you can put in the .gitignore file are as follows:
- Blank lines or lines starting with # are ignored.
- Standard glob patterns work, and will be applied recursively throughout the entire working tree.
- You can start patterns with a forward slash (/) to avoid recursivity.
- You can end patterns with a forward slash (/) to specify a directory.
- You can negate a pattern by starting it with an exclamation point (!).
Glob patterns are like simplified regular expressions that shells use. An asterisk (*) matches zero or more characters; [abc] matches any character inside the brackets (in this case a, b, or c); a question mark (?) matches a single character; and brackets enclosing characters separated by a hyphen ([0-9]) matches any character between them (in this case 0 through 9). You can also use two asterisks to match nested directories; a/**/z would match a/z, a/b/z, a/b/c/z, and so on.
Here is another example .gitignore file:
# ignore all .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in any directory named build
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf
Example: I want to ignore files ending with .log
[osboxes@linuxhost ansible-playbooks]$ cat .gitignore
*.log
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
nothing added to commit but untracked files present (use "git add" to track)
[osboxes@linuxhost ansible-playbooks]$ git add .gitignore
[osboxes@linuxhost ansible-playbooks]$ git commit -m "added gitignore file"
[master 8e582b3] added gitignore file
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
Viewing Your Staged and Unstaged Changes using git diff command ( git status will not show you exactly what is changed. git diff --staged command compares your staged changes to your last commit )
[osboxes@linuxhost ansible-playbooks]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: register-new.yml
[osboxes@linuxhost ansible-playbooks]$ git diff --staged
diff --git a/register-new.yml b/register-new.yml
index 96dea29..8986d05 100644
--- a/register-new.yml
+++ b/register-new.yml
@@ -1,6 +1,6 @@
---
-- name: Register loop variable results
+- name: Register loop example^M
hosts: all
become: true
become_user: root
I have remove few lines of code in loop.yml in working directory. use git diff to see the changes in the file that are staged and the changes that are unstaged.
[osboxes@linuxhost ansible-playbooks]$ git diff
diff --git a/loops.yml b/loops.yml
index dba93c1..a628e21 100644
--- a/loops.yml
+++ b/loops.yml
@@ -13,8 +13,3 @@
debug:
msg: "{{ item }}"
loop: "{{ ansible_play_batch }}"
-
- - name: Show all the hosts in the inventory
- debug:
- msg: "{{ item }}"
- loop: "{{ query('inventory_hostnames', 'all') }}"
No comments:
Post a Comment