Showing posts with label GIT. Show all posts
Showing posts with label GIT. Show all posts

Tuesday, August 29, 2023

Connect from Local Git to Github


GIT


Create a user git

chown -R git:git /home/git


su git


$ cd /home/git


$ mkdir .ssh


$ cd .ssh


$ git config --global user.name "git"


$ git config --global user.email "yashwanthsn@gmail.com"


$ git config --list


user.name=git
user.email=yashwanthsn@gmail.com


Create a repository on GITHUB: https://github.com/new
enter the details and click on create repository


Create a "automation" directory on local git

$ mkdir automation


$ cd automation


$ pwd
/home/git/automation


run git init command
Add SSH git link using git remote add origin command

git remote add origin git@github.com:yashwanthsn2020/automation.git


you can change this to https git link later using git remote set-url origin command

git remote set-url origin git@github.com:yashwanthsn2020/automation.git

Create a config file under $HOME/.ssh in local git

check on how to add SSH Keys from local Git to GITHUB repository -> Generate SSH Keys for Github GIT
Now check the branch and run git push command using git branch and git push commands
Now go to GITHUB -> https://github.com and click on automation repository

In my case the link is https://github.com/yashwanthsn2020/automation


You will be able to see the file that we pushed from local git to github

Clone Git Repository on Jenkins and execute a script

 GIT and Jenkins


Create a new job and select Freestyle project. Go directly to Build section and select "Execute Shell"

The jenkin job runs as SYSTEM user, so ensure you have access to folder where you clone the files.

In my case, I have a SYSTEM user on the system with $HOME directory /home/system. Will close git repo into scripts folder

added following entries in the command section

cd /home/system

rm -rf scripts

git clone https://github.com/yashwanthsn2020/automation.git scripts
./scripts/api_showpassword.sh


./scripts/listfiles.sh


./scripts/crontab.sh

 Click on Save

Click on Build Now, check the console output

Generate SSH Keys for Github GIT

 

GIT


In your local git, run the ssh-keygen command on your linux box from $HOME/.ssh folder
In my case $HOME is set /home/git

Command to run: ssh-keygen -t rsa -b 4096 -C "email_id"

ssh-keygen -t rsa -b 4096 -C "yashwanthsn@gmail.com"

Login to GITHUB: https://github.com

If you do not have an account, create one and login. Click on Profile -> Settings
Click on SSH and GPG keys, click on "New SSH Key" button and copy the $HOME/.ssh/id_rsa.pub public keys generated from step1 and paste it under Key section, add a name and then click on "Add SSH Key" button

Merge branches in Git

 

GIT


In your local Git, create a directory test and then change directory to test.
check out the branches and then create a branch called test. 


Switch to the branch test

mkdir test; cd test
 

git branch
 

git checkout -b test -> you will be switched to test branch
 

git branch

Create a file and then run the following git commands

git add .
 

git commit -m "commit message"
 

git push origin test -> Very important as you are pushing it from test branch (most make mistakes and uses git push origin master)
This push from test branch shows that, we need to create a pull request for 'test' on GitHub on the link - https://github.com/yashwanthsn2020/automation/pull/new/test
If you wish to delete test branch, you can do so by switching to master and then run
git branch -D test

On the GitHub, the file is successfully pulled
Check out the local master repo, we still have 3 files and test_branch_script.sh is not present
run git pull origin master and the new file will be pulled to the local master repo
Note: When you are on local master repo use git push origin master and when you are the different branches other than master run git push origin <branch_name>. In my case it was git push origin test

Add Git Branch Name to Terminal Prompt

 

GIT


Go to $HOME/.ssh folder edit .bash_profile and add the following entries and run the command-> . .bash_profile

git@yashwanthsn-VirtualBox .ssh $ cat .bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^]/d' -e 's/ (.*)/ (\1)/'
}
export PS1="\u@\h \W[\033[32m]\$(parse_git_branch)[\033[00m] $ "

Go to local Git repository directory, and immediately you will see the branch listed at the prompt
Create a branch called test and switch to test branch by issuing following Git command

git checkout -b test


Also list out branches by running git branch command

Git branch


You can switch between the branch using

git checkout <branch-name>

Install Git on Linux

 

GIT


Check if Git is already installed on the linux system
If git is not installed, run apt install git
Now that git is installed on your linux system, go ahead and explore

Refer to GIT related posts on hands on

GIT one liners

 

GIT One-Liners

  • git add -- Add files to the index
    (git add . | git add <filename>)
  • git status -- Obtain a summary of which files have changes that are staged for the next commit
  • git commit -m <msg> -- Record changes to the repository
    (git commit -m "first commit")
  • git branch -- List, create, or delete branches
    (git branch test | git branch -D test)
  • git checkout -- Switch branches or restore working tree files
    (git checkout -b test | git checkout master)
  • git clone -- Clone a repository into a new directory
    (git clone https://github.com/test2020/test.git)
  • git init -- Create an empty Git repository or reinitialize an existing one
  • git log -- Show commit logs
  • git merge [branch] -- Combines the specified branch's history into current branch
  • git pull -- Fetch from and integrate with another repository or a local branch
    (git pull origin master)
  • git push -- Update remote refs along with associated objects
    (git push origin master | git push origin master -f)
  • git rm -- Remove files from the working tree and from the index
  • git switch -- Switch branches
    (git switch test | git switch master)
  • git remote -- Manage set of tracked repositories
    (git remote | git remote -v | git remote add origin https://github.com/test2020/test.git | git remote set-url origin
    https://github.com/test2020/test.git)
  • git config --list -- lists all config values set
  • git config --global user.name "[name]" -- Sets the name you want attached to your commit transactions
  • git config --global user.email "[email address]" -- Sets the email you want attached to your commit transactions

Working with Ansible and GIT

  

Ansible

Create a Repository in Github


git@github.com:yashwanthsn2020/AnsibleRepo.git

Add id_rsa.pub key content into GITHUB -> Profile -> Settings -> SSH and GPG Keys -> New SSH Key

Enter a Name and content and click on Add SSH Key

Install GIT on the Ubuntu

ansible@ansible-VirtualBox:~/Desktop$ sudo apt update
[sudo] password for ansible:
Hit:1 https://download.docker.com/linux/ubuntu groovy InRelease                                                                                       
Get:2 http://security.ubuntu.com/ubuntu groovy-security InRelease [110 kB]                                                                            
Hit:3 http://in.archive.ubuntu.com/ubuntu groovy InRelease                                 
Get:4 http://in.archive.ubuntu.com/ubuntu groovy-updates InRelease [115 kB]
Get:5 http://security.ubuntu.com/ubuntu groovy-security/main amd64 DEP-11 Metadata [4,676 B]
Get:6 http://security.ubuntu.com/ubuntu groovy-security/universe amd64 DEP-11 Metadata [4,552 B]
Get:7 http://in.archive.ubuntu.com/ubuntu groovy-backports InRelease [101 kB]                                                                                                                            
Get:8 http://in.archive.ubuntu.com/ubuntu groovy-updates/main amd64 DEP-11 Metadata [40.6 kB]                                                                                                            
Get:9 http://in.archive.ubuntu.com/ubuntu groovy-updates/universe amd64 DEP-11 Metadata [104 kB]                                                                                                         
Get:10 http://in.archive.ubuntu.com/ubuntu groovy-updates/multiverse amd64 DEP-11 Metadata [2,468 B]                                                                                                     
Get:11 http://in.archive.ubuntu.com/ubuntu groovy-backports/universe amd64 DEP-11 Metadata [600 B]                                                                                                       
Fetched 483 kB in 7s (68.4 kB/s)                                                                                                                                                                         
Reading package lists... Done
Building dependency tree      
Reading state information... Done
251 packages can be upgraded. Run 'apt list --upgradable' to see them.

ansible@ansible-VirtualBox:~/Desktop/Ansible$ sudo apt install git
Reading package lists... Done
Building dependency tree      
Reading state information... Done
Suggested packages:
  git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
  git
0 upgraded, 1 newly installed, 0 to remove and 251 not upgraded.
Need to get 0 B/4,928 kB of archives.
After this operation, 40.0 MB of additional disk space will be used.
Selecting previously unselected package git.
(Reading database ... 271098 files and directories currently installed.)
Preparing to unpack .../git_1%3a2.27.0-1ubuntu1.1_amd64.deb ...
Unpacking git (1:2.27.0-1ubuntu1.1) ...
Setting up git (1:2.27.0-1ubuntu1.1) ...
ansible@ansible-VirtualBox:~/Desktop/Ansible$ 

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git --version
git version 2.27.0

Clone the Github Ansible respository

ansible@ansible-VirtualBox:~/Desktop/Ansible$ git clone git@github.com:yashwanthsn2020/AnsibleRepo.git
Cloning into 'AnsibleRepo'...
The authenticity of host 'github.com (13.234.176.102)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.234.176.102' (RSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

ansible@ansible-VirtualBox:~/Desktop$ ls -ltr
total 28
-rw-rw-r-- 1 ansible ansible  119 Apr 23 15:24 pip.yml
-rwxrwxr-x 1 ansible ansible   87 Apr 23 20:45 shell.sh
-rw-rw-r-- 1 ansible ansible  801 Apr 23 21:23 docker.yml
-rw-rw-r-- 1 ansible ansible  278 Apr 25 12:07 apache.yml
-rw-rw-r-- 1 ansible ansible  284 Apr 25 12:19 docker_1.yml
-rw-rw-r-- 1 ansible ansible  209 Apr 25 14:51 shell.yml
drwxrwxr-x 3 ansible ansible 4096 Apr 25 14:56 AnsibleRepo

ansible@ansible-VirtualBox:~/Desktop$ cd AnsibleRepo
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ ls -ltr
total 4
-rw-rw-r-- 1 ansible ansible 13 Apr 25 14:56 README.md

Add/Config username and email

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git config --global user.name "Yash Ansible"
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git config --global user.email "yashwanthsn@gmail.com"

Ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ cat ~/.gitconfig
[user]
    name = Yash Ansible
    email = yashwanthsn@gmail.com

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    apache.yml
    docker.yml
    docker_1.yml
    pip.yml
    shell.sh
    shell.yml

nothing added to commit but untracked files present (use "git add" to track)

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git init
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git add .
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git commit -m "Adding Ansible files into Github repo"
[main 887fcd0] Adding Ansible files into Github repo
 6 files changed, 76 insertions(+)
 create mode 100644 apache.yml
 create mode 100644 docker.yml
 create mode 100644 docker_1.yml
 create mode 100644 pip.yml
 create mode 100755 shell.sh
 create mode 100644 shell.yml

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git remote add origin git@github.com:yashwanthsn2020/AnsibleRepo.git
fatal: remote origin already exists.

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git branch
* main
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git push origin main
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (11/11), 1.56 KiB | 399.00 KiB/s, done.
Total 11 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), done.
To github.com:yashwanthsn2020/AnsibleRepo.git
   d7a7a1a..d059cfc  main -> main
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$

Refresh the Github repo page and you can see that the files are present


 
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

Updated shell.yml file

ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ pwd
/home/ansible/Desktop/AnsibleRepo
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ vi shell.yml
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git add shell.yml
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git commit -m "Updated the locationof shell.sh"
[main 51404c1] Updated the locationof shell.sh
 1 file changed, 1 insertion(+), 1 deletion(-)
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git branch
* main
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 324 bytes | 324.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:yashwanthsn2020/AnsibleRepo.git
   d059cfc..51404c1  main -> main
ansible@ansible-VirtualBox:~/Desktop/AnsibleRepo$ git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean